Skip to content

Commit 29b5743

Browse files
committed
add guess method for street and suburb
1 parent 1c5bff6 commit 29b5743

File tree

3 files changed

+160
-6
lines changed

3 files changed

+160
-6
lines changed

src/Geocoder/Provider/OpenCage.php

+42-6
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public function getName()
8989
}
9090

9191
/**
92-
* @param string $query
92+
* @param $query
93+
* @return \Geocoder\Model\AddressCollection
9394
*/
9495
private function executeQuery($query)
9596
{
@@ -141,8 +142,8 @@ private function executeQuery($query)
141142
'longitude' => $location['geometry']['lng'],
142143
'bounds' => $bounds ?: [],
143144
'streetNumber' => isset($comp['house_number']) ? $comp['house_number'] : null,
144-
'streetName' => isset($comp['road']) ? $comp['road'] : null,
145-
'subLocality' => isset($comp['suburb']) ? $comp['suburb'] : null,
145+
'streetName' => $this->guessStreetName($comp),
146+
'subLocality' => $this->guessSubLocality($comp),
146147
'locality' => $this->guessLocality($comp),
147148
'postalCode' => isset($comp['postcode']) ? $comp['postcode'] : null,
148149
'adminLevels' => $adminLevels,
@@ -164,12 +165,47 @@ protected function guessLocality(array $components)
164165
{
165166
$localityKeys = array('city', 'town' , 'village', 'hamlet');
166167

167-
foreach ($localityKeys as $key) {
168-
if (isset($components[$key])) {
168+
return $this->guessBestComponent($components, $localityKeys);
169+
}
170+
171+
/**
172+
* @param array $components
173+
*
174+
* @return null|string
175+
*/
176+
protected function guessStreetName(array $components)
177+
{
178+
$streetNameKeys = array('road', 'street', 'street_name', 'residential');
179+
180+
return $this->guessBestComponent($components, $streetNameKeys);
181+
}
182+
183+
/**
184+
* @param array $components
185+
*
186+
* @return null|string
187+
*/
188+
protected function guessSubLocality(array $components)
189+
{
190+
$subLocalityKeys = array('suburb', 'neighbourhood', 'city_district');
191+
192+
return $this->guessBestComponent($components, $subLocalityKeys);
193+
}
194+
195+
/**
196+
* @param array $components
197+
* @param array $keys
198+
*
199+
* @return null|string
200+
*/
201+
protected function guessBestComponent(array $components, array $keys)
202+
{
203+
foreach ($keys as $key) {
204+
if (isset($components[$key]) && !empty($components[$key])) {
169205
return $components[$key];
170206
}
171207
}
172208

173-
return;
209+
return null;
174210
}
175211
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
s:2884:"{
2+
"licenses" : [
3+
{
4+
"name" : "CC-BY-SA",
5+
"url" : "http://creativecommons.org/licenses/by-sa/3.0/"
6+
},
7+
{
8+
"name" : "ODbL",
9+
"url" : "http://opendatacommons.org/licenses/odbl/summary/"
10+
}
11+
],
12+
"rate" : {
13+
"limit" : 2500,
14+
"remaining" : 2496,
15+
"reset" : 1439856000
16+
},
17+
"results" : [
18+
{
19+
"annotations" : {
20+
"DMS" : {
21+
"lat" : "49\u00b0 8' 20.73264'' N",
22+
"lng" : "1\u00b0 39' 26.08632'' E"
23+
},
24+
"MGRS" : "31UDQ0206243786",
25+
"Maidenhead" : "JN09td83uj",
26+
"Mercator" : {
27+
"x" : 184483.803,
28+
"y" : 6266161.941
29+
},
30+
"OSM" : {
31+
"url" : "http://www.openstreetmap.org/?mlat=49.13909&mlon=1.65725#map=17/49.13909/1.65725"
32+
},
33+
"callingcode" : 33,
34+
"geohash" : "u09pt9qzbwsrft58uhm6",
35+
"sun" : {
36+
"rise" : {
37+
"astronomical" : 1439779140,
38+
"civil" : 1439784780,
39+
"nautical" : 1439782200
40+
},
41+
"set" : {
42+
"astronomical" : 1439845980,
43+
"civil" : 1439840400,
44+
"nautical" : 1439843040
45+
}
46+
},
47+
"timezone" : {
48+
"name" : "Europe/Paris",
49+
"now_in_dst" : 1,
50+
"offset_sec" : 7200,
51+
"offset_string" : 200,
52+
"short_name" : "CEST"
53+
},
54+
"what3words" : {
55+
"words" : "cocotier.maniable.arabesque"
56+
}
57+
},
58+
"bounds" : {
59+
"northeast" : {
60+
"lat" : 49.1391424,
61+
"lng" : 1.6572962
62+
},
63+
"southwest" : {
64+
"lat" : 49.1390424,
65+
"lng" : 1.6571962
66+
}
67+
},
68+
"components" : {
69+
"country" : "France",
70+
"country_code" : "fr",
71+
"county" : "Pontoise",
72+
"hotel" : "Les Jardins d'\u00c9picure",
73+
"postcode" : "95710",
74+
"road" : "Grande Rue",
75+
"state" : "\u00cele-de-France",
76+
"village" : "Bray-et-L\u00fb"
77+
},
78+
"confidence" : 10,
79+
"formatted" : "Les Jardins d'\u00c9picure, Grande Rue, 95710 Bray-et-L\u00fb, France",
80+
"geometry" : {
81+
"lat" : 49.1390924,
82+
"lng" : 1.6572462
83+
}
84+
}
85+
],
86+
"status" : {
87+
"code" : 200,
88+
"message" : "OK"
89+
},
90+
"stay_informed" : {
91+
"blog" : "http://blog.opencagedata.com",
92+
"twitter" : "https://twitter.com/opencagedata"
93+
},
94+
"thanks" : "For using an OpenCage Data API",
95+
"timestamp" : {
96+
"created_http" : "Mon, 17 Aug 2015 14:29:15 GMT",
97+
"created_unix" : 1439821755
98+
},
99+
"total_results" : 1
100+
}";

tests/Geocoder/Tests/Provider/OpenCageTest.php

+18
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,24 @@ public function testReverseWithRealCoordinates()
127127
$this->assertEquals('Europe/London' , $result->getTimezone());
128128
}
129129

130+
public function testReverseWithVillage()
131+
{
132+
if (!isset($_SERVER['OPENCAGE_API_KEY'])) {
133+
$this->markTestSkipped('You need to configure the OPENCAGE_API_KEY value in phpunit.xml');
134+
}
135+
136+
$provider = new OpenCage($this->getAdapter($_SERVER['OPENCAGE_API_KEY']), $_SERVER['OPENCAGE_API_KEY']);
137+
$results = $provider->reverse(49.1390924, 1.6572462);
138+
139+
$this->assertInstanceOf('Geocoder\Model\AddressCollection', $results);
140+
$this->assertCount(1, $results);
141+
142+
/** @var \Geocoder\Model\Address $result */
143+
$result = $results->first();
144+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
145+
$this->assertEquals('Bray-et-Lû', $result->getLocality());
146+
}
147+
130148
public function testGeocodeWithCity()
131149
{
132150
if (!isset($_SERVER['OPENCAGE_API_KEY'])) {

0 commit comments

Comments
 (0)