Skip to content

Commit 46a2edf

Browse files
committed
[Photon] Support bbox in geocode query
1 parent 47e33bd commit 46a2edf

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

src/Provider/Photon/Photon.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function geocodeQuery(GeocodeQuery $query): Collection
7676
if (!empty($osmTagFilters)) {
7777
$url .= $osmTagFilters;
7878
}
79+
$bboxQueryString = $this->buildBboxFilterQuery($query);
80+
if ($bboxQueryString) {
81+
$url .= $bboxQueryString;
82+
}
7983

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

@@ -190,6 +194,20 @@ private function buildOsmTagFilterQuery($filters): string
190194
return $query;
191195
}
192196

197+
private function buildBboxFilterQuery(GeocodeQuery $query): ?string
198+
{
199+
if (null === $query->getBounds()) {
200+
return null;
201+
}
202+
203+
return '&bbox='.sprintf('%s,%s,%s,%s',
204+
$query->getBounds()->getWest(),
205+
$query->getBounds()->getSouth(),
206+
$query->getBounds()->getEast(),
207+
$query->getBounds()->getNorth()
208+
);
209+
}
210+
193211
private function executeQuery(string $url): \stdClass
194212
{
195213
$content = $this->getUrlContents($url);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1 @@
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"}";

src/Provider/Photon/Tests/PhotonTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,34 @@ public function testReverseQueryWithLayerCityAndRadiusFilter(): void
192192
$this->assertEquals('city', $result->getType());
193193
$this->assertEquals('Berlin', $result->getLocality());
194194
}
195+
196+
public function testGeocodeQueryWithBbox(): void
197+
{
198+
// Germany
199+
$bounds = new \Geocoder\Model\Bounds(
200+
south: 47.2701,
201+
west: 5.8663,
202+
north: 55.992,
203+
east: 15.0419
204+
);
205+
206+
$provider = Photon::withKomootServer($this->getHttpClient());
207+
$query = GeocodeQuery::create('Paris')
208+
->withLimit(5);
209+
$results = $provider->geocodeQuery($query);
210+
211+
$this->assertCount(5, $results);
212+
$this->assertEquals('France', $results->first()->getCountry());
213+
$this->assertEquals('Paris', $results->first()->getLocality());
214+
215+
$query = GeocodeQuery::create('Paris')
216+
->withBounds($bounds)
217+
->withLimit(5);
218+
$results = $provider->geocodeQuery($query);
219+
220+
$this->assertCount(2, $results);
221+
$this->assertEquals('Deutschland', $results->first()->getCountry());
222+
$this->assertEquals('Wörrstadt', $results->first()->getLocality());
223+
224+
}
195225
}

0 commit comments

Comments
 (0)