Skip to content

Commit 93a3821

Browse files
committed
Some GeoJSON fixes
1 parent c61c1ae commit 93a3821

File tree

7 files changed

+73
-26
lines changed

7 files changed

+73
-26
lines changed

src/Tqdev/PhpCrudApi/GeoJson/FeatureCollection.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php
22
namespace Tqdev\PhpCrudApi\GeoJson;
33

4-
use Tqdev\PhpCrudApi\Record\Document\ListDocument;
5-
64
class FeatureCollection implements \JsonSerializable
75
{
86
private $features;
@@ -12,19 +10,6 @@ public function __construct(array $features)
1210
$this->features = $features;
1311
}
1412

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-
2813
public function serialize()
2914
{
3015
return [

src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function hasTable(string $table): bool
2121
return $this->reflection->hasTable($table);
2222
}
2323

24+
public function getType(string $table): string
25+
{
26+
return $this->reflection->getType($table);
27+
}
28+
2429
private function getGeometryColumnName(string $tableName, string $geometryParam): string
2530
{
2631
$table = $this->reflection->getTable($tableName);
@@ -36,16 +41,37 @@ private function getGeometryColumnName(string $tableName, string $geometryParam)
3641
return "";
3742
}
3843

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+
3951
public function _list(string $tableName, array $params): FeatureCollection
4052
{
4153
$geometryParam = isset($params['geometry']) ? $params['geometry'] : '';
4254
$geometryColumnName = $this->getGeometryColumnName($tableName, $geometryParam);
4355
$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);
4564
}
4665

47-
public function read(string $tableName, string $id, array $params) /*: ?object*/
66+
public function read(string $tableName, string $id, array $params) /*: ?Feature*/
4867
{
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);
5076
}
5177
}

src/Tqdev/PhpCrudApi/GeoJson/Geometry.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,16 @@ public static function fromWkt(string $wkt): Geometry
3232
}
3333
}
3434
$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;
3740
$coordinates = json_decode($coordinates);
38-
if ($type == 'Point') {
39-
$coordinates = $coordinates[0];
41+
if (!$coordinates) {
42+
echo $json;
4043
}
44+
4145
return new Geometry($type, $coordinates);
4246
}
4347

tests/config/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
'database' => 'php-crud-api',
44
'username' => 'php-crud-api',
55
'password' => 'php-crud-api',
6-
'controllers' => 'records,columns,cache,openapi',
6+
'controllers' => 'records,columns,cache,openapi,geojson',
77
'middlewares' => 'cors,jwtAuth,basicAuth,authorization,validation,ipAddress,sanitation,multiTenancy,pageLimits,joinLimits,customization',
88
'jwtAuth.mode' => 'optional',
99
'jwtAuth.time' => '1538207605',

tests/fixtures/blog_mysql.sql

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,21 @@ DROP TABLE IF EXISTS `countries`;
100100
CREATE TABLE `countries` (
101101
`id` int(11) NOT NULL AUTO_INCREMENT,
102102
`name` varchar(255) NOT NULL,
103-
`shape` polygon NOT NULL,
103+
`shape` geometry NOT NULL,
104104
PRIMARY KEY (`id`)
105105
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
106106

107107
INSERT INTO `countries` (`name`, `shape`) VALUES
108108
('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)))'));
110118

111119
DROP TABLE IF EXISTS `events`;
112120
CREATE TABLE `events` (

tests/fixtures/blog_pgsql.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,15 @@ INSERT INTO "users" ("username", "password", "location") VALUES
240240

241241
INSERT INTO "countries" ("name", "shape") VALUES
242242
('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)))'));
244252

245253
--
246254
-- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres

tests/fixtures/blog_sqlsrv.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,22 @@ INSERT [countries] ([name], [shape]) VALUES (N'Left', N'POLYGON ((30 10, 40 40,
344344
GO
345345
INSERT [countries] ([name], [shape]) VALUES (N'Right', N'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')
346346
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
347363

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

0 commit comments

Comments
 (0)