|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the Geocoder package. |
| 7 | + * For the full copyright and license information, please view the LICENSE |
| 8 | + * file that was distributed with this source code. |
| 9 | + * |
| 10 | + * @license MIT License |
| 11 | + */ |
| 12 | + |
| 13 | +namespace Geocoder\Provider\Pelias; |
| 14 | + |
| 15 | +use Geocoder\Collection; |
| 16 | +use Geocoder\Exception\InvalidCredentials; |
| 17 | +use Geocoder\Exception\QuotaExceeded; |
| 18 | +use Geocoder\Exception\UnsupportedOperation; |
| 19 | +use Geocoder\Model\Address; |
| 20 | +use Geocoder\Model\AddressCollection; |
| 21 | +use Geocoder\Query\GeocodeQuery; |
| 22 | +use Geocoder\Query\ReverseQuery; |
| 23 | +use Geocoder\Http\Provider\AbstractHttpProvider; |
| 24 | +use Geocoder\Provider\Provider; |
| 25 | +use Http\Client\HttpClient; |
| 26 | + |
| 27 | +class Pelias extends AbstractHttpProvider implements Provider |
| 28 | +{ |
| 29 | + /** |
| 30 | + * @var string |
| 31 | + */ |
| 32 | + private $root; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var int |
| 36 | + */ |
| 37 | + private $version; |
| 38 | + |
| 39 | + /** |
| 40 | + * @param HttpClient $client an HTTP adapter |
| 41 | + * @param string $root url of Pelias API |
| 42 | + * @param int $version version of Pelias API |
| 43 | + */ |
| 44 | + public function __construct(HttpClient $client, string $root, int $version = 1) |
| 45 | + { |
| 46 | + $this->root = sprintf('%s/v%d', rtrim($root, '/'), $version); |
| 47 | + $this->version = $version; |
| 48 | + |
| 49 | + parent::__construct($client); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @param GeocodeQuery $query |
| 54 | + * @param array $query_data Additional query data (API key for instance). |
| 55 | + * |
| 56 | + * @return string |
| 57 | + * |
| 58 | + * @throws \Geocoder\Exception\Exception |
| 59 | + */ |
| 60 | + protected function getGeocodeQueryUrl(GeocodeQuery $query, array $query_data = []): string |
| 61 | + { |
| 62 | + $address = $query->getText(); |
| 63 | + |
| 64 | + // This API doesn't handle IPs |
| 65 | + if (filter_var($address, FILTER_VALIDATE_IP)) { |
| 66 | + throw new UnsupportedOperation( |
| 67 | + sprintf( |
| 68 | + 'The %s provider does not support IP addresses, only street addresses.', |
| 69 | + $this->getName() |
| 70 | + ) |
| 71 | + ); |
| 72 | + } |
| 73 | + |
| 74 | + $data = [ |
| 75 | + 'text' => $address, |
| 76 | + 'size' => $query->getLimit(), |
| 77 | + ]; |
| 78 | + |
| 79 | + return sprintf('%s/search?%s', $this->root, http_build_query(array_merge($data, $query_data))); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * {@inheritdoc} |
| 84 | + */ |
| 85 | + public function geocodeQuery(GeocodeQuery $query): Collection |
| 86 | + { |
| 87 | + return $this->executeQuery($this->getGeocodeQueryUrl($query)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param ReverseQuery $query |
| 92 | + * @param array $query_data Additional query data (API key for instance). |
| 93 | + * |
| 94 | + * @return string |
| 95 | + * |
| 96 | + * @throws \Geocoder\Exception\Exception |
| 97 | + */ |
| 98 | + protected function getReverseQueryUrl(ReverseQuery $query, array $query_data = []): string |
| 99 | + { |
| 100 | + $coordinates = $query->getCoordinates(); |
| 101 | + $longitude = $coordinates->getLongitude(); |
| 102 | + $latitude = $coordinates->getLatitude(); |
| 103 | + |
| 104 | + $data = [ |
| 105 | + 'point.lat' => $latitude, |
| 106 | + 'point.lon' => $longitude, |
| 107 | + 'size' => $query->getLimit(), |
| 108 | + ]; |
| 109 | + |
| 110 | + return sprintf('%s/reverse?%s', $this->root, http_build_query(array_merge($data, $query_data))); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {@inheritdoc} |
| 115 | + */ |
| 116 | + public function reverseQuery(ReverseQuery $query): Collection |
| 117 | + { |
| 118 | + return $this->executeQuery($this->getReverseQueryUrl($query)); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * {@inheritdoc} |
| 123 | + */ |
| 124 | + public function getName(): string |
| 125 | + { |
| 126 | + return 'pelias'; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param $url |
| 131 | + * |
| 132 | + * @return Collection |
| 133 | + */ |
| 134 | + protected function executeQuery(string $url): AddressCollection |
| 135 | + { |
| 136 | + $content = $this->getUrlContents($url); |
| 137 | + $json = json_decode($content, true); |
| 138 | + |
| 139 | + if (isset($json['meta'])) { |
| 140 | + switch ($json['meta']['status_code']) { |
| 141 | + case 401: |
| 142 | + case 403: |
| 143 | + throw new InvalidCredentials('Invalid or missing api key.'); |
| 144 | + case 429: |
| 145 | + throw new QuotaExceeded('Valid request but quota exceeded.'); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + if ( |
| 150 | + !isset($json['type']) |
| 151 | + || 'FeatureCollection' !== $json['type'] |
| 152 | + || !isset($json['features']) |
| 153 | + || 0 === count($json['features']) |
| 154 | + ) { |
| 155 | + return new AddressCollection([]); |
| 156 | + } |
| 157 | + |
| 158 | + $locations = $json['features']; |
| 159 | + |
| 160 | + if (empty($locations)) { |
| 161 | + return new AddressCollection([]); |
| 162 | + } |
| 163 | + |
| 164 | + $results = []; |
| 165 | + foreach ($locations as $location) { |
| 166 | + if (isset($location['bbox'])) { |
| 167 | + $bounds = [ |
| 168 | + 'south' => $location['bbox'][3], |
| 169 | + 'west' => $location['bbox'][2], |
| 170 | + 'north' => $location['bbox'][1], |
| 171 | + 'east' => $location['bbox'][0], |
| 172 | + ]; |
| 173 | + } else { |
| 174 | + $bounds = null; |
| 175 | + } |
| 176 | + |
| 177 | + $props = $location['properties']; |
| 178 | + |
| 179 | + $adminLevels = []; |
| 180 | + foreach (['region', 'county', 'locality', 'macroregion', 'country'] as $i => $component) { |
| 181 | + if (isset($props[$component])) { |
| 182 | + $adminLevels[] = ['name' => $props[$component], 'level' => $i + 1]; |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + $results[] = Address::createFromArray([ |
| 187 | + 'providedBy' => $this->getName(), |
| 188 | + 'latitude' => $location['geometry']['coordinates'][1], |
| 189 | + 'longitude' => $location['geometry']['coordinates'][0], |
| 190 | + 'bounds' => $bounds, |
| 191 | + 'streetNumber' => isset($props['housenumber']) ? $props['housenumber'] : null, |
| 192 | + 'streetName' => isset($props['street']) ? $props['street'] : null, |
| 193 | + 'subLocality' => isset($props['neighbourhood']) ? $props['neighbourhood'] : null, |
| 194 | + 'locality' => isset($props['locality']) ? $props['locality'] : null, |
| 195 | + 'postalCode' => isset($props['postalcode']) ? $props['postalcode'] : null, |
| 196 | + 'adminLevels' => $adminLevels, |
| 197 | + 'country' => isset($props['country']) ? $props['country'] : null, |
| 198 | + 'countryCode' => isset($props['country_a']) ? strtoupper($props['country_a']) : null, |
| 199 | + ]); |
| 200 | + } |
| 201 | + |
| 202 | + return new AddressCollection($results); |
| 203 | + } |
| 204 | + |
| 205 | + /** |
| 206 | + * @param array $components |
| 207 | + * |
| 208 | + * @return null|string |
| 209 | + */ |
| 210 | + protected function guessLocality(array $components) |
| 211 | + { |
| 212 | + $localityKeys = ['city', 'town', 'village', 'hamlet']; |
| 213 | + |
| 214 | + return $this->guessBestComponent($components, $localityKeys); |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * @param array $components |
| 219 | + * |
| 220 | + * @return null|string |
| 221 | + */ |
| 222 | + protected function guessStreetName(array $components) |
| 223 | + { |
| 224 | + $streetNameKeys = ['road', 'street', 'street_name', 'residential']; |
| 225 | + |
| 226 | + return $this->guessBestComponent($components, $streetNameKeys); |
| 227 | + } |
| 228 | + |
| 229 | + /** |
| 230 | + * @param array $components |
| 231 | + * |
| 232 | + * @return null|string |
| 233 | + */ |
| 234 | + protected function guessSubLocality(array $components) |
| 235 | + { |
| 236 | + $subLocalityKeys = ['neighbourhood', 'city_district']; |
| 237 | + |
| 238 | + return $this->guessBestComponent($components, $subLocalityKeys); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @param array $components |
| 243 | + * @param array $keys |
| 244 | + * |
| 245 | + * @return null|string |
| 246 | + */ |
| 247 | + protected function guessBestComponent(array $components, array $keys) |
| 248 | + { |
| 249 | + foreach ($keys as $key) { |
| 250 | + if (isset($components[$key]) && !empty($components[$key])) { |
| 251 | + return $components[$key]; |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + return null; |
| 256 | + } |
| 257 | +} |
0 commit comments