forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYandexProvider.php
More file actions
149 lines (121 loc) · 4.72 KB
/
Copy pathYandexProvider.php
File metadata and controls
149 lines (121 loc) · 4.72 KB
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
<?php
/**
* 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;
use Geocoder\HttpAdapter\HttpAdapterInterface;
use Geocoder\Exception\UnsupportedException;
use Geocoder\Exception\NoResultException;
/**
* @author Antoine Corcy <contact@sbin.dk>
*/
class YandexProvider extends AbstractProvider implements LocaleAwareProviderInterface
{
/**
* @var string
*/
const GEOCODE_ENDPOINT_URL = 'http://geocode-maps.yandex.ru/1.x/?format=json&geocode=%s';
/**
* @var string
*/
const REVERSE_ENDPOINT_URL = 'http://geocode-maps.yandex.ru/1.x/?format=json&geocode=%F,%F';
/**
* @var string
*/
private $toponym = null;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter.
* @param string $locale A locale (optional).
* @param string $toponym Toponym biasing only for reverse geocoding (optional).
*/
public function __construct(HttpAdapterInterface $adapter, $locale = null, $toponym = null)
{
parent::__construct($adapter, $locale);
$this->toponym = $toponym;
}
/**
* {@inheritDoc}
*/
public function getGeocodedData($address)
{
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedException('The YandexProvider does not support IP addresses.');
}
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address));
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function getReversedData(array $coordinates)
{
$query = sprintf(self::REVERSE_ENDPOINT_URL, $coordinates[1], $coordinates[0]);
if (null !== $this->toponym) {
$query = sprintf('%s&kind=%s', $query, $this->toponym);
}
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'yandex';
}
/**
* @param string $query
*
* @return array
*/
protected function executeQuery($query)
{
if (null !== $this->getLocale()) {
$query = sprintf('%s&lang=%s', $query, str_replace('_', '-', $this->getLocale()));
}
$query = sprintf('%s&results=%d', $query, $this->getMaxResults());
$content = $this->getAdapter()->getContent($query);
$json = (array) json_decode($content, true);
if (empty($json) || '0' === $json['response']['GeoObjectCollection']['metaDataProperty']['GeocoderResponseMetaData']['found']) {
throw new NoResultException(sprintf('Could not execute query %s', $query));
}
$data = $json['response']['GeoObjectCollection']['featureMember'];
$results = array();
foreach ($data as $item) {
$bounds = null;
$details = array('pos' => ' ');
array_walk_recursive(
$item['GeoObject'],
function ($value, $key) use (&$details) {$details[$key] = $value;}
);
if (! empty($details['lowerCorner'])) {
$coordinates = explode(' ', $details['lowerCorner']);
$bounds['south'] = $coordinates[1];
$bounds['west'] = $coordinates[0];
}
if (! empty($details['upperCorner'])) {
$coordinates = explode(' ', $details['upperCorner']);
$bounds['north'] = $coordinates[1];
$bounds['east'] = $coordinates[0];
}
$coordinates = explode(' ', $details['pos']);
$results[] = array_merge($this->getDefaults(), array(
'latitude' => $coordinates[1],
'longitude' => $coordinates[0],
'bounds' => $bounds,
'streetNumber' => isset($details['PremiseNumber']) ? $details['PremiseNumber'] : null,
'streetName' => isset($details['ThoroughfareName']) ? $details['ThoroughfareName'] : null,
'cityDistrict' => isset($details['DependentLocalityName']) ? $details['DependentLocalityName'] : null,
'city' => isset($details['LocalityName']) ? $details['LocalityName'] : null,
'region' => isset($details['AdministrativeAreaName']) ? $details['AdministrativeAreaName'] : null,
'country' => isset($details['CountryName']) ? $details['CountryName'] : null,
'countryCode' => isset($details['CountryNameCode']) ? $details['CountryNameCode'] : null,
));
}
return $results;
}
}