Skip to content

Commit aaabfc8

Browse files
committed
Some GeoJSON fixes
1 parent 665d1bf commit aaabfc8

File tree

5 files changed

+21
-5
lines changed

5 files changed

+21
-5
lines changed

src/Tqdev/PhpCrudApi/GeoJson/Geometry.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Geometry implements \JsonSerializable
1313
"MultiLineString",
1414
"Polygon",
1515
"MultiPolygon",
16-
"GeometryCollection",
16+
//"GeometryCollection",
1717
];
1818

1919
public function __construct(string $type, array $coordinates)
@@ -26,11 +26,16 @@ public static function fromWkt(string $wkt): Geometry
2626
{
2727
$bracket = strpos($wkt, '(');
2828
$type = strtoupper(trim(substr($wkt, 0, $bracket)));
29+
$supported = false;
2930
foreach (Geometry::$types as $typeName) {
3031
if (strtoupper($typeName) == $type) {
3132
$type = $typeName;
33+
$supported = true;
3234
}
3335
}
36+
if (!$supported) {
37+
throw new \Exception('Geometry type not supported: ' . $type);
38+
}
3439
$coordinates = substr($wkt, $bracket);
3540
if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
3641
$coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
@@ -39,9 +44,8 @@ public static function fromWkt(string $wkt): Geometry
3944
$json = $coordinates;
4045
$coordinates = json_decode($coordinates);
4146
if (!$coordinates) {
42-
echo $json;
47+
throw new \Exception('Could not decode WKT: ' . $wkt);
4348
}
44-
4549
return new Geometry($type, $coordinates);
4650
}
4751

tests/fixtures/blog_mysql.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ INSERT INTO `countries` (`name`, `shape`) VALUES
114114
('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
115115
('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
116116
('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')),
117-
('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))'));
117+
('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))')),
118+
('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))'));
118119

119120
DROP TABLE IF EXISTS `events`;
120121
CREATE TABLE `events` (

tests/fixtures/blog_pgsql.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ INSERT INTO "countries" ("name", "shape") VALUES
248248
('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
249249
('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
250250
('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')),
251-
('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))'));
251+
('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))')),
252+
('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))'));
252253

253254
--
254255
-- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres

tests/fixtures/blog_sqlsrv.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,8 @@ INSERT [countries] ([name], [shape]) VALUES (N'Mpoly1', N'MULTIPOLYGON (((30 20,
360360
GO
361361
INSERT [countries] ([name], [shape]) VALUES (N'Mpoly2', N'MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))')
362362
GO
363+
INSERT [countries] ([name], [shape]) VALUES (N'Gcoll', N'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))')
364+
GO
363365

364366
INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)
365367
GO

tests/functional/001_records/081_read_countries_as_geojson.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ Content-Type: application/json
6161
Content-Length: 229
6262

6363
{"type":"Feature","properties":{"id":10,"name":"Mpoly2"},"geometry":{"type":"MultiPolygon","coordinates":[[[[40,40],[20,45],[45,30],[40,40]]],[[[20,35],[10,30],[10,10],[30,5],[45,20],[20,35]],[[30,20],[20,15],[20,25],[30,20]]]]}}
64+
===
65+
GET /geojson/countries/11
66+
===
67+
500
68+
Content-Type: application/json
69+
Content-Length: 73
70+
71+
{"code":9999,"message":"Geometry type not supported: GEOMETRYCOLLECTION"}

0 commit comments

Comments
 (0)