Skip to content

Commit 4b17592

Browse files
committed
Add compatibility for multiple osm tag filters
1 parent 5db5e2d commit 4b17592

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/Provider/Photon/Photon.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,13 @@ public function geocodeQuery(GeocodeQuery $query): Collection
6868
.'/api?'
6969
.http_build_query([
7070
'q' => $address,
71-
'osm_tag' => $query->getData('osm_tag'),
7271
'limit' => $query->getLimit(),
7372
'lang' => $query->getLocale(),
7473
]);
74+
$osmTagFilters = $this->buildOsmTagFilterQuery($query->getData('osm_tag'));
75+
if (!empty($osmTagFilters)) {
76+
$url .= $osmTagFilters;
77+
}
7578

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

@@ -99,10 +102,13 @@ public function reverseQuery(ReverseQuery $query): Collection
99102
.http_build_query([
100103
'lat' => $latitude,
101104
'lon' => $longitude,
102-
'osm_tag' => $query->getData('osm_tag'),
103105
'limit' => $query->getLimit(),
104106
'lang' => $query->getLocale(),
105107
]);
108+
$osmTagFilters = $this->buildOsmTagFilterQuery($query->getData('osm_tag'));
109+
if (!empty($osmTagFilters)) {
110+
$url .= $osmTagFilters;
111+
}
106112

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

@@ -161,6 +167,25 @@ public function getName(): string
161167
return 'photon';
162168
}
163169

170+
/**
171+
* @param string|array|null $filters
172+
*/
173+
private function buildOsmTagFilterQuery($filters): string
174+
{
175+
$query = '';
176+
if (null === $filters) {
177+
return $query;
178+
}
179+
if (is_string($filters)) {
180+
return '&osm_tag='.urlencode($filters);
181+
}
182+
foreach ($filters as $filter) {
183+
$query .= '&osm_tag='.urlencode($filter);
184+
}
185+
186+
return $query;
187+
}
188+
164189
private function executeQuery(string $url): \stdClass
165190
{
166191
$content = $this->getUrlContents($url);

src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_900b8b48594b267db76d84cfe7fd8b0ba4b927d6 renamed to src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_3c362ba852ee2323f9f25ca02c28cc5238959dbc

File renamed without changes.

src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_f3b7c730ba71e84b11ac92049cd31806186e0ef1 renamed to src/Provider/Photon/Tests/.cached_responses/photon.komoot.io_8cd3447834c3ae8316dda602714b4ca4959eae65

File renamed without changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:2263:"{"features":[{"geometry":{"coordinates":[2.3328439,48.8587571],"type":"Point"},"type":"Feature","properties":{"osm_id":4839163372,"country":"France","city":"Paris","countrycode":"FR","postcode":"75006","locality":"Saint-Germain-des-Prés","type":"house","osm_type":"N","osm_key":"tourism","street":"Promenade Marceline Loridan-Ivens","district":"Paris","osm_value":"artwork","name":"La Ville de Paris","state":"Île-de-France"}},{"geometry":{"coordinates":[2.418394477960865,48.8318844],"type":"Point"},"type":"Feature","properties":{"osm_id":26008116,"extent":[2.4141177,48.834668,2.4204695,48.8289812],"country":"France","city":"Paris","countrycode":"FR","postcode":"75012","locality":"Quartier du Bel-Air","type":"house","osm_type":"W","osm_key":"tourism","street":"Promenade Maurice-Boitel","district":"Paris","osm_value":"zoo","name":"Parc zoologique de Paris","state":"Île-de-France"}},{"geometry":{"coordinates":[2.345051233260165,48.85565735],"type":"Point"},"type":"Feature","properties":{"osm_id":536982,"extent":[2.3429677,48.856811,2.3462472,48.8544964],"country":"France","city":"Paris","countrycode":"FR","postcode":"75001","locality":"Quartier Saint-Germain-l'Auxerrois","type":"house","osm_type":"R","osm_key":"tourism","street":"Cour du Premier Président","district":"Paris","osm_value":"attraction","name":"Palais de Justice de Paris","state":"Île-de-France"}},{"geometry":{"coordinates":[2.2933072,48.870745],"type":"Point"},"type":"Feature","properties":{"osm_id":2990418382,"country":"France","city":"Paris","countrycode":"FR","postcode":"75116","locality":"Quartier de Chaillot","type":"house","osm_type":"N","osm_key":"tourism","street":"Avenue Kléber","district":"Paris","osm_value":"hotel","name":"The Peninsula Paris","state":"Île-de-France"}},{"geometry":{"coordinates":[2.288981354650506,48.82872115000001],"type":"Point"},"type":"Feature","properties":{"osm_id":129135460,"extent":[2.2847236,48.8321689,2.2921187,48.8253682],"country":"France","city":"Paris","countrycode":"FR","postcode":"75015","locality":"Paris Expo Porte de Versailles","type":"house","osm_type":"W","osm_key":"tourism","district":"Paris","osm_value":"attraction","name":"Parc des Expositions de Paris","state":"Île-de-France"}}],"type":"FeatureCollection"}";

src/Provider/Photon/Tests/PhotonTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,22 @@ public function testGeocodeQueryWithOsmTagFilter()
111111
}
112112
}
113113

114+
public function testGeocodeQueryWithMultipleOsmTagFilter()
115+
{
116+
$provider = Photon::withKomootServer($this->getHttpClient());
117+
$query = GeocodeQuery::create('Paris')
118+
->withData('osm_tag', ['tourism', ':!museum'])
119+
->withLimit(5);
120+
$results = $provider->geocodeQuery($query);
121+
122+
$this->assertCount(5, $results);
123+
foreach ($results as $result) {
124+
$this->assertInstanceOf(PhotonAddress::class, $result);
125+
$this->assertEquals('tourism', $result->getOSMTag()->key);
126+
$this->assertNotEquals('museum', $result->getOSMTag()->value);
127+
}
128+
}
129+
114130
public function testReverseQuery()
115131
{
116132
$provider = Photon::withKomootServer($this->getHttpClient());

0 commit comments

Comments
 (0)