Skip to content

Instantly share code, notes, and snippets.

@evandonovan
Created April 3, 2010 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandonovan/354149 to your computer and use it in GitHub Desktop.
Save evandonovan/354149 to your computer and use it in GitHub Desktop.
auto-updates geocodes using Google geocoder from location.module
<?php
// geocode_update.module
/**
* Implementation of hook_cron()
*
* Loads all the locations on the site which have invalid latitude/longitude,
* then uses the Google geocoder to update the latitude/longitude, if possible.
*/
function geocode_update_cron() {
//Include Google geocoder (necessary to update node latitude/longitude)
$google_geocoder = './'. drupal_get_path('module', 'location') . '/geocoding/google.inc';
include_once($google_geocoder);
$max_invalid_lid_query = 'select max(lid) from {location} loc where
((loc.latitude=0 and loc.longitude=0) or
(loc.latitude=180 and loc.longitude=360)) and
loc.lid in (select l.lid
from {location_instance} l, {node} n
where l.nid = n.nid)';
$max_invalid_lid = db_result(db_query($max_invalid_lid_query));
$starting_lid = variable_get('geocode_update_starting_lid', 0); // get starting lid from {variables} table
/* if the maximum invalid location is <= the one we're starting on,
then start over again */
if($max_invalid_lid <= $starting_lid) {
variable_set('geocode_update_starting_lid', 0);
$starting_lid = 0;
}
$items_per_run = 100; // arbitrary number - adjust to fit your needs
$invalid_lid_query = 'select * from {location} loc where
((loc.latitude=0 and loc.longitude=0) or
(loc.latitude=180 and loc.longitude=360)) and
(loc.lid in (select l.lid
from {location_instance} l, {node} n
where l.nid = n.nid)) and (loc.lid > %d) order by loc.lid asc';
$result = db_query_range($invalid_lid_query, $starting_lid, 0, $items_per_run);
while ($location = db_fetch_array($result)) {
$old_latitude = $location['latitude'];
$old_longitude = $location['longitude'];
if (!empty($location)) {
$google_location = google_geocode_location($location);
}
// Waits for 250000 microseconds to prevent Google throttling requests
usleep(250000);
$latitude = $google_location['lat'];
$longitude = $google_location['lon'];
$lid = $location['lid'];
// save the lid just updated so we can resume later
variable_set('geocode_update_starting_lid', $lid);
if (!is_null($latitude) && !is_null($longitude) && !($latitude == 0 && $longitude == 0) && $lid) {
$updated = db_query("UPDATE {location} SET latitude = %f, longitude = %f, source = 1 WHERE lid = %d", $latitude, $longitude, $lid);
watchdog('geocode_update', 'Updated location %lid from %old_lat , %old_lon to %new_lat , %new_lon', array('%lid' => $lid, '%old_lat' => $old_latitude, '%old_lon' => $old_longitude, '%new_lat' => $latitude, '%new_lon' => $longitude), WATCHDOG_NOTICE);
}
else {
watchdog('geocode_update', 'Failed to update location %lid', array('%lid' => $lid), WATCHDOG_NOTICE);
}
}
}
/**
* Implements hook_action_info().
* Used by Rules to moderate or unmoderate content. (ead 8/24/09)
*/
function geocode_update_action_info() {
return array(
'geocode_update_lat_lon' => array(
'type' => 'node',
'description' => t('Update latitude/longitude'),
'configurable' => FALSE,
'hooks' => array(
'nodeapi' => array('presave'),
),
),
);
}
function geocode_update_lat_lon(&$node, $context = array()) {
//Include Google geocoder (necessary to update node latitude/longitude)
$google_geocoder = './'. drupal_get_path('module', 'location') . '/geocoding/google.inc';
include_once($google_geocoder);
$lid_query = 'select * from {location} loc where
(loc.lid in (select l.lid
from {location_instance} l, {node} n
where l.nid = %d))';
$result = db_query($lid_query, $node->nid);
while ($location = db_fetch_array($result)) {
$old_latitude = $location['latitude'];
$old_longitude = $location['longitude'];
if (!empty($location)) {
$google_location = google_geocode_location($location);
}
// Waits for 250000 microseconds to prevent Google throttling requests
usleep(250000);
$latitude = $google_location['lat'];
$longitude = $google_location['lon'];
$lid = $location['lid'];
// save the lid just updated so we can resume later
variable_set('geocode_update_starting_lid', $lid);
if (!is_null($latitude) && !is_null($longitude) && !($latitude == 0 && $longitude == 0) && $lid) {
$updated = db_query("UPDATE {location} SET latitude = %f, longitude = %f, source = 1 WHERE lid = %d", $latitude, $longitude, $lid);
watchdog('geocode_update', 'Updated location %lid from %old_lat , %old_lon to %new_lat , %new_lon', array('%lid' => $lid, '%old_lat' => $old_latitude, '%old_lon' => $old_longitude, '%new_lat' => $latitude, '%new_lon' => $longitude), WATCHDOG_NOTICE);
}
else {
watchdog('geocode_update', 'Failed to update location %lid', array('%lid' => $lid), WATCHDOG_NOTICE);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment