? modules/geocode_widget/tests
Index: geocode.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/geocode/geocode.module,v
retrieving revision 1.7
diff -u -p -r1.7 geocode.module
--- geocode.module	28 Jan 2010 17:12:25 -0000	1.7
+++ geocode.module	9 Mar 2010 12:23:16 -0000
@@ -1,6 +1,11 @@
 <?php // $Id: geocode.module,v 1.7 2010/01/28 17:12:25 vauxia Exp $
 
 /**
+ * @file
+ *  Geocode - basic API functions and hook implementations.
+ */
+
+/**
  * The Geocode API call.
  */
 function geocode($handler, $input, $return = 'point', $options = array()) {
@@ -10,8 +15,8 @@ function geocode($handler, $input, $retu
     $info = geocode_handler_info();
     if ($h = $info[$handler]) {
       // Load the geocoder's file.
-      if (isset($h['file path']) && isset($h['path'])) {
-        require_once $h['path'] . '/' . $h['file'];
+      if (isset($h['file path']) && isset($h['file'])) {
+        require_once $h['file path'] . '/' . $h['file'];
       }
 
       // Instantiate a class for it.
@@ -23,7 +28,7 @@ function geocode($handler, $input, $retu
 
   if ($handler->geocode($input, $options)) {
     list ($type, $item) = explode(':', $return);
-    return $handler->get_result($type, $item);
+    return $handler->getResult($type, $item);
   }
 }
 
@@ -40,7 +45,7 @@ function geocode_handler_info($field_typ
   $return = $handlers;
 
   if ($field_type) {
-    foreach($return as $i => $handler) {
+    foreach ($return as $i => $handler) {
       if (!in_array($field_type, $handler['field types'])) {
         unset($return[$i]);
       }
@@ -67,11 +72,11 @@ function geocode_geocode_handler_info() 
   $handlers = array();
 
   // A default geocoding handler.
-  $handlers['geocode_google'] = array(
+  $handlers['GeocodeGoogle'] = array(
     'label' => t('Google API'),
     'callback' => 'geocode_handler_google',
     'module' => 'geocode',
-    'file' => 'geocode.inc',
+    'file' => 'GeocodeGoogle.inc',
     'file path' => drupal_get_path('module', 'geocode') .'/includes',
     'field types' => array('postal_field', 'postal', 'text'),
     'element types' => array('postal', 'textfield', 'textarea'),
@@ -109,7 +114,7 @@ function geocode_gis_input_info($gis_typ
   foreach (geocode_handler_info() as $handler => $info) {
     // Only deal with handlers that attach to a form element.
     if (!isset($info['element types']) || !isset($info['return types']['geo'])) {
-       continue;
+      continue;
     }
 
     foreach ($info['element types'] as $type) {
Index: includes/GeocodeBasic.inc
===================================================================
RCS file: includes/GeocodeBasic.inc
diff -N includes/GeocodeBasic.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/GeocodeBasic.inc	9 Mar 2010 12:23:16 -0000
@@ -0,0 +1,148 @@
+<?php
+// $Id: geocode.inc,v 1.5 2009/11/04 21:07:31 vauxia Exp $
+
+/**
+ * @file
+ *  Definition of GeocodeInterface and GeocodeBasic class.
+ */
+
+/**
+ * Defines interface for a geocoder.
+ */
+interface GeocodeInterface {
+  /**
+   * Return a geocoded result.
+   *
+   * @todo is it preferable to use the individual explict get functions?
+   *
+   * @param $type
+   *  point, linestring, wkt, fieldtext, etc.
+   * @param $item
+   *  (optional) for types where more than one part, or
+   *  version could be returned. For example GeocodeInterface::getWkt().
+   * @return
+   *  string or array from related get$type function.
+   */
+  public function getResult($type, $item);
+
+  /**
+   * Return a point.
+   *
+   * @return
+   *  array('lat' => int, 'lon' => int, 'alt' => int).
+   */
+  public function getPoint();
+
+  /**
+   * Return a linestring.
+   *
+   * @return
+   *  array(array('lat' => int, 'lon' => int'), ...).
+   */
+  public function getLinestring();
+
+  /**
+   * Return WKT (Simple Features).
+   *
+   * @param $item
+   *  Type of Simple Feature.
+   * @return
+   *  String representation of Simple Feature.
+   */
+  public function getWkt($item);
+
+  /**
+   * Return array for cck field_text.
+   *
+   * @todo shouldn't this be handled within geocode_widget.
+   *
+   * @param $item.
+   *  Part of a geocoded address to return.
+   * @return
+   *  CCK field value with string part of address.
+   */
+  public function getField_text($item);
+
+  /**
+   * Return array for cck field_postal.
+   *
+   * @todo shouldn't this be handled within geocode_widget.
+   *
+   * @return
+   *  CCK field values for postal address.
+   */
+  public function getField_postal();
+
+  /**
+   * Geocode input.
+   *
+   * @todo better to thow an error based on HTTP request result,
+   *   or xml parsing/json_decoding.
+   *
+   * @return
+   *  Bool true on success.
+   */
+  public function geocode($input, $options);
+}
+
+/**
+ * Basic geocoding class. This lacks the acutal geocoding request,
+ * which varies from api to api, and thus setting of individual properties.
+ */
+abstract class GeocodeBasic implements GeocodeInterface {
+  protected $result;
+  protected $point,  $box;
+  protected $street1, $street2, $city, $state, $zip, $country, $countryName;
+
+  /**
+   * Internal method to save geocode result in class properties.
+   *
+   * @param $value
+   *  Result of geocoding.
+   */
+  protected function setResult($value) {
+    $this->$result = $value;
+  }
+
+  function getResult($type, $item = NULL) {
+    $func = 'get' . ucfirst($type);
+    if (method_exists($this, $func)) {
+      return $this->$func($item);
+    }
+  }
+
+  function getPoint() {
+    return $this->point;
+  }
+
+  function getLinestring() {
+    return $this->linestring;
+  }
+
+  function getWkt($item) {
+    // TODO figure out how to make any geo data type (shape, line) into a point.
+    switch ($item) {
+      case 'point':
+        return 'POINT(' . $this->point['lon'] . ' ' . $this->point['lat'] . ')';
+      case 'linestring':
+        $wkt = 'LINESTRING(';
+        foreach ($this->linestring as $point) {
+          $wkt .= $point['lon'] .' '. $point['lat'] .', ';
+        }
+        $wkt = substr($wkt, 0, -2) . ')';
+        return $wkt;
+    }
+  }
+
+  function getField_text($item) {
+    return array('value' => $this->$item);
+  }
+
+  function getField_postal() {
+    $postal = array();
+    foreach (array('street1', 'street2', 'city', 'state', 'zip', 'country') as $item) {
+      if (isset($this->$item)) $postal[$item] = $this->$item;
+    }
+    return $postal;
+  }
+}
Index: includes/GeocodeGoogle.inc
===================================================================
RCS file: includes/GeocodeGoogle.inc
diff -N includes/GeocodeGoogle.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/GeocodeGoogle.inc	9 Mar 2010 12:23:16 -0000
@@ -0,0 +1,60 @@
+<?php
+// $Id: geocode.inc,v 1.5 2009/11/04 21:07:31 vauxia Exp $
+
+/**
+ * @file
+ *  Geocoding class implementation using Google API.
+ */
+
+require_once(dirname(__FILE__) . '/GeocodeBasic.inc');
+
+/**
+ * Implementation of abstract GeocodeBasic for google geocoding.
+ */
+class GeocodeGoogle extends GeocodeBasic {
+
+  protected function setResult($result) {
+    $this->result = $result;
+    if (isset($result->AddressDetails)) {
+      $addr = $result->AddressDetails->Country;
+      $this->countryName = $addr->CountryName;
+      $this->country = $addr->CountryNameCode;
+      $this->state = $addr->AdministrativeArea->AdministrativeAreaName;
+      $this->city = $addr->AdministrativeArea->Locality->LocalityName;
+      $this->street1 = $addr->AdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
+      $this->zip = $addr->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
+    }
+
+    if (isset($result->Point)) {
+      $point = (array) $result->Point->coordinates;
+      $this->point = array(
+        'type' => 'point', 'lat' => $point[1], 'lon' => $point[0], 'alt' => $point[2],
+      );
+    }
+    if (isset($result->ExtendedData->LatLonBox)) {
+      $this->box = (array) $result->ExtendedData->LatLonBox;
+    }
+  }
+
+  function geocode($input, $options) {
+    if (is_array($input)) $input = join(',', $input);
+    $url = "http://maps.google.com/maps/geo?";
+    $url .= 'q='. urlencode($input);
+
+    // Deliberately reusing the GMAP key, still pending an interface.
+    global $conf;
+    if ($key = variable_get('googlemap_api_key', '')) {
+      $url .= '&key='. check_plain($key);
+    }
+    $url .= '&sensor=false&output=json&oe=utf-8';
+    $ret = drupal_http_request($url);
+    $result = json_decode($ret->data);
+
+    if ($result->Status->code == '200') {
+      $this->setResult($result->Placemark[0]);
+      return TRUE;
+    }
+
+    return FALSE;
+  }
+}
Index: includes/geocode.inc
===================================================================
RCS file: includes/geocode.inc
diff -N includes/geocode.inc
--- includes/geocode.inc	4 Nov 2009 21:07:31 -0000	1.5
+++ /dev/null	1 Jan 1970 00:00:00 -0000
@@ -1,110 +0,0 @@
-<?php // $Id: geocode.inc,v 1.5 2009/11/04 21:07:31 vauxia Exp $
-
-class geocode {
-  var $result;
-
-  var $point;
-  var $box;
-  var $street1;
-  var $street2;
-  var $city;
-  var $state;
-  var $zip;
-  var $country;
-  var $country_name;
-
-  function set_result($value) {
-    $this->$result = $value;
-  }
-
-  function get_result($type, $item = NULL) {
-    $func = 'get_'. $type;
-    if (method_exists($this, $func)) {
-      return $this->$func($item);
-    }
-  }
-
-  function get_point() {
-    return $this->point;
-  }
-
-  function get_linestring() {
-    return $this->linestring;
-  }
-
-  function get_wkt($item) {
-    // TODO figure out how to make any geo data type (shape, line) into a point.
-    switch ($item) {
-      case 'point':
-        return 'POINT('. $this->point['lon'].' '. $this->point['lat'] .')';
-
-      case 'linestring':
-        $wkt = 'LINESTRING(';
-        foreach ($this->linestring as $point) {
-          $wkt .= $point['lon'] .' '. $point['lat'] .', ';
-        }
-        $wkt = substr($wkt, 0, -2) .')';
-        return $wkt;
-    }
-  }
-
-  function get_field_text($item) {
-    return array('value' => $this->$item);
-  }
-
-  function get_field_postal() {
-    $postal = array();
-    foreach (array('street1', 'street2', 'city', 'state', 'zip', 'country') as $item) {
-      if (isset($this->$item)) $postal[$item] = $this->$item;
-    }
-    return $postal;
-  }
-}
-
-class geocode_google extends geocode {
-
-  function set_result($result) {
-    $this->result = $result;
-    if (isset($result->AddressDetails)) {
-      $addr = $result->AddressDetails->Country;
-      $this->country_name = $addr->CountryName;
-      $this->country = $addr->CountryNameCode;
-      $this->state = $addr->AdministrativeArea->AdministrativeAreaName;
-      $this->city = $addr->AdministrativeArea->Locality->LocalityName;
-      $this->street1 = $addr->AdministrativeArea->Locality->Thoroughfare->ThoroughfareName;
-      $this->zip = $addr->AdministrativeArea->Locality->PostalCode->PostalCodeNumber;
-    }
-
-    if (isset($result->Point)) {
-      $point = (array) $result->Point->coordinates;
-      $this->point = array(
-        'type' => 'point', 'lat' => $point[1], 'lon' => $point[0], 'alt' => $point[2],
-      );
-    }
-    if (isset($result->ExtendedData->LatLonBox)) {
-      $this->box = (array) $result->ExtendedData->LatLonBox;
-    }
-  }
-
-  function geocode($input, $options) {
-    if (is_array($input)) $input = join(',', $input);
-    $url = "http://maps.google.com/maps/geo?";
-    $url .= 'q='. urlencode($input);
-
-    // Deliberately reusing the GMAP key, still pending an interface.
-    global $conf;
-    if ($key = variable_get('googlemap_api_key', '')) {
-      $url .= '&key='. check_plain($key);
-    }
-    $url .= '&sensor=false&output=json&oe=utf-8';
-    $ret = drupal_http_request($url);
-    $result = json_decode($ret->data);
-
-    if ($result->Status->code == '200') {
-      $this->set_result($result->Placemark[0]);
-      return TRUE;
-    }
-
-    return FALSE;
-  }
-}
Index: includes/modules/filefield.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/geocode/includes/modules/filefield.inc,v
retrieving revision 1.3
diff -u -p -r1.3 filefield.inc
--- includes/modules/filefield.inc	18 Jun 2009 01:58:59 -0000	1.3
+++ includes/modules/filefield.inc	9 Mar 2010 12:23:16 -0000
@@ -1,11 +1,13 @@
 <?php // $Id: filefield.inc,v 1.3 2009/06/18 01:58:59 vauxia Exp $
 
+require_once(drupal_get_path('module', 'geocode') . '/includes/GeocodeBasic.inc');
+
 /**
  * Implementation of hook_geocode_handler_info() on behalf of filefield.
  */
 function filefield_geocode_handler_info() {
   return array(
-    'geocode_filefield' => array(
+    'GeocodeFilefield' => array(
       'label' => t('File field'),
       'module' => 'geocode',
       'file' => 'filefield.inc',
@@ -18,7 +20,7 @@ function filefield_geocode_handler_info(
   );
 }
 
-class geocode_filefield extends geocode {
+class GeocodeFilefield extends GeocodeBasic {
 
   /**
    * Geocode from GPX files. (and hopefully additional formats, e.g. KML, ...)
@@ -34,15 +36,15 @@ class geocode_filefield extends geocode 
         $gpx = New DOMDocument();
         $gpx->load($file['filepath']);
         if ($points = gpx_trackpoints($gpx)) {
-          $this->set_result($points);
+          $this->setResult($points);
           return TRUE;
         }
       }
     }
     return FALSE;
-  }
+  } 
 
-  function set_result($data) {
+  function setResult($data) {
 
     $this->result = $data;
     $this->linestring = $this->result['data'];
Index: includes/modules/imagefield.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/geocode/includes/modules/imagefield.inc,v
retrieving revision 1.5
diff -u -p -r1.5 imagefield.inc
--- includes/modules/imagefield.inc	18 Jun 2009 01:58:59 -0000	1.5
+++ includes/modules/imagefield.inc	9 Mar 2010 12:23:16 -0000
@@ -1,5 +1,7 @@
 <?php // $Id: imagefield.inc,v 1.5 2009/06/18 01:58:59 vauxia Exp $
 
+require_once(drupal_get_path('module', 'geocode') . '/includes/GeocodeBasic.inc');
+
 /**
  * Implementation of hook_geocode_handler_info() on behalf of imagefield.
  */
@@ -7,7 +9,7 @@ function imagefield_geocode_handler_info
   $handlers = array();
   // This functionality requires PHP's exif module
   if (function_exists('exif_read_data')) {
-    $handlers['geocode_imagefield'] = array(
+    $handlers['GeocodeImagefield'] = array(
       'label' => t('Image field'),
       'module' => 'geocode',
       'file' => 'imagefield.inc',
@@ -21,17 +23,17 @@ function imagefield_geocode_handler_info
   return $handlers;
 }
 
-class geocode_imagefield extends geocode {
+class GeocodeImagefield extends GeocodeBasic {
   function geocode($file) {
     if ($file['filepath'] && $data = exif_read_data($file['filepath'])) {
       if (!isset($data['GPSLatitudeRef'])) return FALSE;
-      $this->set_result($data);
+      $this->setResult($data);
       return TRUE;
     }
     return FALSE;
   }
 
-  function set_result($data) {
+  function setResult($data) {
     
     $this->result = $data;
     $this->point = array(
