Merge branch 'geonamesxml' into 0.9.x
This commit is contained in:
commit
0bb65f8739
|
@ -76,38 +76,25 @@ class GeonamesPlugin extends Plugin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = HTTPClient::start();
|
try {
|
||||||
|
$geonames = $this->getGeonames('search',
|
||||||
// XXX: break down a name by commas, narrow by each
|
array('maxRows' => 1,
|
||||||
|
'q' => $name,
|
||||||
$result = $client->get($this->wsUrl('search',
|
'lang' => $language,
|
||||||
array('maxRows' => 1,
|
'type' => 'xml'));
|
||||||
'q' => $name,
|
} catch (Exception $e) {
|
||||||
'lang' => $language,
|
$this->log(LOG_WARNING, "Error for $name: " . $e->getMessage());
|
||||||
'type' => 'json')));
|
|
||||||
|
|
||||||
if (!$result->isOk()) {
|
|
||||||
$this->log(LOG_WARNING, "Error code " . $result->code .
|
|
||||||
" from " . $this->host . " for $name");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rj = json_decode($result->getBody());
|
$n = $geonames[0];
|
||||||
|
|
||||||
if (count($rj->geonames) <= 0) {
|
|
||||||
$this->log(LOG_WARNING, "No results in response from " .
|
|
||||||
$this->host . " for $name");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$n = $rj->geonames[0];
|
|
||||||
|
|
||||||
$location = new Location();
|
$location = new Location();
|
||||||
|
|
||||||
$location->lat = $n->lat;
|
$location->lat = (string)$n->lat;
|
||||||
$location->lon = $n->lng;
|
$location->lon = (string)$n->lng;
|
||||||
$location->names[$language] = $n->name;
|
$location->names[$language] = (string)$n->name;
|
||||||
$location->location_id = $n->geonameId;
|
$location->location_id = (string)$n->geonameId;
|
||||||
$location->location_ns = self::LOCATION_NS;
|
$location->location_ns = self::LOCATION_NS;
|
||||||
|
|
||||||
$this->setCache(array('name' => $name,
|
$this->setCache(array('name' => $name,
|
||||||
|
@ -143,54 +130,41 @@ class GeonamesPlugin extends Plugin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = HTTPClient::start();
|
try {
|
||||||
|
$geonames = $this->getGeonames('hierarchy',
|
||||||
$result = $client->get($this->wsUrl('hierarchyJSON',
|
array('geonameId' => $id,
|
||||||
array('geonameId' => $id,
|
'lang' => $language));
|
||||||
'lang' => $language)));
|
} catch (Exception $e) {
|
||||||
|
$this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
|
||||||
if (!$result->isOk()) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"Error code " . $result->code .
|
|
||||||
" from " . $this->host . " for ID $id");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rj = json_decode($result->getBody());
|
|
||||||
|
|
||||||
if (count($rj->geonames) <= 0) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"No results in response from " .
|
|
||||||
$this->host . " for ID $id");
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts = array();
|
$parts = array();
|
||||||
|
|
||||||
foreach ($rj->geonames as $level) {
|
foreach ($geonames as $level) {
|
||||||
if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
||||||
$parts[] = $level->name;
|
$parts[] = (string)$level->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$last = $rj->geonames[count($rj->geonames)-1];
|
$last = $geonames[count($geonames)-1];
|
||||||
|
|
||||||
if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
||||||
$parts[] = $last->name;
|
$parts[] = (string)$last->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
$location = new Location();
|
$location = new Location();
|
||||||
|
|
||||||
$location->location_id = $last->geonameId;
|
$location->location_id = (string)$last->geonameId;
|
||||||
$location->location_ns = self::LOCATION_NS;
|
$location->location_ns = self::LOCATION_NS;
|
||||||
$location->lat = $last->lat;
|
$location->lat = (string)$last->lat;
|
||||||
$location->lon = $last->lng;
|
$location->lon = (string)$last->lng;
|
||||||
$location->names[$language] = implode(', ', array_reverse($parts));
|
$location->names[$language] = implode(', ', array_reverse($parts));
|
||||||
|
|
||||||
$this->setCache(array('id' => $last->geonameId),
|
$this->setCache(array('id' => (string)$last->geonameId),
|
||||||
$location);
|
$location);
|
||||||
|
|
||||||
// We're responsible for this NAMESPACE; nobody else
|
// We're responsible for this namespace; nobody else
|
||||||
// can resolve it
|
// can resolve it
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
@ -223,50 +197,36 @@ class GeonamesPlugin extends Plugin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = HTTPClient::start();
|
try {
|
||||||
|
$geonames = $this->getGeonames('findNearbyPlaceName',
|
||||||
$result =
|
array('lat' => $lat,
|
||||||
$client->get($this->wsUrl('findNearbyPlaceNameJSON',
|
'lng' => $lon,
|
||||||
array('lat' => $lat,
|
'lang' => $language));
|
||||||
'lng' => $lon,
|
} catch (Exception $e) {
|
||||||
'lang' => $language)));
|
$this->log(LOG_WARNING, "Error for coords $lat, $lon: " . $e->getMessage());
|
||||||
|
|
||||||
if (!$result->isOk()) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"Error code " . $result->code .
|
|
||||||
" from " . $this->host . " for coords $lat, $lon");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$rj = json_decode($result->getBody());
|
$n = $geonames[0];
|
||||||
|
|
||||||
if (count($rj->geonames) <= 0) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"No results in response from " .
|
|
||||||
$this->host . " for coords $lat, $lon");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
$n = $rj->geonames[0];
|
|
||||||
|
|
||||||
$parts = array();
|
$parts = array();
|
||||||
|
|
||||||
$location = new Location();
|
$location = new Location();
|
||||||
|
|
||||||
$parts[] = $n->name;
|
$parts[] = (string)$n->name;
|
||||||
|
|
||||||
if (!empty($n->adminName1)) {
|
if (!empty($n->adminName1)) {
|
||||||
$parts[] = $n->adminName1;
|
$parts[] = (string)$n->adminName1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($n->countryName)) {
|
if (!empty($n->countryName)) {
|
||||||
$parts[] = $n->countryName;
|
$parts[] = (string)$n->countryName;
|
||||||
}
|
}
|
||||||
|
|
||||||
$location->location_id = $n->geonameId;
|
$location->location_id = (string)$n->geonameId;
|
||||||
$location->location_ns = self::LOCATION_NS;
|
$location->location_ns = self::LOCATION_NS;
|
||||||
$location->lat = $lat;
|
$location->lat = (string)$lat;
|
||||||
$location->lon = $lon;
|
$location->lon = (string)$lon;
|
||||||
|
|
||||||
$location->names[$language] = implode(', ', $parts);
|
$location->names[$language] = implode(', ', $parts);
|
||||||
|
|
||||||
|
@ -299,7 +259,9 @@ class GeonamesPlugin extends Plugin
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$n = $this->getCache(array('id' => $location->location_id,
|
$id = $location->location_id;
|
||||||
|
|
||||||
|
$n = $this->getCache(array('id' => $id,
|
||||||
'language' => $language));
|
'language' => $language));
|
||||||
|
|
||||||
if (!empty($n)) {
|
if (!empty($n)) {
|
||||||
|
@ -307,45 +269,32 @@ class GeonamesPlugin extends Plugin
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$client = HTTPClient::start();
|
try {
|
||||||
|
$geonames = $this->getGeonames('hierarchy',
|
||||||
$result = $client->get($this->wsUrl('hierarchyJSON',
|
array('geonameId' => $id,
|
||||||
array('geonameId' => $location->location_id,
|
'lang' => $language));
|
||||||
'lang' => $language)));
|
} catch (Exception $e) {
|
||||||
|
$this->log(LOG_WARNING, "Error for ID $id: " . $e->getMessage());
|
||||||
if (!$result->isOk()) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"Error code " . $result->code .
|
|
||||||
" from " . $this->host . " for ID " . $location->location_id);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$rj = json_decode($result->getBody());
|
|
||||||
|
|
||||||
if (count($rj->geonames) <= 0) {
|
|
||||||
$this->log(LOG_WARNING,
|
|
||||||
"No results " .
|
|
||||||
" from " . $this->host . " for ID " . $location->location_id);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parts = array();
|
$parts = array();
|
||||||
|
|
||||||
foreach ($rj->geonames as $level) {
|
foreach ($geonames as $level) {
|
||||||
if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
if (in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
||||||
$parts[] = $level->name;
|
$parts[] = (string)$level->name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$last = $rj->geonames[count($rj->geonames)-1];
|
$last = $geonames[count($geonames)-1];
|
||||||
|
|
||||||
if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
if (!in_array($level->fcode, array('PCLI', 'ADM1', 'PPL'))) {
|
||||||
$parts[] = $last->name;
|
$parts[] = (string)$last->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count($parts)) {
|
if (count($parts)) {
|
||||||
$name = implode(', ', array_reverse($parts));
|
$name = implode(', ', array_reverse($parts));
|
||||||
$this->setCache(array('id' => $location->location_id,
|
$this->setCache(array('id' => $id,
|
||||||
'language' => $language),
|
'language' => $language),
|
||||||
$name);
|
$name);
|
||||||
}
|
}
|
||||||
|
@ -354,7 +303,7 @@ class GeonamesPlugin extends Plugin
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Human-readable name for a location
|
* Human-readable URL for a location
|
||||||
*
|
*
|
||||||
* Given a location, we try to retrieve a geonames.org URL.
|
* Given a location, we try to retrieve a geonames.org URL.
|
||||||
*
|
*
|
||||||
|
@ -452,4 +401,29 @@ class GeonamesPlugin extends Plugin
|
||||||
|
|
||||||
return 'http://'.$this->host.'/'.$method.'?'.$str;
|
return 'http://'.$this->host.'/'.$method.'?'.$str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getGeonames($method, $params)
|
||||||
|
{
|
||||||
|
$client = HTTPClient::start();
|
||||||
|
|
||||||
|
$result = $client->get($this->wsUrl($method, $params));
|
||||||
|
|
||||||
|
if (!$result->isOk()) {
|
||||||
|
throw new Exception("HTTP error code " . $result->code);
|
||||||
|
}
|
||||||
|
|
||||||
|
$document = new SimpleXMLElement($result->getBody());
|
||||||
|
|
||||||
|
if (empty($document)) {
|
||||||
|
throw new Exception("No results in response");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($document->status)) {
|
||||||
|
throw new Exception("Error #".$document->status['value']." ('".$document->status['message']."')");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Array of elements
|
||||||
|
|
||||||
|
return $document->geoname;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user