diff --git a/src/Provider/Photon/Photon.php b/src/Provider/Photon/Photon.php index 1da16efb6..c8efa767f 100644 --- a/src/Provider/Photon/Photon.php +++ b/src/Provider/Photon/Photon.php @@ -78,6 +78,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection if (!empty($osmTagFilters)) { $url .= $osmTagFilters; } + $bboxQueryString = $this->buildBboxFilterQuery($query); + if (!is_null($bboxQueryString)) { + $url .= $bboxQueryString; + } $json = $this->executeQuery($url); @@ -192,6 +196,20 @@ private function buildOsmTagFilterQuery($filters): string return $query; } + private function buildBboxFilterQuery(GeocodeQuery $query): ?string + { + if (null === $query->getBounds()) { + return null; + } + + return '&bbox='.sprintf('%f,%f,%f,%f', + $query->getBounds()->getWest(), + $query->getBounds()->getSouth(), + $query->getBounds()->getEast(), + $query->getBounds()->getNorth() + ); + } + private function executeQuery(string $url): \stdClass { $content = $this->getUrlContents($url); diff --git a/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_ab0a3e352306e701cef79478d26bdd56f4598e81 b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_ab0a3e352306e701cef79478d26bdd56f4598e81 new file mode 100644 index 000000000..edcf21a38 --- /dev/null +++ b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_ab0a3e352306e701cef79478d26bdd56f4598e81 @@ -0,0 +1 @@ +s:2001:"{"features":[{"geometry":{"coordinates":[2.3200410217200766,48.8588897],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":7444,"extent":[2.224122,48.902156,2.4697602,48.8155755],"country":"France","osm_key":"boundary","city":"Paris","countrycode":"FR","osm_value":"administrative","postcode":"75000;75001;75002;75003;75004;75005;75006;75007;75008;75009;75010;75011;75012;75013;75014;75015;75016;75017;75018;75019;75020;75116","name":"Paris","state":"Île-de-France","type":"district"}},{"geometry":{"coordinates":[2.3483915,48.8534951],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":71525,"extent":[2.224122,48.902156,2.4697602,48.8155755],"country":"France","osm_key":"place","countrycode":"FR","osm_value":"city","name":"Paris","state":"Île-de-France","type":"city"}},{"geometry":{"coordinates":[2.3200410217200766,48.8588897],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":1641193,"extent":[2.224122,48.902156,2.4697602,48.8155755],"country":"France","osm_key":"boundary","city":"Paris","countrycode":"FR","osm_value":"administrative","name":"Paris","state":"Île-de-France","type":"district"}},{"geometry":{"coordinates":[-95.555513,33.6617962],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":115357,"extent":[-95.6279396,33.7383866,-95.4354115,33.6206345],"country":"United States","osm_key":"place","countrycode":"US","osm_value":"town","name":"Paris","county":"Lamar","state":"Texas","type":"city"}},{"geometry":{"coordinates":[2.3365253984179155,48.8365091],"type":"Point"},"type":"Feature","properties":{"osm_id":79611305,"extent":[2.3358691,48.8366578,2.3371706,48.836243],"country":"France","city":"Paris","countrycode":"FR","postcode":"75014","locality":"Quartier du Montparnasse","type":"house","osm_type":"W","osm_key":"building","street":"Avenue de l'Observatoire","district":"Paris","osm_value":"historic","name":"Observatoire de Paris","state":"Île-de-France"}}],"type":"FeatureCollection"}"; \ No newline at end of file diff --git a/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_efc348818ff5a7d34fc7305bb97f5738b2ea8f8d b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_efc348818ff5a7d34fc7305bb97f5738b2ea8f8d new file mode 100644 index 000000000..bc6d78d4c --- /dev/null +++ b/src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_efc348818ff5a7d34fc7305bb97f5738b2ea8f8d @@ -0,0 +1 @@ +s:813:"{"features":[{"geometry":{"coordinates":[8.1147545,49.833289],"type":"Point"},"type":"Feature","properties":{"osm_id":1310664730,"extent":[8.1147525,49.8336895,8.1147673,49.8331048],"country":"Deutschland","city":"Wörrstadt","countrycode":"DE","postcode":"55286","county":"Alzey-Worms","type":"street","osm_type":"W","osm_key":"highway","osm_value":"primary","name":"Pariser Straße","state":"Rheinland-Pfalz"}},{"geometry":{"coordinates":[13.378690821250334,52.51635135],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":181198,"extent":[13.3777517,52.5169588,13.3798039,52.5157489],"country":"Deutschland","osm_key":"place","city":"Berlin","countrycode":"DE","district":"Mitte","osm_value":"square","postcode":"10117","name":"Pariser Platz","type":"locality"}}],"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 721df89da..051a8cb41 100644 --- a/src/Provider/Photon/Tests/PhotonTest.php +++ b/src/Provider/Photon/Tests/PhotonTest.php @@ -212,4 +212,33 @@ public function testReverseQueryWithLayerCityAndRadiusFilter(): void $this->assertEquals('city', $result->getType()); $this->assertEquals('Berlin', $result->getLocality()); } + + public function testGeocodeQueryWithBbox(): void + { + // Germany + $bounds = new \Geocoder\Model\Bounds( + south: 47.2701, + west: 5.8663, + north: 55.992, + east: 15.0419 + ); + + $provider = Photon::withKomootServer($this->getHttpClient()); + $query = GeocodeQuery::create('Paris') + ->withLimit(5); + $results = $provider->geocodeQuery($query); + + $this->assertCount(5, $results); + $this->assertEquals('France', $results->first()->getCountry()); + $this->assertEquals('Paris', $results->first()->getLocality()); + + $query = GeocodeQuery::create('Paris') + ->withBounds($bounds) + ->withLimit(5); + $results = $provider->geocodeQuery($query); + + $this->assertCount(2, $results); + $this->assertEquals('Deutschland', $results->first()->getCountry()); + $this->assertEquals('Wörrstadt', $results->first()->getLocality()); + } }