Add initial, basic implementation for a Geonames.org geocoding client.
authorMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 20 Dec 2009 21:01:19 +0000 (16:01 -0500)
committerMichael J. Rubinsky <mrubinsk@horde.org>
Sun, 20 Dec 2009 21:01:19 +0000 (16:01 -0500)
framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php [new file with mode: 0644]
framework/Ajax/package.xml

diff --git a/framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php b/framework/Ajax/lib/Horde/Ajax/Imple/Geocoder/Geonames.php
new file mode 100644 (file)
index 0000000..bdc3864
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+/**
+ * Geocode client for the Geonames API.
+ *
+ * Copyright 2009 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (GPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
+ *
+ * @author  Michael J. Rubinsky <mrubinsk@horde.org>
+ * @package Horde_Ajax
+ */
+class Horde_Ajax_Imple_Geocoder_Geonames extends Horde_Ajax_Imple_Base
+{
+    /**
+     * Constructor.
+     *
+     * @param array $params  Configuration parameters.
+     * <pre>
+     * 'triggerId' => (string) [optional] TODO
+     * </pre>
+     */
+    public function __construct($params)
+    {
+        parent::__construct($params);
+    }
+
+    /**
+     * Attach the object to a javascript event.
+     */
+    public function attach()
+    {
+    }
+
+
+    /**
+     * Handle the geocoding request.
+     *
+     * @TODO: For reverse requests come up with a reasonable algorithm for
+     *        checking if we have a lat/lng in the US since the
+     *        findNearestAddress method is US only. If non-us, fallback to a
+     *        findNearest or findPostalcode or similar request. Also will need
+     *        to normalize the various response structures.
+     *
+     * $args['locations'] will trigger a forward geocoding request.
+     * $args['lat'] and $args['lon'] will trigger a reverse geocoding request.
+     *
+     * @see framework/Ajax/lib/Horde/Ajax/Imple/Horde_Ajax_Imple_Base#handle($args, $post)
+     * @throws Horde_Exception
+     */
+    public function handle($args, $post)
+    {
+        if ($args['location']) {
+            $url = Horde_Util::addParameter('http://ws.geonames.org/searchJSON', 'q', $args['location']);
+        } elseif (!empty($args['lat']) && !empty($args['lon'])) {
+            $url = Horde_Util::addParameter('http:/ws.geonames.org/findNearestJSON', array('lat' => $args['lat'], 'lng' => $args['lon']));
+        }
+        $client = new Horde_Http_Client();
+        try {
+            $response = $client->get($url);
+        } catch (Horde_Http_Exception $e) {
+            throw new Horde_Exception($e);
+        }
+        return array('status' => 200,
+                     'results' => $response->getBody());
+    }
+
+}
index c709220..39e2dfa 100644 (file)
@@ -36,6 +36,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
        <file name="AutoCompleter.php" role="php" />
        <file name="Base.php" role="php" />
        <file name="SpellChecker.php" role="php" />
+       <dir name="Geocoder">
+        <file name="Geonames.php" role="php" />
+       </dir>
       </dir> <!-- /lib/Horde/Ajax/Imple -->
       <file name="Imple.php" role="php" />
      </dir> <!-- /lib/Horde/Ajax -->
@@ -73,6 +76,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Ajax/Imple/Base.php" as="Horde/Ajax/Imple/Base.php" />
    <install name="lib/Horde/Ajax/Imple/SpellChecker.php" as="Horde/Ajax/Imple/SpellChecker.php" />
    <install name="lib/Horde/Ajax/Imple.php" as="Horde/Ajax/Imple.php" />
+   <install name="lib/Horde/Ajax/Imple/Geocoder/Geonames.php" as="Horde/Ajax/Imple/Geocoder/Geonames.php" />
   </filelist>
  </phprelease>
  <changelog/>