Skip to content

feat: [Photon] support multiple layers #1253

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
Show file tree
Hide file tree
Changes from all 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
23 changes: 21 additions & 2 deletions src/Provider/Photon/Photon.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function geocodeQuery(GeocodeQuery $query): Collection
.'/api?'
.http_build_query([
'q' => $address,
'layer' => $query->getData('layer'),
'limit' => $query->getLimit(),
'lang' => $query->getLocale(),
'lat' => $query->getData('lat'),
'lon' => $query->getData('lon'),
]);
$url .= $this->buildLayerFilterQuery($query->getData('layer'));
$osmTagFilters = $this->buildOsmTagFilterQuery($query->getData('osm_tag'));
if (!empty($osmTagFilters)) {
$url .= $osmTagFilters;
Expand Down Expand Up @@ -109,11 +109,11 @@ 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(),
]);
$url .= $this->buildLayerFilterQuery($query->getData('layer'));
$osmTagFilters = $this->buildOsmTagFilterQuery($query->getData('osm_tag'));
if (!empty($osmTagFilters)) {
$url .= $osmTagFilters;
Expand Down Expand Up @@ -177,6 +177,25 @@ public function getName(): string
return 'photon';
}

/**
* @param string|string[]|null $layers
*/
private function buildLayerFilterQuery(mixed $layers): string
{
$query = '';
if (null === $layers) {
return $query;
}
if (is_string($layers)) {
return '&layer='.urlencode($layers);
}
foreach ($layers as $layer) {
$query .= '&layer='.urlencode($layer);
}

return $query;
}

/**
* @param string|array<int, string>|null $filters
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:359:"{"features":[{"geometry":{"coordinates":[21.2392122,49.0000074],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":388255,"extent":[20.870461,49.185185,21.485864,48.810739],"country":"Slovensko","osm_key":"place","countrycode":"SK","osm_value":"city","name":"Prešov","state":"Prešovský kraj","type":"city"}}],"type":"FeatureCollection"}";
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
s:703:"{"features":[{"geometry":{"coordinates":[21.2392122,49.0000074],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":388255,"extent":[20.870461,49.185185,21.485864,48.810739],"country":"Slovensko","osm_key":"place","countrycode":"SK","osm_value":"city","name":"Prešov","state":"Prešovský kraj","type":"city"}},{"geometry":{"coordinates":[21.2392122,49.0000074],"type":"Point"},"type":"Feature","properties":{"osm_type":"R","osm_id":2320257,"extent":[21.1569866,49.0468381,21.3354271,48.9449997],"country":"Slovensko","osm_key":"place","city":"Prešov","countrycode":"SK","osm_value":"city","name":"Prešov","state":"Prešovský kraj","type":"district"}}],"type":"FeatureCollection"}";
26 changes: 26 additions & 0 deletions src/Provider/Photon/Tests/PhotonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,32 @@ public function testReverseQueryWithLayerCityAndRadiusFilter(): void
$this->assertEquals('Berlin', $result->getLocality());
}

public function testReverseQueryWithMultipleLayers(): void
{
$provider = Photon::withKomootServer($this->getHttpClient());
$reverseQuery = ReverseQuery::fromCoordinates(49.001831, 21.239311)
->withData('layer', 'city')
->withLimit(2);

$results = $provider->reverseQuery($reverseQuery);
$this->assertInstanceOf(AddressCollection::class, $results);
$this->assertCount(1, $results);
$result0 = $results->get(0);
$this->assertInstanceOf(PhotonAddress::class, $result0);
$this->assertEquals('city', $result0->getType());

$reverseQuery = $reverseQuery->withData('layer', ['city', 'district']);
$results = $provider->reverseQuery($reverseQuery);
$this->assertInstanceOf(AddressCollection::class, $results);
$this->assertCount(2, $results);
$result0 = $results->get(0);
$this->assertInstanceOf(PhotonAddress::class, $result0);
$this->assertEquals('city', $result0->getType());
$result1 = $results->get(1);
$this->assertInstanceOf(PhotonAddress::class, $result1);
$this->assertEquals('district', $result1->getType());
}

public function testGeocodeQueryWithBbox(): void
{
// Germany
Expand Down