@@ -4477,22 +4477,25 @@ public function fromJdbc(string $type): string
44774477
44784478class 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
0 commit comments