Skip to content

Commit b200091

Browse files
authored
Fix Nominatim provider when "missing" osm_id and osm_type (#997)
* Fix Nominatim provider when "missing" osm_id and osm_type * Apply fixes from StyleCI
1 parent 22f4305 commit b200091

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Provider/Nominatim/Nominatim.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,15 @@ private function jsonResultToLocation(\stdClass $place, bool $reverse): Location
230230

231231
$location = $builder->build(NominatimAddress::class);
232232
$location = $location->withAttribution($place->licence);
233-
$location = $location->withOSMId(intval($place->osm_id));
234-
$location = $location->withOSMType($place->osm_type);
235233
$location = $location->withDisplayName($place->display_name);
236234

235+
if (isset($place->osm_id)) {
236+
$location = $location->withOSMId(intval($place->osm_id));
237+
}
238+
if (isset($place->osm_type)) {
239+
$location = $location->withOSMType($place->osm_type);
240+
}
241+
237242
if (false === $reverse) {
238243
$location = $location->withCategory($place->category);
239244
$location = $location->withType($place->type);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
s:505:"[{"place_id":200404707,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","boundingbox":["34.085763939939","34.085863939939","-118.2905153602","-118.2904153602"],"lat":"34.0858139399386","lon":"-118.290465360203","display_name":"LA, California, 90210, United States of America","place_rank":21,"category":"place","type":"postcode","importance":0.535,"address":{"city":"LA","state":"California","postcode":"90210","country":"United States of America","country_code":"us"}}]";

src/Provider/Nominatim/Tests/NominatimTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,4 +159,25 @@ public function testGeocodeWithViewbox()
159159
$this->assertEquals('way', $result->getOSMType());
160160
$this->assertEquals('yes', $result->getType());
161161
}
162+
163+
public function testGeocodeNoOSMId()
164+
{
165+
$provider = Nominatim::withOpenStreetMapServer($this->getHttpClient(), 'Geocoder PHP/Nominatim Provider/Nominatim Test');
166+
$results = $provider->geocodeQuery(GeocodeQuery::create('90210,United States'));
167+
168+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
169+
$this->assertCount(1, $results);
170+
171+
/** @var \Geocoder\Model\Address $result */
172+
$result = $results->first();
173+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
174+
$this->assertEquals('90210', $result->getPostalCode());
175+
$this->assertEquals('US', $result->getCountry()->getCode());
176+
177+
$this->assertEquals('Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright', $result->getAttribution());
178+
$this->assertEquals('place', $result->getCategory());
179+
$this->assertEquals('postcode', $result->getType());
180+
$this->assertEquals(null, $result->getOSMId());
181+
$this->assertEquals(null, $result->getOSMType());
182+
}
162183
}

0 commit comments

Comments
 (0)