forked from geocoder-php/Geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeonames.php
More file actions
166 lines (136 loc) · 5.15 KB
/
Copy pathGeonames.php
File metadata and controls
166 lines (136 loc) · 5.15 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?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\Exception\InvalidCredentials;
use Geocoder\Exception\NoResult;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\AdminLevelCollection;
use Ivory\HttpAdapter\HttpAdapterInterface;
/**
* @author Giovanni Pirrotta <giovanni.pirrotta@gmail.com>
*/
class Geonames extends AbstractHttpProvider implements LocaleAwareProvider
{
/**
* @var string
*/
const GEOCODE_ENDPOINT_URL = 'http://api.geonames.org/searchJSON?q=%s&maxRows=%d&style=full&username=%s';
/**
* @var string
*/
const REVERSE_ENDPOINT_URL = 'http://api.geonames.org/findNearbyPlaceNameJSON?lat=%F&lng=%F&style=full&maxRows=%d&username=%s';
use LocaleTrait;
/**
* @var string
*/
private $username;
/**
* @param HttpAdapterInterface $adapter An HTTP adapter
* @param string $username Username login (Free registration at http://www.geonames.org/login)
* @param string $locale A locale (optional)
*/
public function __construct(HttpAdapterInterface $adapter, $username, $locale = null)
{
parent::__construct($adapter);
$this->username = $username;
$this->locale = $locale;
}
/**
* {@inheritDoc}
*/
public function geocode($address)
{
if (null === $this->username) {
throw new InvalidCredentials('No username provided.');
}
// This API doesn't handle IPs
if (filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The Geonames provider does not support IP addresses.');
}
$query = sprintf(self::GEOCODE_ENDPOINT_URL, urlencode($address), $this->getLimit(), $this->username);
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function reverse($latitude, $longitude)
{
if (null === $this->username) {
throw new InvalidCredentials('No username provided.');
}
$query = sprintf(self::REVERSE_ENDPOINT_URL, $latitude, $longitude, $this->getLimit(), $this->username);
return $this->executeQuery($query);
}
/**
* {@inheritDoc}
*/
public function getName()
{
return 'geonames';
}
/**
* @param string $query
*/
private function executeQuery($query)
{
if (null !== $this->getLocale()) {
// Locale code transformation: for example from it_IT to it
$query = sprintf('%s&lang=%s', $query, substr($this->getLocale(), 0, 2));
}
$content = (string) $this->getAdapter()->get($query)->getBody();
if (empty($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
}
if (null === $json = json_decode($content)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
}
if (isset($json->totalResultsCount) && empty($json->totalResultsCount)) {
throw new NoResult(sprintf('No places found for query "%s".', $query));
}
$data = $json->geonames;
if (empty($data)) {
throw new NoResult(sprintf('Could not execute query "%s".', $query));
}
$results = [];
foreach ($data as $item) {
$bounds = null;
if (isset($item->bbox)) {
$bounds = array(
'south' => $item->bbox->south,
'west' => $item->bbox->west,
'north' => $item->bbox->north,
'east' => $item->bbox->east
);
}
$adminLevels = [];
for ($level = 1; $level <= AdminLevelCollection::MAX_LEVEL_DEPTH; ++ $level) {
$adminNameProp = 'adminName' . $level;
$adminCodeProp = 'adminCode' . $level;
if (! empty($item->$adminNameProp) || ! empty($item->$adminCodeProp)) {
$adminLevels[] = [
'name' => empty($item->$adminNameProp) ? null : $item->$adminNameProp ,
'code' => empty($item->$adminCodeProp) ? null : $item->$adminCodeProp,
'level' => $level,
];
}
}
$results[] = array_merge($this->getDefaults(), [
'latitude' => isset($item->lat) ? $item->lat : null,
'longitude' => isset($item->lng) ? $item->lng : null,
'bounds' => $bounds,
'locality' => isset($item->name) ? $item->name : null,
'adminLevels' => $adminLevels,
'country' => isset($item->countryName) ? $item->countryName : null,
'countryCode' => isset($item->countryCode) ? $item->countryCode : null,
'timezone' => isset($item->timezone->timeZoneId) ? $item->timezone->timeZoneId : null,
]);
}
return $this->returnResults($results);
}
}