Skip to content

[Photon] Support bbox in geocode query #1247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/Provider/Photon/Photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection
if (!empty($osmTagFilters)) {
$url .= $osmTagFilters;
}
$bboxQueryString = $this->buildBboxFilterQuery($query);
if ($bboxQueryString) {
$url .= $bboxQueryString;
}

$json = $this->executeQuery($url);

Expand Down Expand Up @@ -190,6 +194,20 @@ private function buildOsmTagFilterQuery($filters): string
return $query;
}

private function buildBboxFilterQuery(GeocodeQuery $query): ?string
{
if (null === $query->getBounds()) {
return null;
}

return '&bbox='.sprintf('%s,%s,%s,%s',
$query->getBounds()->getWest(),
$query->getBounds()->getSouth(),
$query->getBounds()->getEast(),
$query->getBounds()->getNorth()
);
}

private function executeQuery(string $url): \stdClass
{
$content = $this->getUrlContents($url);
Expand Down
Original file line number Diff line number Diff line change
@@ -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"}";
Original file line number Diff line number Diff line change
@@ -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"}";
30 changes: 30 additions & 0 deletions src/Provider/Photon/Tests/PhotonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,34 @@ 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());

}
}
Loading