Skip to content
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

[Pelias][OpenRouteService][GeocodeEarth] Fix admin level ordering #1202

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/Provider/GeocodeEarth/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
### Changed

- Migrate from PHP-HTTP to PSR-18 client
- Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level.

## 1.3.0

Expand Down
20 changes: 10 additions & 10 deletions src/Provider/GeocodeEarth/Tests/GeocodeEarthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testGeocodeWithRealAddress(): void
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('London', $result->getLocality());
$this->assertCount(3, $result->getAdminLevels());
$this->assertEquals('London', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('London', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
Expand All @@ -87,8 +87,8 @@ public function testReverseWithRealCoordinates(): void
$this->assertEquals('LA1 1UH', $result->getPostalCode());
$this->assertEquals('Lancaster', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('England', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('England', $result->getAdminLevels()->get(2)->getName());
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
Expand Down Expand Up @@ -130,8 +130,8 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -140,7 +140,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertCount(3, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -150,7 +150,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertNull($result->getLocality());
$this->assertCount(2, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Jamaica', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -160,7 +160,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United States', $result->getCountry()->getName());
}

Expand All @@ -186,8 +186,8 @@ public function testGeocodeWithCityDistrict(): void
$this->assertEquals(60437, $result->getPostalCode());
$this->assertEquals('Frankfurt', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName());
$this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('Hessen', $result->getAdminLevels()->get(3)->getName());
$this->assertNull($result->getAdminLevels()->get(1)->getCode());
$this->assertEquals('Germany', $result->getCountry()->getName());
$this->assertEquals('DEU', $result->getCountry()->getCode());
Expand Down
7 changes: 7 additions & 0 deletions src/Provider/OpenRouteService/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.

## 1.4.0

### Changed

- Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level.


## 1.3.0

### Added
Expand Down
20 changes: 10 additions & 10 deletions src/Provider/OpenRouteService/Tests/OpenRouteServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function testGeocodeWithRealAddress(): void
$this->assertEquals('Acklam Road', $result->getStreetName());
$this->assertEquals('London', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('London', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('London', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
Expand All @@ -87,8 +87,8 @@ public function testReverseWithRealCoordinates(): void
$this->assertEquals('LA1 1UH', $result->getPostalCode());
$this->assertEquals('Lancaster', $result->getLocality());
$this->assertCount(5, $result->getAdminLevels());
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('England', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('England', $result->getAdminLevels()->get(2)->getName());
$this->assertEquals('Lancashire', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('United Kingdom', $result->getCountry()->getName());
$this->assertEquals('GBR', $result->getCountry()->getCode());
}
Expand Down Expand Up @@ -130,8 +130,8 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(9.787455, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -140,7 +140,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(52.37362, $result->getCoordinates()->getLatitude(), 0.01);
$this->assertEqualsWithDelta(9.73711, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertCount(3, $result->getAdminLevels());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Niedersachsen', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Germany', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -150,7 +150,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(-78.107687, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertNull($result->getLocality());
$this->assertCount(2, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Jamaica', $result->getCountry()->getName());

/** @var \Geocoder\Model\Address $result */
Expand All @@ -160,7 +160,7 @@ public function testGeocodeWithCity(): void
$this->assertEqualsWithDelta(-76.724140000000006, $result->getCoordinates()->getLongitude(), 0.01);
$this->assertEquals('Hanover', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(3)->getName());
$this->assertEquals('Hanover', $result->getAdminLevels()->get(5)->getName());
$this->assertEquals('United States', $result->getCountry()->getName());
}

Expand All @@ -186,8 +186,8 @@ public function testGeocodeWithCityDistrict(): void
$this->assertEquals(60437, $result->getPostalCode());
$this->assertEquals('Frankfurt', $result->getLocality());
$this->assertCount(4, $result->getAdminLevels());
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(2)->getName());
$this->assertEquals('Hessen', $result->getAdminLevels()->get(1)->getName());
$this->assertEquals('Frankfurt', $result->getAdminLevels()->get(4)->getName());
$this->assertEquals('Hessen', $result->getAdminLevels()->get(3)->getName());
$this->assertNull($result->getAdminLevels()->get(1)->getCode());
$this->assertEquals('Germany', $result->getCountry()->getName());
$this->assertEquals('DEU', $result->getCountry()->getCode());
Expand Down
1 change: 1 addition & 0 deletions src/Provider/Pelias/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The change log describes what is "Added", "Removed", "Changed" or "Fixed" betwee
### Changed

- Migrate from PHP-HTTP to PSR-18 client
- Admin levels are numbered in a strict top->down order. 1 is the Country level, 5 is the locality level.

## 1.3.0

Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Pelias/Pelias.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected function executeQuery(string $url): AddressCollection
$props = $location['properties'];

$adminLevels = [];
foreach (['region', 'county', 'locality', 'macroregion', 'country'] as $i => $component) {
foreach (['country', 'macroregion', 'region', 'county', 'locality'] as $i => $component) {
if (isset($props[$component])) {
$adminLevels[] = ['name' => $props[$component], 'level' => $i + 1];
}
Expand Down
73 changes: 73 additions & 0 deletions src/Provider/Pelias/Tests/AdminLevelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/

namespace Geocoder\Provider\Pelias\Tests;

use Geocoder\Collection;
use Geocoder\IntegrationTest\BaseTestCase;
use Geocoder\Provider\Pelias\Pelias;
use Geocoder\Query\GeocodeQuery;

class AdminLevelTest extends BaseTestCase
{
protected function getCacheDir(): string
{
return __DIR__.'/.cached_responses';
}

public function testAdminLevelsOrder(): void
{
$response = '
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"coordinates": [
1.000,
1.000
]
},
"properties": {
"country": "COUNTRY",
"macroregion": "MACROREGION",
"region": "REGION",
"county": "COUNTY",
"locality": "LOCALITY",
"neighbourhood": "NEIGHBORHOOD"
}
}
]
}
';
$provider = new Pelias($this->getMockedHttpClient($response), 'http://localhost/');
$result = $provider->geocodeQuery(GeocodeQuery::create('foobar'));

$this->assertInstanceOf(Collection::class, $result);
$this->assertCount(1, $result);
$address = $result->get(0);

$expectedOrder = [
'COUNTRY',
'MACROREGION',
'REGION',
'COUNTY',
'LOCALITY',
];

foreach ($address->getAdminLevels()->all() as $key => $adminLevel) {
$this->assertSame($expectedOrder[$key - 1], $adminLevel->getName());
$this->assertSame($key, $adminLevel->getLevel(), 'Invalid admin level number for level '.$adminLevel->getName());
}
}
}