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
52 const LOCATION_NS = 1;
54 public $host = 'ws.geonames.org';
55 public $username = null;
57 public $expiry = 7776000; // 90-day expiry
58 public $timeout = 2; // Web service timeout in seconds.
59 public $timeoutWindow = 60; // Further lookups in this process will be disabled for N seconds after a timeout.
60 public $cachePrefix = null; // Optional shared memcache prefix override
61 // to share lookups between local instances.
63 protected $lastTimeout = null; // timestamp of last web service timeout
66 * convert a name into a Location object
68 * @param string $name Name to convert
69 * @param string $language ISO code for anguage the name is in
70 * @param Location &$location Location object (may be null)
72 * @return boolean whether to continue (results in $location)
75 function onLocationFromName($name, $language, &$location)
77 $loc = $this->getCache(array('name' => $name,
78 'language' => $language));
86 $geonames = $this->getGeonames('search',
91 } catch (Exception $e) {
92 $this->log(LOG_WARNING, "Error for $name: " . $e->getMessage());
96 if (count($geonames) == 0) {
98 $this->setCache(array('name' => $name,
99 'language' => $language),
106 $location = new Location();
108 $location->lat = $this->canonical($n->lat);
109 $location->lon = $this->canonical($n->lng);
110 $location->names[$language] = (string)$n->name;
111 $location->location_id = (string)$n->geonameId;
112 $location->location_ns = self::LOCATION_NS;
114 $this->setCache(array('name' => $name,
115 'language' => $language),
118 // handled, don't continue processing!
123 * convert an id into a Location object
125 * @param string $id Name to convert
126 * @param string $ns Name to convert
127 * @param string $language ISO code for language for results
128 * @param Location &$location Location object (may be null)
130 * @return boolean whether to continue (results in $location)
133 function onLocationFromId($id, $ns, $language, &$location)
135 if ($ns != self::LOCATION_NS) {
136 // It's not one of our IDs... keep processing
140 $loc = $this->getCache(array('id' => $id));
142 if ($loc !== false) {
148 $geonames = $this->getGeonames('hierarchy',
149 array('geonameId' => $id,
150 'lang' => $language));
151 } catch (Exception $e) {
152 $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
158 foreach ($geonames as $level) {
159 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
160 $parts[] = (string)$level->name;
164 $last = $geonames[count($geonames)-1];
166 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
167 $parts[] = (string)$last->name;
170 $location = new Location();
172 $location->location_id = (string)$last->geonameId;
173 $location->location_ns = self::LOCATION_NS;
174 $location->lat = $this->canonical($last->lat);
175 $location->lon = $this->canonical($last->lng);
177 $location->names[$language] = implode(', ', array_reverse($parts));
179 $this->setCache(array('id' => (string)$last->geonameId),
182 // We're responsible for this namespace; nobody else
189 * convert a lat/lon pair into a Location object
191 * Given a lat/lon, we try to find a Location that's around
192 * it or nearby. We prefer populated places (cities, towns, villages).
194 * @param string $lat Latitude
195 * @param string $lon Longitude
196 * @param string $language ISO code for language for results
197 * @param Location &$location Location object (may be null)
199 * @return boolean whether to continue (results in $location)
202 function onLocationFromLatLon($lat, $lon, $language, &$location)
204 // Make sure they're canonical
206 $lat = $this->canonical($lat);
207 $lon = $this->canonical($lon);
209 $loc = $this->getCache(array('lat' => $lat,
212 if ($loc !== false) {
218 $geonames = $this->getGeonames('findNearbyPlaceName',
221 'lang' => $language));
222 } catch (Exception $e) {
223 $this->log(LOG_WARNING, "Error for coords $lat, $lon: " . $e->getMessage());
227 if (count($geonames) == 0) {
229 $this->setCache(array('lat' => $lat,
239 $location = new Location();
241 $parts[] = (string)$n->name;
243 if (!empty($n->adminName1)) {
244 $parts[] = (string)$n->adminName1;
247 if (!empty($n->countryName)) {
248 $parts[] = (string)$n->countryName;
251 $location->location_id = (string)$n->geonameId;
252 $location->location_ns = self::LOCATION_NS;
253 $location->lat = $this->canonical($n->lat);
254 $location->lon = $this->canonical($n->lng);
256 $location->names[$language] = implode(', ', $parts);
258 $this->setCache(array('lat' => $lat,
262 // Success! We handled it, so no further processing
268 * Human-readable name for a location
270 * Given a location, we try to retrieve a human-readable name
271 * in the target language.
273 * @param Location $location Location to get the name for
274 * @param string $language ISO code for language to find name in
275 * @param string &$name Place to put the name
277 * @return boolean whether to continue
280 function onLocationNameLanguage($location, $language, &$name)
282 if ($location->location_ns != self::LOCATION_NS) {
283 // It's not one of our IDs... keep processing
287 $id = $location->location_id;
289 $n = $this->getCache(array('id' => $id,
290 'language' => $language));
298 $geonames = $this->getGeonames('hierarchy',
299 array('geonameId' => $id,
300 'lang' => $language));
301 } catch (Exception $e) {
302 $this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
306 if (count($geonames) == 0) {
307 $this->setCache(array('id' => $id,
308 'language' => $language),
315 foreach ($geonames as $level) {
316 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
317 $parts[] = (string)$level->name;
321 $last = $geonames[count($geonames)-1];
323 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
324 $parts[] = (string)$last->name;
328 $name = implode(', ', array_reverse($parts));
329 $this->setCache(array('id' => $id,
330 'language' => $language),
338 * Human-readable URL for a location
340 * Given a location, we try to retrieve a geonames.org URL.
342 * @param Location $location Location to get the url for
343 * @param string &$url Place to put the url
345 * @return boolean whether to continue
348 function onLocationUrl($location, &$url)
350 if ($location->location_ns != self::LOCATION_NS) {
351 // It's not one of our IDs... keep processing
355 $url = 'http://www.geonames.org/' . $location->location_id;
357 // it's been filled, so don't process further.
362 * Machine-readable name for a location
364 * Given a location, we try to retrieve a geonames.org URL.
366 * @param Location $location Location to get the url for
367 * @param string &$url Place to put the url
369 * @return boolean whether to continue
372 function onLocationRdfUrl($location, &$url)
374 if ($location->location_ns != self::LOCATION_NS) {
375 // It's not one of our IDs... keep processing
379 $url = 'http://sw.geonames.org/' . $location->location_id . '/';
381 // it's been filled, so don't process further.
385 function getCache($attrs)
387 $c = common_memcache();
393 $key = $this->cacheKey($attrs);
395 $value = $c->get($key);
400 function setCache($attrs, $loc)
402 $c = common_memcache();
408 $key = $this->cacheKey($attrs);
410 $result = $c->set($key, $loc, 0, time() + $this->expiry);
415 function cacheKey($attrs)
418 implode(',', array_keys($attrs)) . ':'.
419 common_keyize(implode(',', array_values($attrs)));
420 if ($this->cachePrefix) {
421 return $this->cachePrefix . ':' . $key;
423 return common_cache_key($key);
427 function wsUrl($method, $params)
429 if (!empty($this->username)) {
430 $params['username'] = $this->username;
433 if (!empty($this->token)) {
434 $params['token'] = $this->token;
437 $str = http_build_query($params, null, '&');
439 return 'http://'.$this->host.'/'.$method.'?'.$str;
442 function getGeonames($method, $params)
444 if ($this->lastTimeout && (time() - $this->lastTimeout < $this->timeoutWindow)) {
445 throw new Exception("skipping due to recent web service timeout");
448 $client = HTTPClient::start();
449 $client->setConfig('connect_timeout', $this->timeout);
450 $client->setConfig('timeout', $this->timeout);
453 $result = $client->get($this->wsUrl($method, $params));
454 } catch (Exception $e) {
455 common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage());
456 $this->lastTimeout = time();
460 if (!$result->isOk()) {
461 throw new Exception("HTTP error code " . $result->getStatus());
464 $body = $result->getBody();
467 throw new Exception("Empty HTTP body in response");
470 // This will throw an exception if the XML is mal-formed
472 $document = new SimpleXMLElement($body);
474 // No children, usually no results
476 $children = $document->children();
478 if (count($children) == 0) {
482 if (isset($document->status)) {
483 throw new Exception("Error #".$document->status['value']." ('".$document->status['message']."')");
486 // Array of elements, >0 elements
488 return $document->geoname;
491 function onPluginVersion(&$versions)
493 $versions[] = array('name' => 'Geonames',
494 'version' => STATUSNET_VERSION,
495 'author' => 'Evan Prodromou',
496 'homepage' => 'http://status.net/wiki/Plugin:Geonames',
498 _m('Uses <a href="http://geonames.org/">Geonames</a> service to get human-readable '.
499 'names for locations based on user-provided lat/long pairs.'));
503 function canonical($coord)
505 $coord = rtrim($coord, "0");
506 $coord = rtrim($coord, ".");