Browse Source

Some GeoJSON fixes

Maurits van der Schee 5 years ago
parent
commit
aaabfc86bb

+ 7
- 3
src/Tqdev/PhpCrudApi/GeoJson/Geometry.php View File

13
         "MultiLineString",
13
         "MultiLineString",
14
         "Polygon",
14
         "Polygon",
15
         "MultiPolygon",
15
         "MultiPolygon",
16
-        "GeometryCollection",
16
+        //"GeometryCollection",
17
     ];
17
     ];
18
 
18
 
19
     public function __construct(string $type, array $coordinates)
19
     public function __construct(string $type, array $coordinates)
26
     {
26
     {
27
         $bracket = strpos($wkt, '(');
27
         $bracket = strpos($wkt, '(');
28
         $type = strtoupper(trim(substr($wkt, 0, $bracket)));
28
         $type = strtoupper(trim(substr($wkt, 0, $bracket)));
29
+        $supported = false;
29
         foreach (Geometry::$types as $typeName) {
30
         foreach (Geometry::$types as $typeName) {
30
             if (strtoupper($typeName) == $type) {
31
             if (strtoupper($typeName) == $type) {
31
                 $type = $typeName;
32
                 $type = $typeName;
33
+                $supported = true;
32
             }
34
             }
33
         }
35
         }
36
+        if (!$supported) {
37
+            throw new \Exception('Geometry type not supported: ' . $type);
38
+        }
34
         $coordinates = substr($wkt, $bracket);
39
         $coordinates = substr($wkt, $bracket);
35
         if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
40
         if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
36
             $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
41
             $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
39
         $json = $coordinates;
44
         $json = $coordinates;
40
         $coordinates = json_decode($coordinates);
45
         $coordinates = json_decode($coordinates);
41
         if (!$coordinates) {
46
         if (!$coordinates) {
42
-            echo $json;
47
+            throw new \Exception('Could not decode WKT: ' . $wkt);
43
         }
48
         }
44
-
45
         return new Geometry($type, $coordinates);
49
         return new Geometry($type, $coordinates);
46
     }
50
     }
47
 
51
 

+ 2
- 1
tests/fixtures/blog_mysql.sql View File

114
 ('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
114
 ('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
115
 ('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
115
 ('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
116
 ('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')),
116
 ('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))'));
118
 
119
 
119
 DROP TABLE IF EXISTS `events`;
120
 DROP TABLE IF EXISTS `events`;
120
 CREATE TABLE `events` (
121
 CREATE TABLE `events` (

+ 2
- 1
tests/fixtures/blog_pgsql.sql View File

248
 ('Mpoint',	ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
248
 ('Mpoint',	ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
249
 ('Mline',	ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
249
 ('Mline',	ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
250
 ('Mpoly1',	ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')),
250
 ('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))'));
252
 
253
 
253
 --
254
 --
254
 -- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres
255
 -- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres

+ 2
- 0
tests/fixtures/blog_sqlsrv.sql View File

360
 GO
360
 GO
361
 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)))')
361
 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)))')
362
 GO
362
 GO
363
+INSERT [countries] ([name], [shape]) VALUES (N'Gcoll', N'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))')
364
+GO
363
 
365
 
364
 INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)
366
 INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)
365
 GO
367
 GO

+ 8
- 0
tests/functional/001_records/081_read_countries_as_geojson.log View File

61
 Content-Length: 229
61
 Content-Length: 229
62
 
62
 
63
 {"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]]]]}}
63
 {"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"}

Loading…
Cancel
Save