浏览代码

Some GeoJSON fixes

Maurits van der Schee 5 年前
父节点
当前提交
aaabfc86bb

+ 7
- 3
src/Tqdev/PhpCrudApi/GeoJson/Geometry.php 查看文件

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

+ 2
- 1
tests/fixtures/blog_mysql.sql 查看文件

@@ -114,7 +114,8 @@ INSERT INTO `countries` (`name`, `shape`) VALUES
114 114
 ('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
115 115
 ('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
116 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 120
 DROP TABLE IF EXISTS `events`;
120 121
 CREATE TABLE `events` (

+ 2
- 1
tests/fixtures/blog_pgsql.sql 查看文件

@@ -248,7 +248,8 @@ INSERT INTO "countries" ("name", "shape") VALUES
248 248
 ('Mpoint',	ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')),
249 249
 ('Mline',	ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')),
250 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 255
 -- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres

+ 2
- 0
tests/fixtures/blog_sqlsrv.sql 查看文件

@@ -360,6 +360,8 @@ INSERT [countries] ([name], [shape]) VALUES (N'Mpoly1', N'MULTIPOLYGON (((30 20,
360 360
 GO
361 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 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 366
 INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)
365 367
 GO

+ 8
- 0
tests/functional/001_records/081_read_countries_as_geojson.log 查看文件

@@ -61,3 +61,11 @@ Content-Type: application/json
61 61
 Content-Length: 229
62 62
 
63 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"}

正在加载...
取消
保存