Avatar Brit Gardner’s Site

all the code that’s fit to printf()

Using CouchDB For Storing Google Geocoded JSON Data

Recently, I’ve been working on a project that requires a lot of geocoding.  Google provides a free service that takes any string as an input and returns a bunch of JSON encoded data if that string matches a physical location.  Check it out here.

Unfortunately, Google’s geocoding service is limited to 15k requests per day per IP.  Sounds like a lot, but for certain applications this limit can be reached very quickly.

What’s a good solution in these cases?  Why, how about use my favorite up-and-coming database, CouchDB, to store a repository of geo-data mined from Google?  Why use CouchDB?  As previously mentioned, google natively return JSON data for geocoding requests.  Perfect.  CouchDB just happens to store documents natively in JSON!

The following is a rough and tumble library I put together called GeoCouch that you can use to easily handle geocoded data in CouchDB.  This lib has a narrow scope right now - the requirements are:

  1. PHP 5+
  2. CouchDB
  3. A Google API Key (get one here)

Usage

Using the GeoCouch is dead simple.
Grab the file here.

  1.  
  2. <?php
  3.   require (‘geocouch.php’);
  4.  
  5.   /*
  6.    * Don’t forget to edit the $GeoCouch->conf parameters!
  7.    */
  8.   $GeoCouch = new GeoCouch();
  9.  
  10.   /*
  11.    * The all-in-one method.
  12.    * This geocodes the string and writes it to CouchDB
  13.    * The second parameter is any other fields other
  14.    * than the Google data that you want to save along
  15.    * with this document.
  16.    *
  17.    * NOTE: if this address already exists in CouchDB
  18.    * a new revision is created.
  19.    *
  20.    * Returns the CouchDB response, i.e.:
  21.    * {"ok" : true, "rev":"3825793742", "id" : "dallas-tx" }
  22.    */
  23.   $GeoCouch->save(‘Dallas, TX’, array(‘custom_field’ => ‘value’));
  24.  
  25.   /*
  26.    * Simply geo coding.  
  27.    * Does not write to CouchDB.
  28.    * Returns an Google Geocoded Object.
  29.    */
  30.   $geoObj = $GeoCouch->geoCode(‘Dallas, TX’);
  31.  
  32.   /*
  33.    * Write some Geo JSON to CouchDB.
  34.    * First parameter is a unique name for the data
  35.    * Second parameter is the JSON - in
  36.    * this case the json_encoded $geoObj from above.
  37.    */
  38.   $GeoCouch->put(‘Dallas, TX’, json_encode($geoObj));
  39.  
  40.   /*
  41.    * Get some existing geo data
  42.    */
  43.   $geoObj = $GeoCouch->get(‘Dallas, TX’);
  44. ?>
  45.  

CouchDB - Thinking Outside the Relational Database Box

Schema-Free Is The New Drug-Free

Lately I’ve been thinking a lot about this new-fangled contraption, Document Oriented Databases. Ok, so maybe these aren’t that new-fangled - Lotus Notes is a document oriented database and it’s been around for 20 years or so. But, they’re new-fangled to me; my experience to date has solely been with relational databases.

So what is a Document Oriented Database? Actually, just what it sounds like - a data store whose primary unit is a document. A document can be anything, usually a collection of key-value pairs that describe something, anything… it’s really that simple, really no different than a Microsoft Word document…

[Read more]

,

Passion Projects