Skip to content

Commit 573be5f

Browse files
committed
support tiles in GeoJSON
1 parent 9085280 commit 573be5f

File tree

3 files changed

+88
-34
lines changed

3 files changed

+88
-34
lines changed

api.php

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,22 +4477,25 @@ public function fromJdbc(string $type): string
44774477

44784478
class Feature implements \JsonSerializable
44794479
{
4480+
private $id;
44804481
private $properties;
44814482
private $geometry;
44824483

4483-
public function __construct(array $properties, /*?Geometry*/ $geometry)
4484+
public function __construct(string $id, array $properties, /*?Geometry*/ $geometry)
44844485
{
4486+
$this->id = $id;
44854487
$this->properties = $properties;
44864488
$this->geometry = $geometry;
44874489
}
44884490

44894491
public function serialize()
44904492
{
4491-
return [
4493+
return array_filter([
44924494
'type' => 'Feature',
4495+
'id' => $this->id,
44934496
'properties' => $this->properties,
44944497
'geometry' => $this->geometry,
4495-
];
4498+
]);
44964499
}
44974500

44984501
public function jsonSerialize()
@@ -4586,38 +4589,61 @@ private function setBoudingBoxFilter(string $geometryColumnName, array &$params)
45864589
}
45874590
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
45884591
}
4589-
/*
45904592
$tile = isset($params['tile']) ? $params['tile'][0] : '';
45914593
if ($tile) {
4592-
4593-
$n = pow(2, $zoom);
4594-
$lon_deg = $xtile / $n * 360.0 - 180.0;
4595-
$lat_deg = rad2deg(atan(sinh(pi() * (1 - 2 * $ytile / $n))));
4596-
4597-
calculates upperleft corner
4594+
$zxy = explode(',', $tile);
4595+
if (count($zxy) == 3) {
4596+
list($z, $x, $y) = $zxy;
4597+
$c = array();
4598+
$c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x, $y));
4599+
$c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x + 1, $y + 1));
4600+
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
4601+
}
4602+
}
4603+
}
45984604

4599-
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
4600-
}*/
4605+
private function convertTileToLatLonOfUpperLeftCorner($z, $x, $y): array
4606+
{
4607+
$n = pow(2, $z);
4608+
$lon = $x / $n * 360.0 - 180.0;
4609+
$lat = rad2deg(atan(sinh(pi() * (1 - 2 * $y / $n))));
4610+
return [$lon, $lat];
46014611
}
46024612

4603-
private function convertRecordToFeature( /*object*/$record, string $geometryColumnName)
4613+
private function convertRecordToFeature( /*object*/$record, string $primaryKeyColumnName, string $geometryColumnName)
46044614
{
4615+
$id = '';
4616+
if ($primaryKeyColumnName) {
4617+
$id = $record[$primaryKeyColumnName];
4618+
}
46054619
$geometry = null;
46064620
if (isset($record[$geometryColumnName])) {
46074621
$geometry = Geometry::fromWkt($record[$geometryColumnName]);
46084622
}
4609-
$properties = array_diff_key($record, [$geometryColumnName => true]);
4610-
return new Feature($properties, $geometry);
4623+
$properties = array_diff_key($record, [$primaryKeyColumnName => true, $geometryColumnName => true]);
4624+
return new Feature($id, $properties, $geometry);
4625+
}
4626+
4627+
private function getPrimaryKeyColumnName(string $tableName, array &$params): string
4628+
{
4629+
$primaryKeyColumn = $this->reflection->getTable($tableName)->getPk();
4630+
if (!$primaryKeyColumn) {
4631+
return '';
4632+
}
4633+
$primaryKeyColumnName = $primaryKeyColumn->getName();
4634+
$params['mandatory'][] = $tableName . "." . $primaryKeyColumnName;
4635+
return $primaryKeyColumnName;
46114636
}
46124637

46134638
public function _list(string $tableName, array $params): FeatureCollection
46144639
{
46154640
$geometryColumnName = $this->getGeometryColumnName($tableName, $params);
46164641
$this->setBoudingBoxFilter($geometryColumnName, $params);
4642+
$primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
46174643
$records = $this->records->_list($tableName, $params);
46184644
$features = array();
46194645
foreach ($records->getRecords() as $record) {
4620-
$features[] = $this->convertRecordToFeature($record, $geometryColumnName);
4646+
$features[] = $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
46214647
}
46224648
return new FeatureCollection($features, $records->getResults());
46234649
}
@@ -4626,8 +4652,9 @@ public function read(string $tableName, string $id, array $params): Feature
46264652
{
46274653
$geometryColumnName = $this->getGeometryColumnName($tableName, $params);
46284654
$this->setBoudingBoxFilter($geometryColumnName, $params);
4655+
$primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
46294656
$record = $this->records->read($tableName, $id, $params);
4630-
return $this->convertRecordToFeature($record, $geometryColumnName);
4657+
return $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
46314658
}
46324659
}
46334660

src/Tqdev/PhpCrudApi/GeoJson/Feature.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,25 @@
33

44
class Feature implements \JsonSerializable
55
{
6+
private $id;
67
private $properties;
78
private $geometry;
89

9-
public function __construct(array $properties, /*?Geometry*/ $geometry)
10+
public function __construct(string $id, array $properties, /*?Geometry*/ $geometry)
1011
{
12+
$this->id = $id;
1113
$this->properties = $properties;
1214
$this->geometry = $geometry;
1315
}
1416

1517
public function serialize()
1618
{
17-
return [
19+
return array_filter([
1820
'type' => 'Feature',
21+
'id' => $this->id,
1922
'properties' => $this->properties,
2023
'geometry' => $this->geometry,
21-
];
24+
]);
2225
}
2326

2427
public function jsonSerialize()

src/Tqdev/PhpCrudApi/GeoJson/GeoJsonService.php

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,61 @@ private function setBoudingBoxFilter(string $geometryColumnName, array &$params)
5757
}
5858
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
5959
}
60-
/*
6160
$tile = isset($params['tile']) ? $params['tile'][0] : '';
6261
if ($tile) {
63-
64-
$n = pow(2, $zoom);
65-
$lon_deg = $xtile / $n * 360.0 - 180.0;
66-
$lat_deg = rad2deg(atan(sinh(pi() * (1 - 2 * $ytile / $n))));
67-
68-
calculates upperleft corner
62+
$zxy = explode(',', $tile);
63+
if (count($zxy) == 3) {
64+
list($z, $x, $y) = $zxy;
65+
$c = array();
66+
$c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x, $y));
67+
$c = array_merge($c, $this->convertTileToLatLonOfUpperLeftCorner($z, $x + 1, $y + 1));
68+
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
69+
}
70+
}
71+
}
6972

70-
$params['filter'][] = "$geometryColumnName,sin,POLYGON(($c[0] $c[1],$c[2] $c[1],$c[2] $c[3],$c[0] $c[3],$c[0] $c[1]))";
71-
}*/
73+
private function convertTileToLatLonOfUpperLeftCorner($z, $x, $y): array
74+
{
75+
$n = pow(2, $z);
76+
$lon = $x / $n * 360.0 - 180.0;
77+
$lat = rad2deg(atan(sinh(pi() * (1 - 2 * $y / $n))));
78+
return [$lon, $lat];
7279
}
7380

74-
private function convertRecordToFeature( /*object*/$record, string $geometryColumnName)
81+
private function convertRecordToFeature( /*object*/$record, string $primaryKeyColumnName, string $geometryColumnName)
7582
{
83+
$id = '';
84+
if ($primaryKeyColumnName) {
85+
$id = $record[$primaryKeyColumnName];
86+
}
7687
$geometry = null;
7788
if (isset($record[$geometryColumnName])) {
7889
$geometry = Geometry::fromWkt($record[$geometryColumnName]);
7990
}
80-
$properties = array_diff_key($record, [$geometryColumnName => true]);
81-
return new Feature($properties, $geometry);
91+
$properties = array_diff_key($record, [$primaryKeyColumnName => true, $geometryColumnName => true]);
92+
return new Feature($id, $properties, $geometry);
93+
}
94+
95+
private function getPrimaryKeyColumnName(string $tableName, array &$params): string
96+
{
97+
$primaryKeyColumn = $this->reflection->getTable($tableName)->getPk();
98+
if (!$primaryKeyColumn) {
99+
return '';
100+
}
101+
$primaryKeyColumnName = $primaryKeyColumn->getName();
102+
$params['mandatory'][] = $tableName . "." . $primaryKeyColumnName;
103+
return $primaryKeyColumnName;
82104
}
83105

84106
public function _list(string $tableName, array $params): FeatureCollection
85107
{
86108
$geometryColumnName = $this->getGeometryColumnName($tableName, $params);
87109
$this->setBoudingBoxFilter($geometryColumnName, $params);
110+
$primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
88111
$records = $this->records->_list($tableName, $params);
89112
$features = array();
90113
foreach ($records->getRecords() as $record) {
91-
$features[] = $this->convertRecordToFeature($record, $geometryColumnName);
114+
$features[] = $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
92115
}
93116
return new FeatureCollection($features, $records->getResults());
94117
}
@@ -97,7 +120,8 @@ public function read(string $tableName, string $id, array $params): Feature
97120
{
98121
$geometryColumnName = $this->getGeometryColumnName($tableName, $params);
99122
$this->setBoudingBoxFilter($geometryColumnName, $params);
123+
$primaryKeyColumnName = $this->getPrimaryKeyColumnName($tableName, $params);
100124
$record = $this->records->read($tableName, $id, $params);
101-
return $this->convertRecordToFeature($record, $geometryColumnName);
125+
return $this->convertRecordToFeature($record, $primaryKeyColumnName, $geometryColumnName);
102126
}
103127
}

0 commit comments

Comments
 (0)