Browse Source

Some GeoJSON fixes

Maurits van der Schee 5 years ago
parent
commit
93a3821387

+ 0
- 15
src/Tqdev/PhpCrudApi/GeoJson/FeatureCollection.php View File

@@ -1,8 +1,6 @@
1 1
 <?php
2 2
 namespace Tqdev\PhpCrudApi\GeoJson;
3 3
 
4
-use Tqdev\PhpCrudApi\Record\Document\ListDocument;
5
-
6 4
 class FeatureCollection implements \JsonSerializable
7 5
 {
8 6
     private $features;
@@ -12,19 +10,6 @@ class FeatureCollection implements \JsonSerializable
12 10
         $this->features = $features;
13 11
     }
14 12
 
15
-    public static function fromListDocument(ListDocument $records, string $geometryColumnName): FeatureCollection
16
-    {
17
-        $features = array();
18
-        foreach ($records->getRecords() as $record) {
19
-            if (isset($record[$geometryColumnName])) {
20
-                $geometry = Geometry::fromWkt($record[$geometryColumnName]);
21
-                unset($record[$geometryColumnName]);
22
-                $features[] = new Feature($record, $geometry);
23
-            }
24
-        }
25
-        return new FeatureCollection($features);
26
-    }
27
-
28 13
     public function serialize()
29 14
     {
30 15
         return [

+ 29
- 3
src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php View File

@@ -21,6 +21,11 @@ class GeoJsonService
21 21
         return $this->reflection->hasTable($table);
22 22
     }
23 23
 
24
+    public function getType(string $table): string
25
+    {
26
+        return $this->reflection->getType($table);
27
+    }
28
+
24 29
     private function getGeometryColumnName(string $tableName, string $geometryParam): string
25 30
     {
26 31
         $table = $this->reflection->getTable($tableName);
@@ -36,16 +41,37 @@ class GeoJsonService
36 41
         return "";
37 42
     }
38 43
 
44
+    private function convertRecordToFeature( /*object*/$record, string $geometryColumnName)
45
+    {
46
+        $geometry = Geometry::fromWkt($record[$geometryColumnName]);
47
+        unset($record[$geometryColumnName]);
48
+        return new Feature($record, $geometry);
49
+    }
50
+
39 51
     public function _list(string $tableName, array $params): FeatureCollection
40 52
     {
41 53
         $geometryParam = isset($params['geometry']) ? $params['geometry'] : '';
42 54
         $geometryColumnName = $this->getGeometryColumnName($tableName, $geometryParam);
43 55
         $records = $this->records->_list($tableName, $params);
44
-        return FeatureCollection::fromListDocument($records, $geometryColumnName);
56
+
57
+        $features = array();
58
+        foreach ($records->getRecords() as $record) {
59
+            if (isset($record[$geometryColumnName])) {
60
+                $features[] = $this->convertRecordToFeature($record, $geometryColumnName);
61
+            }
62
+        }
63
+        return new FeatureCollection($features);
45 64
     }
46 65
 
47
-    public function read(string $tableName, string $id, array $params) /*: ?object*/
66
+    public function read(string $tableName, string $id, array $params) /*: ?Feature*/
48 67
     {
49
-        return $this->records->read($tableName, $id, $params);
68
+        $geometryParam = isset($params['geometry']) ? $params['geometry'] : '';
69
+        $geometryColumnName = $this->getGeometryColumnName($tableName, $geometryParam);
70
+        $record = $this->records->read($tableName, $id, $params);
71
+        if (!isset($record[$geometryColumnName])) {
72
+            print_r($record);
73
+            return null;
74
+        }
75
+        return $this->convertRecordToFeature($record, $geometryColumnName);
50 76
     }
51 77
 }

+ 8
- 4
src/Tqdev/PhpCrudApi/GeoJson/Geometry.php View File

@@ -32,12 +32,16 @@ class Geometry implements \JsonSerializable
32 32
             }
33 33
         }
34 34
         $coordinates = substr($wkt, $bracket);
35
-        $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
36
-        $coordinates = str_replace(['(', ')', ' '], ['[', ']', ','], $coordinates);
35
+        if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
36
+            $coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
37
+        }
38
+        $coordinates = str_replace(['(', ')', ', ', ' '], ['[', ']', ',', ','], $coordinates);
39
+        $json = $coordinates;
37 40
         $coordinates = json_decode($coordinates);
38
-        if ($type == 'Point') {
39
-            $coordinates = $coordinates[0];
41
+        if (!$coordinates) {
42
+            echo $json;
40 43
         }
44
+
41 45
         return new Geometry($type, $coordinates);
42 46
     }
43 47
 

+ 1
- 1
tests/config/base.php View File

@@ -3,7 +3,7 @@ $settings = [
3 3
     'database' => 'php-crud-api',
4 4
     'username' => 'php-crud-api',
5 5
     'password' => 'php-crud-api',
6
-    'controllers' => 'records,columns,cache,openapi',
6
+    'controllers' => 'records,columns,cache,openapi,geojson',
7 7
     'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,ipAddress,sanitation,multiTenancy,pageLimits,joinLimits,customization',
8 8
     'jwtAuth.mode' => 'optional',
9 9
     'jwtAuth.time' => '1538207605',

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

@@ -100,13 +100,21 @@ DROP TABLE IF EXISTS `countries`;
100 100
 CREATE TABLE `countries` (
101 101
   `id` int(11) NOT NULL AUTO_INCREMENT,
102 102
   `name` varchar(255) NOT NULL,
103
-  `shape` polygon NOT NULL,
103
+  `shape` geometry NOT NULL,
104 104
   PRIMARY KEY (`id`)
105 105
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
106 106
 
107 107
 INSERT INTO `countries` (`name`, `shape`) VALUES
108 108
 ('Left',	ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')),
109
-('Right',	ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))'));
109
+('Right',	ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')),
110
+('Point', ST_GeomFromText('POINT (30 10)')),
111
+('Line', ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)')),
112
+('Poly1', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')),
113
+('Poly2', ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))')),
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))')),
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)))'));
110 118
 
111 119
 DROP TABLE IF EXISTS `events`;
112 120
 CREATE TABLE `events` (

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

@@ -240,7 +240,15 @@ INSERT INTO "users" ("username", "password", "location") VALUES
240 240
 
241 241
 INSERT INTO "countries" ("name", "shape") VALUES
242 242
 ('Left',	ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')),
243
-('Right',	ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))'));
243
+('Right',	ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')),
244
+('Point',	ST_GeomFromText('POINT (30 10)')),
245
+('Line',	ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)')),
246
+('Poly1',	ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')),
247
+('Poly2',	ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))')),
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))')),
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)))'));
244 252
 
245 253
 --
246 254
 -- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres

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

@@ -344,6 +344,22 @@ INSERT [countries] ([name], [shape]) VALUES (N'Left', N'POLYGON ((30 10, 40 40,
344 344
 GO
345 345
 INSERT [countries] ([name], [shape]) VALUES (N'Right', N'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')
346 346
 GO
347
+INSERT [countries] ([name], [shape]) VALUES (N'Point', N'POINT (30 10)')
348
+GO
349
+INSERT [countries] ([name], [shape]) VALUES (N'Line', N'LINESTRING (30 10, 10 30, 40 40)')
350
+GO
351
+INSERT [countries] ([name], [shape]) VALUES (N'Poly1', N'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')
352
+GO
353
+INSERT [countries] ([name], [shape]) VALUES (N'Poly2', N'POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))')
354
+GO
355
+INSERT [countries] ([name], [shape]) VALUES (N'Mpoint', N'MULTIPOINT (10 40, 40 30, 20 20, 30 10)')
356
+GO
357
+INSERT [countries] ([name], [shape]) VALUES (N'Mline', N'MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')
358
+GO
359
+INSERT [countries] ([name], [shape]) VALUES (N'Mpoly1', N'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')
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)))')
362
+GO
347 363
 
348 364
 INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)
349 365
 GO

Loading…
Cancel
Save