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;
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 $loc = $this->getCache(array('name' => $name,
67 'language' => $language));
74 $client = HTTPClient::start();
76 // XXX: break down a name by commas, narrow by each
78 $str = http_build_query(array('maxRows' => 1,
83 $result = $client->get('http://ws.geonames.org/search?'.$str);
85 if ($result->isOk()) {
86 $rj = json_decode($result->getBody());
87 if (count($rj->geonames) > 0) {
88 $n = $rj->geonames[0];
90 $location = new Location();
92 $location->lat = $n->lat;
93 $location->lon = $n->lng;
94 $location->names[$language] = $n->name;
95 $location->location_id = $n->geonameId;
96 $location->location_ns = self::LOCATION_NS;
98 $this->setCache(array('name' => $name,
99 'language' => $language),
102 // handled, don't continue processing!
107 // Continue processing; we don't have the answer
112 * convert an id into a Location object
114 * @param string $id Name to convert
115 * @param string $ns Name to convert
116 * @param string $language ISO code for language for results
117 * @param Location &$location Location object (may be null)
119 * @return boolean whether to continue (results in $location)
122 function onLocationFromId($id, $ns, $language, &$location)
124 if ($ns != self::LOCATION_NS) {
125 // It's not one of our IDs... keep processing
129 $loc = $this->getCache(array('id' => $id));
136 $client = HTTPClient::start();
138 $str = http_build_query(array('geonameId' => $id,
139 'lang' => $language));
141 $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
143 if ($result->isOk()) {
145 $rj = json_decode($result->getBody());
147 if (count($rj->geonames) > 0) {
151 foreach ($rj->geonames as $level) {
152 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
153 $parts[] = $level->name;
157 $last = $rj->geonames[count($rj->geonames)-1];
159 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
160 $parts[] = $last->name;
163 $location = new Location();
165 $location->location_id = $last->geonameId;
166 $location->location_ns = self::LOCATION_NS;
167 $location->lat = $last->lat;
168 $location->lon = $last->lng;
169 $location->names[$language] = implode(', ', array_reverse($parts));
171 $this->setCache(array('id' => $last->geonameId),
176 // We're responsible for this NAMESPACE; nobody else
183 * convert a lat/lon pair into a Location object
185 * Given a lat/lon, we try to find a Location that's around
186 * it or nearby. We prefer populated places (cities, towns, villages).
188 * @param string $lat Latitude
189 * @param string $lon Longitude
190 * @param string $language ISO code for language for results
191 * @param Location &$location Location object (may be null)
193 * @return boolean whether to continue (results in $location)
196 function onLocationFromLatLon($lat, $lon, $language, &$location)
198 $loc = $this->getCache(array('lat' => $lat,
206 $client = HTTPClient::start();
208 $str = http_build_query(array('lat' => $lat,
210 'lang' => $language));
213 $client->get('http://ws.geonames.org/findNearbyPlaceNameJSON?'.$str);
215 if ($result->isOk()) {
217 $rj = json_decode($result->getBody());
219 if (count($rj->geonames) > 0) {
221 $n = $rj->geonames[0];
225 $location = new Location();
229 if (!empty($n->adminName1)) {
230 $parts[] = $n->adminName1;
233 if (!empty($n->countryName)) {
234 $parts[] = $n->countryName;
237 $location->location_id = $n->geonameId;
238 $location->location_ns = self::LOCATION_NS;
239 $location->lat = $lat;
240 $location->lon = $lon;
242 $location->names[$language] = implode(', ', $parts);
244 $this->setCache(array('lat' => $lat,
248 // Success! We handled it, so no further processing
254 // For some reason we don't know, so pass.
260 * Human-readable name for a location
262 * Given a location, we try to retrieve a human-readable name
263 * in the target language.
265 * @param Location $location Location to get the name for
266 * @param string $language ISO code for language to find name in
267 * @param string &$name Place to put the name
269 * @return boolean whether to continue
272 function onLocationNameLanguage($location, $language, &$name)
274 if ($location->location_ns != self::LOCATION_NS) {
275 // It's not one of our IDs... keep processing
279 $n = $this->getCache(array('id' => $location->location_id,
280 'language' => $language));
287 $client = HTTPClient::start();
289 $str = http_build_query(array('geonameId' => $location->location_id,
290 'lang' => $language));
292 $result = $client->get('http://ws.geonames.org/hierarchyJSON?'.$str);
294 if ($result->isOk()) {
296 $rj = json_decode($result->getBody());
298 if (count($rj->geonames) > 0) {
302 foreach ($rj->geonames as $level) {
303 if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
304 $parts[] = $level->name;
308 $last = $rj->geonames[count($rj->geonames)-1];
310 if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
311 $parts[] = $last->name;
315 $name = implode(', ', array_reverse($parts));
316 $this->setCache(array('id' => $location->location_id,
317 'language' => $language),
328 * Human-readable name for a location
330 * Given a location, we try to retrieve a geonames.org URL.
332 * @param Location $location Location to get the url for
333 * @param string &$url Place to put the url
335 * @return boolean whether to continue
338 function onLocationUrl($location, &$url)
340 if ($location->location_ns != self::LOCATION_NS) {
341 // It's not one of our IDs... keep processing
345 $url = 'http://www.geonames.org/' . $location->location_id;
347 // it's been filled, so don't process further.
352 * Machine-readable name for a location
354 * Given a location, we try to retrieve a geonames.org URL.
356 * @param Location $location Location to get the url for
357 * @param string &$url Place to put the url
359 * @return boolean whether to continue
362 function onLocationRdfUrl($location, &$url)
364 if ($location->location_ns != self::LOCATION_NS) {
365 // It's not one of our IDs... keep processing
369 $url = 'http://sw.geonames.org/' . $location->location_id . '/';
371 // it's been filled, so don't process further.
375 function getCache($attrs)
377 $c = common_memcache();
383 return $c->get($this->cacheKey($attrs));
386 function setCache($attrs, $loc)
388 $c = common_memcache();
394 $c->set($this->cacheKey($attrs), $loc);
397 function clearCache($attrs)
399 $c = common_memcache();
405 $c->delete($this->cacheKey($attrs));
408 function cacheKey($attrs)
410 return common_cache_key('geonames:'.
411 implode(',', array_keys($attrs)) . ':'.
412 common_keyize(implode(',', array_values($attrs))));