-
Notifications
You must be signed in to change notification settings - Fork 3
/
OpenCage.php
276 lines (232 loc) · 9.08 KB
/
OpenCage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?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\OpenCage;
use Geocoder\Collection;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Model\AddressBuilder;
use Geocoder\Model\AddressCollection;
use Geocoder\Provider\OpenCage\Model\OpenCageAddress;
use Geocoder\Provider\Provider;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Psr\Http\Client\ClientInterface;
/**
* @author mtm <mtm@opencagedata.com>
*/
final class OpenCage extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
public const GEOCODE_ENDPOINT_URL = 'https://api.opencagedata.com/geocode/v1/json?key=%s&query=%s&limit=%d&pretty=1';
/**
* @var string
*/
private $apiKey;
/**
* @param ClientInterface $client an HTTP adapter
* @param string $apiKey an API key
*/
public function __construct(ClientInterface $client, string $apiKey)
{
if (empty($apiKey)) {
throw new InvalidCredentials('No API key provided.');
}
$this->apiKey = $apiKey;
parent::__construct($client);
}
public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The OpenCage provider does not support IP addresses, only street addresses.');
}
$url = sprintf(self::GEOCODE_ENDPOINT_URL, $this->apiKey, urlencode($address), $query->getLimit());
if (null !== $countryCode = $query->getData('countrycode')) {
$url = sprintf('%s&countrycode=%s', $url, $countryCode);
}
if (null !== $bounds = $query->getBounds()) {
$url = sprintf('%s&bounds=%s,%s,%s,%s', $url, $bounds->getWest(), $bounds->getSouth(), $bounds->getEast(), $bounds->getNorth());
}
if (null !== $proximity = $query->getData('proximity')) {
$url = sprintf('%s&proximity=%s', $url, $proximity);
}
return $this->executeQuery($url, $query->getLocale());
}
public function reverseQuery(ReverseQuery $query): Collection
{
$coordinates = $query->getCoordinates();
$address = sprintf('%f, %f', $coordinates->getLatitude(), $coordinates->getLongitude());
$geocodeQuery = GeocodeQuery::create($address);
if (null !== $locale = $query->getLocale()) {
$geocodeQuery = $geocodeQuery->withLocale($query->getLocale());
}
return $this->geocodeQuery($geocodeQuery);
}
public function getName(): string
{
return 'opencage';
}
/**
* @throws \Geocoder\Exception\Exception
*/
private function executeQuery(string $url, ?string $locale = null): AddressCollection
{
if (null !== $locale) {
$url = sprintf('%s&language=%s', $url, $locale);
}
$content = $this->getUrlContents($url);
$json = json_decode($content, true);
// https://geocoder.opencagedata.com/api#codes
if (isset($json['status'])) {
switch ($json['status']['code']) {
case 400:
throw new InvalidArgument('Invalid request (a required parameter is missing).');
case 402:
throw new QuotaExceeded('Valid request but quota exceeded.');
case 403:
throw new InvalidCredentials('Invalid or missing api key.');
}
}
if (!isset($json['total_results']) || 0 == $json['total_results']) {
return new AddressCollection([]);
}
$locations = $json['results'];
if (empty($locations)) {
return new AddressCollection([]);
}
$results = [];
foreach ($locations as $location) {
$builder = new AddressBuilder($this->getName());
$this->parseCoordinates($builder, $location);
$components = $location['components'];
$annotations = $location['annotations'];
$this->parseAdminsLevels($builder, $components);
$this->parseCountry($builder, $components);
$builder->setLocality($this->guessLocality($components));
$builder->setSubLocality($this->guessSubLocality($components));
$builder->setStreetNumber(isset($components['house_number']) ? $components['house_number'] : null);
$builder->setStreetName($this->guessStreetName($components));
$builder->setPostalCode(isset($components['postcode']) ? $components['postcode'] : null);
$builder->setTimezone(isset($annotations['timezone']['name']) ? $annotations['timezone']['name'] : null);
/** @var OpenCageAddress $address */
$address = $builder->build(OpenCageAddress::class);
$address = $address->withMGRS(isset($annotations['MGRS']) ? $annotations['MGRS'] : null);
$address = $address->withMaidenhead(isset($annotations['Maidenhead']) ? $annotations['Maidenhead'] : null);
$address = $address->withGeohash(isset($annotations['geohash']) ? $annotations['geohash'] : null);
$address = $address->withWhat3words(isset($annotations['what3words'], $annotations['what3words']['words']) ? $annotations['what3words']['words'] : null);
$address = $address->withFormattedAddress($location['formatted']);
$results[] = $address;
}
return new AddressCollection($results);
}
/**
* @param array<string, mixed> $location
*/
private function parseCoordinates(AddressBuilder $builder, array $location): void
{
$builder->setCoordinates($location['geometry']['lat'], $location['geometry']['lng']);
$bounds = [
'south' => null,
'west' => null,
'north' => null,
'east' => null,
];
if (isset($location['bounds'])) {
$bounds = [
'south' => $location['bounds']['southwest']['lat'],
'west' => $location['bounds']['southwest']['lng'],
'north' => $location['bounds']['northeast']['lat'],
'east' => $location['bounds']['northeast']['lng'],
];
}
$builder->setBounds(
$bounds['south'],
$bounds['west'],
$bounds['north'],
$bounds['east']
);
}
/**
* @param array<string, mixed> $components
*/
private function parseAdminsLevels(AddressBuilder $builder, array $components): void
{
if (isset($components['state'])) {
$stateCode = isset($components['state_code']) ? $components['state_code'] : null;
$builder->addAdminLevel(1, $components['state'], $stateCode);
}
if (isset($components['county'])) {
$builder->addAdminLevel(2, $components['county']);
}
}
/**
* @param array<string, mixed> $components
*/
private function parseCountry(AddressBuilder $builder, array $components): void
{
if (isset($components['country'])) {
$builder->setCountry($components['country']);
}
if (isset($components['country_code'])) {
$builder->setCountryCode(\strtoupper($components['country_code']));
}
}
/**
* @param array<string, mixed> $components
*
* @return string|null
*/
protected function guessLocality(array $components)
{
$localityKeys = ['city', 'town', 'municipality', 'village', 'hamlet', 'locality', 'croft'];
return $this->guessBestComponent($components, $localityKeys);
}
/**
* @param array<string, mixed> $components
*
* @return string|null
*/
protected function guessStreetName(array $components)
{
$streetNameKeys = ['road', 'footway', 'street', 'street_name', 'residential', 'path', 'pedestrian', 'road_reference', 'road_reference_intl'];
return $this->guessBestComponent($components, $streetNameKeys);
}
/**
* @param array<string, mixed> $components
*
* @return string|null
*/
protected function guessSubLocality(array $components)
{
$subLocalityKeys = ['neighbourhood', 'suburb', 'city_district', 'district', 'quarter', 'houses', 'subdivision'];
return $this->guessBestComponent($components, $subLocalityKeys);
}
/**
* @param array<string, mixed> $components
* @param string[] $keys
*
* @return string|null
*/
protected function guessBestComponent(array $components, array $keys)
{
foreach ($keys as $key) {
if (isset($components[$key]) && !empty($components[$key])) {
return $components[$key];
}
}
return null;
}
}