From 995626b1830f64902a8fa343c3626ed8bd7bf2a2 Mon Sep 17 00:00:00 2001 From: Yohann Berthon Date: Mon, 10 Feb 2025 16:03:14 +0100 Subject: [PATCH 1/2] Improve locality for city layer & Add layer & radius query params --- src/Provider/Photon/Model/PhotonAddress.php | 28 +++++++++++++++++++ src/Provider/Photon/Photon.php | 6 +++- ...o_1452fda1b72428b0f5de90de04038275fd7f7e7e | 1 + src/Provider/Photon/Tests/PhotonTest.php | 14 ++++++++++ 4 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_1452fda1b72428b0f5de90de04038275fd7f7e7e diff --git a/src/Provider/Photon/Model/PhotonAddress.php b/src/Provider/Photon/Model/PhotonAddress.php index 033008849..654d66b94 100644 --- a/src/Provider/Photon/Model/PhotonAddress.php +++ b/src/Provider/Photon/Model/PhotonAddress.php @@ -54,6 +54,21 @@ final class PhotonAddress extends Address */ private $district; + /** + * @var string|null + */ + private $type; + + public function getLocality(): ?string + { + $locality = parent::getLocality(); + if ($locality === null && $this->type === 'city' && $this->name !== null) { + $locality = $this->name; + } + + return $locality; + } + /** * @return string|null */ @@ -173,4 +188,17 @@ public function withDistrict(?string $district = null): self return $new; } + + public function getType(): ?string + { + return $this->type; + } + + public function withType(?string $type = null): self + { + $new = clone $this; + $new->type = $type; + + return $new; + } } diff --git a/src/Provider/Photon/Photon.php b/src/Provider/Photon/Photon.php index 564b8a36f..caed4ecab 100644 --- a/src/Provider/Photon/Photon.php +++ b/src/Provider/Photon/Photon.php @@ -68,6 +68,7 @@ public function geocodeQuery(GeocodeQuery $query): Collection .'/api?' .http_build_query([ 'q' => $address, + 'layer' => $query->getData('layer'), 'limit' => $query->getLimit(), 'lang' => $query->getLocale(), ]); @@ -102,6 +103,8 @@ public function reverseQuery(ReverseQuery $query): Collection .http_build_query([ 'lat' => $latitude, 'lon' => $longitude, + 'layer' => $query->getData('layer'), + 'radius' => $query->getData('radius'), 'limit' => $query->getLimit(), 'lang' => $query->getLocale(), ]); @@ -157,7 +160,8 @@ private function featureToAddress(\stdClass $feature): Location ->withName($properties->name ?? null) ->withState($properties->state ?? null) ->withCounty($properties->county ?? null) - ->withDistrict($properties->district ?? null); + ->withDistrict($properties->district ?? null) + ->withType($properties->type ?? null); return $address; } diff --git a/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_1452fda1b72428b0f5de90de04038275fd7f7e7e b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_1452fda1b72428b0f5de90de04038275fd7f7e7e new file mode 100644 index 000000000..bd0964e26 --- /dev/null +++ b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_1452fda1b72428b0f5de90de04038275fd7f7e7e @@ -0,0 +1 @@ +s:334:"{"features":[{"geometry":{"coordinates":[13.3989367,52.510885],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":62422,"extent":[13.088345,52.6755087,13.7611609,52.3382448],"country":"Deutschland","osm_key":"place","countrycode":"DE","osm_value":"city","name":"Berlin","type":"city"}}],"type":"FeatureCollection"}"; \ No newline at end of file diff --git a/src/Provider/Photon/Tests/PhotonTest.php b/src/Provider/Photon/Tests/PhotonTest.php index 1148341d7..2ae88c655 100644 --- a/src/Provider/Photon/Tests/PhotonTest.php +++ b/src/Provider/Photon/Tests/PhotonTest.php @@ -178,4 +178,18 @@ public function testReverseQueryWithOsmTagFilter(): void $this->assertEquals('pharmacy', $result->getOSMTag()->value); } } + + public function testReverseQueryWithLayerCityAndRadiusFilter(): void + { + $provider = Photon::withKomootServer($this->getHttpClient()); + $reverseQuery = ReverseQuery::fromCoordinates(52.51644, 13.38890) + ->withData('layer', 'city') + ->withData('radius', 20) + ->withLimit(1); + $result = $provider->reverseQuery($reverseQuery)->first(); + + $this->assertInstanceOf(PhotonAddress::class, $result); + $this->assertEquals('city', $result->getType()); + $this->assertEquals('Berlin', $result->getLocality()); + } } From 1fa5246599c26d5f8c5e79d726cc2dc2d59eadfb Mon Sep 17 00:00:00 2001 From: Yohann Berthon Date: Mon, 10 Feb 2025 16:19:11 +0100 Subject: [PATCH 2/2] CS fix --- src/Provider/Photon/Model/PhotonAddress.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Provider/Photon/Model/PhotonAddress.php b/src/Provider/Photon/Model/PhotonAddress.php index 654d66b94..049b2c706 100644 --- a/src/Provider/Photon/Model/PhotonAddress.php +++ b/src/Provider/Photon/Model/PhotonAddress.php @@ -62,7 +62,7 @@ final class PhotonAddress extends Address public function getLocality(): ?string { $locality = parent::getLocality(); - if ($locality === null && $this->type === 'city' && $this->name !== null) { + if (null === $locality && 'city' === $this->type && null !== $this->name) { $locality = $this->name; }