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:
- PHP 5+
- CouchDB
- A Google API Key (get one here)
Usage
Using the GeoCouch is dead simple.
Grab the file here.
conf parameters!
*/
$GeoCouch = new GeoCouch();
/*
* The all-in-one method.
* This geocodes the string and writes it to CouchDB
* The second parameter is any other fields other
* than the Google data that you want to save along
* with this document.
*
* NOTE: if this address already exists in CouchDB
* a new revision is created.
*
* Returns the CouchDB response, i.e.:
* {"ok" : true, "rev":"3825793742", "id" : "dallas-tx" }
*/
$GeoCouch->save('Dallas, TX', array('custom_field' => 'value'));
/*
* Simply geo coding.
* Does not write to CouchDB.
* Returns an Google Geocoded Object.
*/
$geoObj = $GeoCouch->geoCode('Dallas, TX');
/*
* Write some Geo JSON to CouchDB.
* First parameter is a unique name for the data
* Second parameter is the JSON - in
* this case the json_encoded $geoObj from above.
*/
$GeoCouch->put('Dallas, TX', json_encode($geoObj));
/*
* Get some existing geo data
*/
$geoObj = $GeoCouch->get('Dallas, TX');
?>














Pingback: GeoCouch in CakePHP
Pingback: Dev Blog AF83 » Blog Archive » Veille technologique : Frameworks, HTML5, applis web et desktop