Skip to content

Commit b255669

Browse files
committed
Some GeoJSON fixes
1 parent ae58b92 commit b255669

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

api.php

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4479,7 +4479,7 @@ class Feature implements \JsonSerializable
44794479
private $properties;
44804480
private $geometry;
44814481

4482-
public function __construct(array $properties, Geometry $geometry)
4482+
public function __construct(array $properties, /*?Geometry*/ $geometry)
44834483
{
44844484
$this->properties = $properties;
44854485
$this->geometry = $geometry;
@@ -4506,35 +4506,28 @@ class FeatureCollection implements \JsonSerializable
45064506
{
45074507
private $features;
45084508

4509-
public function __construct(array $features)
4510-
{
4511-
$this->features = $features;
4512-
}
4509+
private $results;
45134510

4514-
public static function fromListDocument(ListDocument $records, string $geometryColumnName): FeatureCollection
4511+
public function __construct(array $features, int $results)
45154512
{
4516-
$features = array();
4517-
foreach ($records->getRecords() as $record) {
4518-
if (isset($record[$geometryColumnName])) {
4519-
$geometry = Geometry::fromWkt($record[$geometryColumnName]);
4520-
unset($record[$geometryColumnName]);
4521-
$features[] = new Feature($record, $geometry);
4522-
}
4523-
}
4524-
return new FeatureCollection($features);
4513+
$this->features = $features;
4514+
$this->results = $results;
45254515
}
45264516

45274517
public function serialize()
45284518
{
45294519
return [
45304520
'type' => 'FeatureCollection',
45314521
'features' => $this->features,
4522+
'results' => $this->results,
45324523
];
45334524
}
45344525

45354526
public function jsonSerialize()
45364527
{
4537-
return $this->serialize();
4528+
return array_filter($this->serialize(), function ($v) {
4529+
return $v !== 0;
4530+
});
45384531
}
45394532
}
45404533

@@ -4556,6 +4549,11 @@ public function hasTable(string $table): bool
45564549
return $this->reflection->hasTable($table);
45574550
}
45584551

4552+
public function getType(string $table): string
4553+
{
4554+
return $this->reflection->getType($table);
4555+
}
4556+
45594557
private function getGeometryColumnName(string $tableName, string $geometryParam): string
45604558
{
45614559
$table = $this->reflection->getTable($tableName);
@@ -4571,17 +4569,35 @@ private function getGeometryColumnName(string $tableName, string $geometryParam)
45714569
return "";
45724570
}
45734571

4572+
private function convertRecordToFeature( /*object*/$record, string $geometryColumnName)
4573+
{
4574+
$geometry = null;
4575+
if (isset($record[$geometryColumnName])) {
4576+
$geometry = Geometry::fromWkt($record[$geometryColumnName]);
4577+
}
4578+
$properties = array_diff_key($record, [$geometryColumnName => true]);
4579+
return new Feature($properties, $geometry);
4580+
}
4581+
45744582
public function _list(string $tableName, array $params): FeatureCollection
45754583
{
4576-
$geometryParam = isset($params['geometry']) ? $params['geometry'] : '';
4584+
$geometryParam = isset($params['geometry']) ? $params['geometry'][0] : '';
45774585
$geometryColumnName = $this->getGeometryColumnName($tableName, $geometryParam);
45784586
$records = $this->records->_list($tableName, $params);
4579-
return FeatureCollection::fromListDocument($records, $geometryColumnName);
4587+
4588+
$features = array();
4589+
foreach ($records->getRecords() as $record) {
4590+
$features[] = $this->convertRecordToFeature($record, $geometryColumnName);
4591+
}
4592+
return new FeatureCollection($features, $records->getResults());
45804593
}
45814594

4582-
public function read(string $tableName, string $id, array $params) /*: ?object*/
4595+
public function read(string $tableName, string $id, array $params): Feature
45834596
{
4584-
return $this->records->read($tableName, $id, $params);
4597+
$geometryParam = isset($params['geometry']) ? $params['geometry'][0] : '';
4598+
$geometryColumnName = $this->getGeometryColumnName($tableName, $geometryParam);
4599+
$record = $this->records->read($tableName, $id, $params);
4600+
return $this->convertRecordToFeature($record, $geometryColumnName);
45854601
}
45864602
}
45874603

@@ -4599,7 +4615,6 @@ class Geometry implements \JsonSerializable
45994615
"MultiLineString",
46004616
"Polygon",
46014617
"MultiPolygon",
4602-
"GeometryCollection",
46034618
];
46044619

46054620
public function __construct(string $type, array $coordinates)
@@ -4612,17 +4627,25 @@ public static function fromWkt(string $wkt): Geometry
46124627
{
46134628
$bracket = strpos($wkt, '(');
46144629
$type = strtoupper(trim(substr($wkt, 0, $bracket)));
4630+
$supported = false;
46154631
foreach (Geometry::$types as $typeName) {
46164632
if (strtoupper($typeName) == $type) {
46174633
$type = $typeName;
4634+
$supported = true;
46184635
}
46194636
}
4637+
if (!$supported) {
4638+
throw new \Exception('Geometry type not supported: ' . $type);
4639+
}
46204640
$coordinates = substr($wkt, $bracket);
4621-
$coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
4622-
$coordinates = str_replace(['(', ')', ' '], ['[', ']', ','], $coordinates);
4641+
if (substr($type, -5) != 'Point' || ($type == 'MultiPoint' && $coordinates[1] != '(')) {
4642+
$coordinates = preg_replace('|([0-9\-\.]+ )+([0-9\-\.]+)|', '[\1\2]', $coordinates);
4643+
}
4644+
$coordinates = str_replace(['(', ')', ', ', ' '], ['[', ']', ',', ','], $coordinates);
4645+
$json = $coordinates;
46234646
$coordinates = json_decode($coordinates);
4624-
if ($type == 'Point') {
4625-
$coordinates = $coordinates[0];
4647+
if (!$coordinates) {
4648+
throw new \Exception('Could not decode WKT: ' . $wkt);
46264649
}
46274650
return new Geometry($type, $coordinates);
46284651
}

0 commit comments

Comments
 (0)