|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Geocoder package. |
| 5 | + * For the full copyright and license information, please view the LICENSE |
| 6 | + * file that was distributed with this source code. |
| 7 | + * |
| 8 | + * @license MIT License |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Geocoder\Provider; |
| 12 | + |
| 13 | +use Geocoder\Exception\NoResultException; |
| 14 | +use Geocoder\Exception\UnsupportedException; |
| 15 | +use Geocoder\HttpAdapter\HttpAdapterInterface; |
| 16 | + |
| 17 | +/** |
| 18 | + * @author Niklas Närhinen <[email protected]> |
| 19 | + */ |
| 20 | +class NominatimProvider extends AbstractProvider implements LocaleAwareProviderInterface |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @param HttpAdapterInterface $adapter An HTTP adapter. |
| 24 | + * @param string $rootUrl Root URL of the nominatim server |
| 25 | + * @param string $locale A locale (optional). |
| 26 | + */ |
| 27 | + public function __construct(HttpAdapterInterface $adapter, $rootUrl, $locale = null) |
| 28 | + { |
| 29 | + parent::__construct($adapter, $locale); |
| 30 | + |
| 31 | + $this->rootUrl = rtrim($rootUrl, '/'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * {@inheritDoc} |
| 36 | + */ |
| 37 | + public function getGeocodedData($address) |
| 38 | + { |
| 39 | + // This API does not support IPv6 |
| 40 | + if (filter_var($address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { |
| 41 | + throw new UnsupportedException('The NominatimProvider does not support IPv6 addresses.'); |
| 42 | + } |
| 43 | + |
| 44 | + if ('127.0.0.1' === $address) { |
| 45 | + return array($this->getLocalhostDefaults()); |
| 46 | + } |
| 47 | + |
| 48 | + $query = sprintf($this->getGeocodeEndpointUrl(), urlencode($address), $this->getMaxResults()); |
| 49 | + $content = $this->executeQuery($query); |
| 50 | + |
| 51 | + if (null === $content) { |
| 52 | + throw new NoResultException(sprintf('Could not resolve address "%s"', $address)); |
| 53 | + } |
| 54 | + |
| 55 | + $doc = new \DOMDocument(); |
| 56 | + if (!@$doc->loadXML($content) || null === $doc->getElementsByTagName('searchresults')->item(0)) { |
| 57 | + throw new NoResultException(sprintf('Could not execute query %s', $query)); |
| 58 | + } |
| 59 | + |
| 60 | + $searchResult = $doc->getElementsByTagName('searchresults')->item(0); |
| 61 | + $places = $searchResult->getElementsByTagName('place'); |
| 62 | + |
| 63 | + if (null === $places || 0 === $places->length) { |
| 64 | + throw new NoResultException(sprintf('Could not execute query %s', $query)); |
| 65 | + } |
| 66 | + |
| 67 | + $results = array(); |
| 68 | + |
| 69 | + foreach ($places as $place) { |
| 70 | + $boundsAttr = $place->getAttribute('boundingbox'); |
| 71 | + list($bounds['south'], $bounds['north'], $bounds['west'], $bounds['east']) = $boundsAttr |
| 72 | + ? explode(',', $boundsAttr) |
| 73 | + : null; |
| 74 | + |
| 75 | + $results[] = array_merge($this->getDefaults(), array( |
| 76 | + 'latitude' => $place->getAttribute('lat'), |
| 77 | + 'longitude' => $place->getAttribute('lon'), |
| 78 | + 'bounds' => $bounds, |
| 79 | + 'zipcode' => $this->getNodeValue($place->getElementsByTagName('postcode')), |
| 80 | + 'county' => $this->getNodeValue($place->getElementsByTagName('county')), |
| 81 | + 'region' => $this->getNodeValue($place->getElementsByTagName('state')), |
| 82 | + 'streetNumber' => $this->getNodeValue($place->getElementsByTagName('house_number')), |
| 83 | + 'streetName' => $this->getNodeValue($place->getElementsByTagName('road')) ?: $this->getNodeValue($place->getElementsByTagName('pedestrian')), |
| 84 | + 'city' => $this->getNodeValue($place->getElementsByTagName('city')), |
| 85 | + 'cityDistrict' => $this->getNodeValue($place->getElementsByTagName('suburb')), |
| 86 | + 'country' => $this->getNodeValue($place->getElementsByTagName('country')), |
| 87 | + 'countryCode' => strtoupper($this->getNodeValue($place->getElementsByTagName('country_code'))), |
| 88 | + )); |
| 89 | + } |
| 90 | + |
| 91 | + return $results; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * {@inheritDoc} |
| 96 | + */ |
| 97 | + public function getReversedData(array $coordinates) |
| 98 | + { |
| 99 | + $query = sprintf($this->getReverseEndpointUrl(), $coordinates[0], $coordinates[1]); |
| 100 | + $content = $this->executeQuery($query); |
| 101 | + |
| 102 | + if (null === $content) { |
| 103 | + throw new NoResultException(sprintf('Unable to resolve the coordinates %s', implode(', ', $coordinates))); |
| 104 | + } |
| 105 | + |
| 106 | + $doc = new \DOMDocument(); |
| 107 | + if (!@$doc->loadXML($content) || $doc->getElementsByTagName('error')->length > 0) { |
| 108 | + throw new NoResultException(sprintf('Could not resolve coordinates %s', implode(', ', $coordinates))); |
| 109 | + } |
| 110 | + |
| 111 | + $searchResult = $doc->getElementsByTagName('reversegeocode')->item(0); |
| 112 | + $addressParts = $searchResult->getElementsByTagName('addressparts')->item(0); |
| 113 | + $result = $searchResult->getElementsByTagName('result')->item(0); |
| 114 | + |
| 115 | + return array(array_merge($this->getDefaults(), array( |
| 116 | + 'latitude' => $result->getAttribute('lat'), |
| 117 | + 'longitude' => $result->getAttribute('lon'), |
| 118 | + 'zipcode' => $this->getNodeValue($addressParts->getElementsByTagName('postcode')), |
| 119 | + 'county' => $this->getNodeValue($addressParts->getElementsByTagName('county')), |
| 120 | + 'region' => $this->getNodeValue($addressParts->getElementsByTagName('state')), |
| 121 | + 'streetNumber' => $this->getNodeValue($addressParts->getElementsByTagName('house_number')), |
| 122 | + 'streetName' => $this->getNodeValue($addressParts->getElementsByTagName('road')) ?: $this->getNodeValue($addressParts->getElementsByTagName('pedestrian')), |
| 123 | + 'city' => $this->getNodeValue($addressParts->getElementsByTagName('city')), |
| 124 | + 'cityDistrict' => $this->getNodeValue($addressParts->getElementsByTagName('suburb')), |
| 125 | + 'country' => $this->getNodeValue($addressParts->getElementsByTagName('country')), |
| 126 | + 'countryCode' => strtoupper($this->getNodeValue($addressParts->getElementsByTagName('country_code'))), |
| 127 | + ))); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * {@inheritDoc} |
| 132 | + */ |
| 133 | + public function getName() |
| 134 | + { |
| 135 | + return 'openstreetmaps'; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @param string $query |
| 140 | + * |
| 141 | + * @return string |
| 142 | + */ |
| 143 | + protected function executeQuery($query) |
| 144 | + { |
| 145 | + if (null !== $this->getLocale()) { |
| 146 | + $query = sprintf('%s&accept-language=%s', $query, $this->getLocale()); |
| 147 | + } |
| 148 | + |
| 149 | + return $this->getAdapter()->getContent($query); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @return string |
| 154 | + */ |
| 155 | + protected function getGeocodeEndpointUrl() |
| 156 | + { |
| 157 | + return $this->rootUrl.'/search?q=%s&format=xml&addressdetails=1&limit=%d'; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return string |
| 162 | + */ |
| 163 | + protected function getReverseEndpointUrl() |
| 164 | + { |
| 165 | + return $this->rootUrl.'/reverse?format=xml&lat=%F&lon=%F&addressdetails=1&zoom=18'; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param \DOMNodeList |
| 170 | + * |
| 171 | + * @return string |
| 172 | + */ |
| 173 | + private function getNodeValue(\DOMNodeList $element) |
| 174 | + { |
| 175 | + return $element->length ? $element->item(0)->nodeValue : null; |
| 176 | + } |
| 177 | +} |
0 commit comments