Skip to content

Commit 918d765

Browse files
authored
[Nominatim] Add support for neighbourhood (#1178)
* Update NominatimAddress.php * Update Nominatim.php * Update NominatimTest.php * Update cached response
1 parent 9eb9855 commit 918d765

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/Provider/Nominatim/Model/NominatimAddress.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ final class NominatimAddress extends Address
6464
*/
6565
private $tags;
6666

67+
/**
68+
* @var string|null
69+
*/
70+
private $neighbourhood;
71+
6772
/**
6873
* @return string|null
6974
*/
@@ -270,4 +275,25 @@ public function withTags(array $tags = null): self
270275

271276
return $new;
272277
}
278+
279+
/**
280+
* @return string|null
281+
*/
282+
public function getNeighbourhood()
283+
{
284+
return $this->neighbourhood;
285+
}
286+
287+
/**
288+
* @param string|null $neighbourhood
289+
*
290+
* @return NominatimAddress
291+
*/
292+
public function withNeighbourhood(string $neighbourhood = null): self
293+
{
294+
$new = clone $this;
295+
$new->neighbourhood = $neighbourhood;
296+
297+
return $new;
298+
}
273299
}

src/Provider/Nominatim/Nominatim.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ private function jsonResultToLocation(\stdClass $place, bool $reverse): Location
244244
if (isset($place->address->quarter)) {
245245
$location = $location->withQuarter($place->address->quarter);
246246
}
247+
if (isset($place->address->neighbourhood)) {
248+
$location = $location->withNeighbourhood($place->address->neighbourhood);
249+
}
247250
if (isset($place->osm_id)) {
248251
$location = $location->withOSMId(intval($place->osm_id));
249252
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:562:"{"place_id":112067733,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":28063955,"lat":"35.686010380404326","lon":"139.8116922926535","place_rank":26,"category":"highway","type":"tertiary","importance":0.09999999999999998,"addresstype":"road","name":null,"display_name":"Sarue 1-chome, Koto, Tokyo, 135-0002, Japan","address":{"neighbourhood":"Sarue 1-chome","city":"Koto","postcode":"135-0002","country":"Japan","country_code":"jp"},"boundingbox":["35.6859434","35.68617","139.8098558","139.8160149"]}";

src/Provider/Nominatim/Tests/NominatimTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,21 @@ public function testGeocodeNoCountry()
236236
$this->assertEquals('way', $result->getOSMType());
237237
$this->assertEquals(null, $result->getCountry());
238238
}
239+
240+
public function testGeocodeNeighbourhood()
241+
{
242+
$provider = Nominatim::withOpenStreetMapServer($this->getHttpClient(), 'Geocoder PHP/Nominatim Provider/Nominatim Test');
243+
$results = $provider->reverseQuery(ReverseQuery::fromCoordinates(35.685939, 139.811695)->withLocale('en'));
244+
245+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
246+
$this->assertCount(1, $results);
247+
248+
/** @var \Geocoder\Provider\Nominatim\Model\NominatimAddress $result */
249+
$result = $results->first();
250+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
251+
$this->assertEquals('Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', $result->getAttribution());
252+
253+
$this->assertEquals('Sarue 1-chome', $result->getNeighbourhood());
254+
$this->assertEquals('Japan', $result->getCountry());
255+
}
239256
}

0 commit comments

Comments
 (0)