Skip to content
This repository has been archived by the owner on Feb 2, 2022. It is now read-only.

Commit

Permalink
Fine-tunes geocoding for all geolocation services.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Safley committed Nov 17, 2010
1 parent c9a35eb commit 08d8bdc
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 57 deletions.
89 changes: 60 additions & 29 deletions Geolocation/GeoNames.php
Expand Up @@ -11,7 +11,10 @@ class Plants_Geolocation_GeoNames implements Plants_Geolocation_Interface
const URL = 'http://ws.geonames.org/search';

private $_client;
private $_result;
private $_totalCount;
private $_latitude;
private $_longitude;
private $_requestUri;

public function __construct()
{
Expand All @@ -21,47 +24,75 @@ public function __construct()
$this->_client->setParameterGet('type', 'json');
}

public function query($location, $limit)
public function query($location, $country)
{
$this->_client->setParameterGet('maxRows', $limit);
$this->_client->setParameterGet('q', $location);
$response = $this->_client->request();
$this->_result = json_decode($response->getBody());
}

public function getRequestUri()
{
preg_match('/GET (.+) /', $this->_client->getLastRequest(), $matches);
return $matches[1];
}

public function getResponse()
{
return $this->_client->getLastResponse()->getBody();

// Set the queries in priority order.
$queries = array("$location $country", $location, $country);

// Iterate the queries.
foreach ($queries as $query) {

// Make the request.
$this->_client->setParameterGet('q', $query);
$response = json_decode($this->_client->request()->getBody());

// Continue to the next query if there are no results.
if (!$response->totalResultsCount) {
continue;
}

// Set the first response with results.
if (!isset($firstResponse)) {
$firstResponse = $response;
$firstRequest = $this->_client->getLastRequest();
}

// Set the coordinates from the first result with a matching country.
foreach ($response->geonames as $result) {
if (strstr($country, $result->countryName)) {
$this->_totalCount = $response->totalResultsCount;
$this->_latitude = $result->lat;
$this->_longitude = $result->lng;
preg_match('/GET (.+) /', $this->_client->getLastRequest(), $matches);
$this->_requestUri = $matches[1];
return;
}
}
}

// There was at least one response, but no country matches. Set the
// coordinates from the first result.
if (isset($firstResponse)) {
$this->_totalCount = $firstResponse->totalResultsCount;
$this->_latitude = $firstResponse->geonames[0]->lat;
$this->_longitude = $firstResponse->geonames[0]->lng;
preg_match('/GET (.+) /', $firstRequest, $matches);
$this->_requestUri = $matches[1];
return;
}

// There were no results for all queries.
$this->_totalCount = 0;
}

public function getTotalCount()
{
return $this->_result->totalResultsCount;
return $this->_totalCount;
}

public function getLatitude($index)
public function getLatitude()
{
return $this->_result->geonames[$index]->lat;
return $this->_latitude;
}

public function getLongitude($index)
public function getLongitude()
{
return $this->_result->geonames[$index]->lng;
return $this->_longitude;
}

public function getLocation($index)
public function getRequestUri()
{
$result = $this->_result->geonames[$index];
$name = array(trim($result->name),
trim($result->adminName1),
trim($result->countryName));
$name = array_filter($name);
return implode(' ', $name);
return $this->_requestUri;
}
}
2 changes: 1 addition & 1 deletion Geolocation/Interface.php
Expand Up @@ -10,7 +10,7 @@ interface Plants_Geolocation_Interface
* @param string $location The string to geolocate.
* @param int $limit The maximum number of geolocation results.
*/
public function query($location, $country = null);
public function query($location, $country);

/**
* Get the total count of the query results.
Expand Down
85 changes: 61 additions & 24 deletions Geolocation/Nominatim.php
Expand Up @@ -11,52 +11,89 @@ class Plants_Geolocation_Nominatim implements Plants_Geolocation_Interface
const URL = 'http://nominatim.openstreetmap.org/search';

private $_client;
private $_result;
private $_totalCount;
private $_latitude;
private $_longitude;
private $_requestUri;

public function __construct()
{
require_once 'Zend/Http/Client.php';
$this->_client = new Zend_Http_Client(self::URL);
$this->_client->setConfig(array('keepalive' => true));
$this->_client->setParameterGet('format', 'json');
$this->_client->setParameterGet('addressdetails', '1');
}

public function query($location, $limit)
public function query($location, $country)
{
$this->_client->setParameterGet('limit', $limit);
$this->_client->setParameterGet('q', $location);
$response = $this->_client->request();
$this->_result = json_decode($response->getBody());
}

public function getRequestUri()
{
preg_match('/GET (.+) /', $this->_client->getLastRequest(), $matches);
return $matches[1];
}

public function getResponse()
{
return $this->_client->getLastResponse()->getBody();

// Set the queries in priority order.
$queries = array("$location $country", $location, $country);

// Iterate the queries.
foreach ($queries as $query) {

// Make the request.
$this->_client->setParameterGet('q', $query);
$response = json_decode($this->_client->request()->getBody());

// Continue to the next query if there are no results.
if (!count($response)) {
continue;
}

// Set the first response with results.
if (!isset($firstResponse)) {
$firstResponse = $response;
$firstRequest = $this->_client->getLastRequest();
}

// Set the coordinates from the first result with a matching country.
foreach ($response as $result) {
if (strstr($country, $result->address->country)) {
$this->_totalCount = count($response);
$this->_latitude = $result->lat;
$this->_longitude = $result->lon;
preg_match('/GET (.+) /', $this->_client->getLastRequest(), $matches);
$this->_requestUri = $matches[1];
return;
}
}
}

// There was at least one response, but no country matches. Set the
// coordinates from the first result.
if (isset($firstResponse)) {
$this->_totalCount = count($firstResponse);
$this->_latitude = $firstResponse[0]->lat;
$this->_longitude = $firstResponse[0]->lon;
preg_match('/GET (.+) /', $firstRequest, $matches);
$this->_requestUri = $matches[1];
return;
}

// There were no results for all queries.
$this->_totalCount = 0;
}

public function getTotalCount()
{
return count($this->_result);
return $this->_totalCount;
}

public function getLatitude($index)
public function getLatitude()
{
return $this->_result[$index]->lat;
return $this->_latitude;
}

public function getLongitude($index)
public function getLongitude()
{
return $this->_result[$index]->lon;
return $this->_longitude;
}

public function getLocation($index)
public function getRequestUri()
{
return $this->_result[$index]->display_name;
return $this->_requestUri;
}
}
4 changes: 2 additions & 2 deletions Geolocation/PlaceFinder.php
Expand Up @@ -24,7 +24,7 @@ public function __construct()
$this->_client->setParameterGet('flags', 'J'); // JSON format
}

public function query($location, $country = null)
public function query($location, $country)
{

// Set the queries in priority order.
Expand All @@ -34,7 +34,7 @@ public function query($location, $country = null)
foreach ($queries as $query) {

// Make the request.
$this->_client->setParameterGet('q', trim($query));
$this->_client->setParameterGet('q', $query);
$response = json_decode($this->_client->request()->getBody());

// Continue to the next query if there are no results.
Expand Down
2 changes: 1 addition & 1 deletion Process/Geolocate.php
Expand Up @@ -30,7 +30,7 @@ public function __construct(Zend_Db_Adapter_Abstract $db)
public function geolocate($searchId)
{
// Find all geolocation services
$sql = 'SELECT * FROM geolocation_services WHERE id = 2';
$sql = 'SELECT * FROM geolocation_services';
$geolocationServices = $this->_db->fetchAll($sql);

// Iterate the geolocation services.
Expand Down

0 comments on commit 08d8bdc

Please sign in to comment.