3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin to convert string locations to Geonames IDs and vice versa
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @copyright 2009 StatusNet Inc.
26 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27 * @link http://status.net/
30 if (!defined('STATUSNET')) {
35 * Plugin to convert string locations to Geonames IDs and vice versa
37 * This handles most of the events that Location class emits. It uses
38 * the geonames.org Web service to convert names like 'Montreal, Quebec, Canada'
39 * into IDs and lat/lon pairs.
43 * @author Evan Prodromou <evan@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
50 class GeonamesPlugin extends Plugin
55 * convert a name into a Location object
57 * @param string $name Name to convert
58 * @param string $language ISO code for anguage the name is in
59 * @param Location &$location Location object (may be null)
61 * @return boolean whether to continue (results in $location)
64 function onLocationFromName($name, $language, &$location)
66 $client = HTTPClient::start();
68 // XXX: break down a name by commas, narrow by each
70 $str = http_build_query(array('maxRows' => 1,
75 $result = $client->get('http://ws.geonames.org/search?'.$str);
77 if ($result->isOk()) {
78 $rj = json_decode($result->getBody());
79 if (count($rj->geonames) > 0) {
80 $n = $rj->geonames[0];
82 $location = new Location();
84 $location->lat = $n->lat;
85 $location->lon = $n->lng;
86 $location->names[$language] = $n->name;
87 $location->location_id = $n->geonameId;
88 $location->location_ns = self::NAMESPACE;
90 // handled, don't continue processing!
95 // Continue processing; we don't have the answer
100 * convert an id into a Location object
102 * @param string $id Name to convert
103 * @param string $ns Name to convert
104 * @param string $language ISO code for language for results
105 * @param Location &$location Location object (may be null)
107 * @return boolean whether to continue (results in $location)
110 function onLocationFromId($id, $ns, $language, &$location)
112 if ($ns != self::NAMESPACE) {
113 // It's not one of our IDs... keep processing
117 $client = HTTPClient::start();
119 $str = http_build_query(array('geonameId' => $id,
120 'lang' => $language));
122 $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
124 if ($result->isOk()) {
126 $rj = json_decode($result->getBody());
128 if (count($rj->geonames) > 0) {
132 foreach ($rj->geonames as $level) {
133 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
134 $parts[] = $level->name;
138 $last = $rj->geonames[count($rj->geonames)-1];
140 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
141 $parts[] = $last->name;
144 $location = new Location();
146 $location->location_id = $last->geonameId;
147 $location->location_ns = self::NAMESPACE;
148 $location->lat = $last->lat;
149 $location->lon = $last->lng;
150 $location->names[$language] = implode(', ', array_reverse($parts));
154 // We're responsible for this NAMESPACE; nobody else
161 * convert a lat/lon pair into a Location object
163 * Given a lat/lon, we try to find a Location that's around
164 * it or nearby. We prefer populated places (cities, towns, villages).
166 * @param string $lat Latitude
167 * @param string $lon Longitude
168 * @param string $language ISO code for language for results
169 * @param Location &$location Location object (may be null)
171 * @return boolean whether to continue (results in $location)
174 function onLocationFromLatLon($lat, $lon, $language, &$location)
176 $client = HTTPClient::start();
178 $str = http_build_query(array('lat' => $lat,
180 'lang' => $language));
183 $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str);
185 if ($result->isOk()) {
187 $rj = json_decode($result->getBody());
189 if (count($rj->geonames) > 0) {
191 $n = $rj->geonames[0];
195 $location = new Location();
199 if (!empty($n->adminName1)) {
200 $parts[] = $n->adminName1;
203 if (!empty($n->countryName)) {
204 $parts[] = $n->countryName;
207 $location->location_id = $n->geonameId;
208 $location->location_ns = self::NAMESPACE;
209 $location->lat = $lat;
210 $location->lon = $lon;
212 $location->names[$language] = implode(', ', $parts);
214 // Success! We handled it, so no further processing
220 // For some reason we don't know, so pass.
226 * Human-readable name for a location
228 * Given a location, we try to retrieve a human-readable name
229 * in the target language.
231 * @param Location $location Location to get the name for
232 * @param string $language ISO code for language to find name in
233 * @param string &$name Place to put the name
235 * @return boolean whether to continue
238 function onLocationNameLanguage($location, $language, &$name)
240 if ($location->location_ns != self::NAMESPACE) {
241 // It's not one of our IDs... keep processing
245 $client = HTTPClient::start();
247 $str = http_build_query(array('geonameId' => $id,
248 'lang' => $language));
250 $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
252 if ($result->isOk()) {
254 $rj = json_decode($result->getBody());
256 if (count($rj->geonames) > 0) {
260 foreach ($rj->geonames as $level) {
261 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
262 $parts[] = $level->name;
266 $last = $rj->geonames[count($rj->geonames)-1];
268 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
269 $parts[] = $last->name;
273 $name = implode(', ', array_reverse($parts));
283 * Human-readable name for a location
285 * Given a location, we try to retrieve a geonames.org URL.
287 * @param Location $location Location to get the url for
288 * @param string &$url Place to put the url
290 * @return boolean whether to continue
293 function onLocationUrl($location, &$url)
295 if ($location->location_ns != self::NAMESPACE) {
296 // It's not one of our IDs... keep processing
300 $url = 'http://www.geonames.org/' . $location->location_id;
302 // it's been filled, so don't process further.