<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="/stylesheets/rss.css" type="text/css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Plasmic Peach: UK Geocoding with Google and Graticule</title>
    <link>http://plasmicpeach.com/articles/2007/09/12/uk-geocoding-with-google-and-graticule</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>The Momo Web Blog</description>
    <item>
      <title>UK Geocoding with Google and Graticule</title>
      <description>&lt;p&gt;At long last, there&amp;#8217;s a good, free geocoding service for the UK.  Finding coordinates based on post code and/or address data has long been a free service in many countries.  But in the UK, the Royal Mail&amp;#8217;s control of post code data &amp;#8211; coupled with the ever-changing nature of that data &amp;#8211; has meant geocoding has been difficult or expensive.  Services like Postcode Anywhere and iShareMaps have provided equivalents to Google&amp;#8217;s geocoder, but for a fee.  That changed in early July 2007, when Google released geocoding for the UK.&lt;/p&gt;&lt;p&gt;If you&amp;#8217;re a Ruby programmer and you&amp;#8217;ve been doing geocoding, it&amp;#8217;s possible you&amp;#8217;ve been using a gem called Graticule.  This gem makes light work of plugging into various geocoding services and performing distance calculations.&lt;/p&gt;


	&lt;p&gt;Graticule includes a couple of classes for UK geocoding, one based on Postcode Anywhere and one based on Local Search Maps.  The first requires a Postcode Anywhere account, which is not free.  The second is free, but in my experience, not very reliable.&lt;/p&gt;


	&lt;p&gt;Neither matter anymore, though, since you can now just use the Google geocoder.  This post explains how to use Graticule to take advantage of Google&amp;#8217;s new UK geocoding service.&lt;/p&gt;


	&lt;p&gt;I&amp;#8217;m assuming you have a Google Maps &lt;span class="caps"&gt;API&lt;/span&gt; key, and that you have the Graticule gem installed.  (If you don&amp;#8217;t have Graticule, it&amp;#8217;s just &amp;#8220;gem install graticule&amp;#8221;.)&lt;/p&gt;


	&lt;p&gt;In the simplest case, using the UK geocoding service is easy right out of the box.  Just pass the address data to the standard Graticule Google service, and you&amp;#8217;re there:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;require 'rubygems'
require 'graticule'

&amp;gt; g = Graticule.service(:google).new GMAP_API_KEY
#=&amp;gt; #&amp;lt;Graticule::Geocoder::Google:0xb73959f0 @key=&amp;quot;xxxxxxxxxx&amp;quot;,
@url=#&amp;lt;URI::HTTP:0xfdb9cabfe URL:http://maps.google.com/maps/geo&amp;gt;&amp;gt;

&amp;gt; g.locate &amp;quot;NW1 8BX&amp;quot;
#=&amp;gt; #&amp;lt;Graticule::Location:0xb7367064 @longitude=-0.145855,
@region=&amp;quot;England&amp;quot;, @precision=:zip, @latitude=51.542272,
@locality=&amp;quot;London&amp;quot;, @country=&amp;quot;GB&amp;quot;, @street=nil, @postal_code=&amp;quot;NW1 8BX&amp;quot;&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;As you can see, the service correctly found the location in the UK.&lt;/p&gt;


	&lt;p&gt;But sometimes you don&amp;#8217;t have such complete data.  You might be using a partial post code, or a street address and no post code.  In those cases, the service might find a location in the wrong country&amp;#8212;the US, Canada, or Australia, for instance.&lt;/p&gt;


	&lt;p&gt;The easiest way to avoid that problem is to use the UK-specific geocoding service &lt;span class="caps"&gt;URL&lt;/span&gt;.  Graticule&amp;#8217;s Google geocoding class sets its &lt;span class="caps"&gt;URL&lt;/span&gt; to maps.google.com/maps/geo.  What we want is maps.google.co.uk/maps/geo.&lt;/p&gt;


	&lt;p&gt;When you call the method Graticule.service, it returns a geocoder class based on the symbol argument you pass in.  If you pass in :google, it returns the Graticule::Geocoder::Google class.  You could just as easily call Graticule::Geocoder::Google.new.  In either case, the way to get a UK-oriented geocoder is to make a new class that inherits from Graticule::Geocoder::Google, since the UK geocoding service is identical to the international one with the exception of the &lt;span class="caps"&gt;URL&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;In fact, the only method you need to write in the new class is the initialize method, which sets the &lt;span class="caps"&gt;URL&lt;/span&gt;.  Below is the code for the new class.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_ruby "&gt;&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;rubygems&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="ident"&gt;require&lt;/span&gt; &lt;span class="punct"&gt;'&lt;/span&gt;&lt;span class="string"&gt;graticule&lt;/span&gt;&lt;span class="punct"&gt;'&lt;/span&gt;
&lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Graticule&lt;/span&gt;
  &lt;span class="keyword"&gt;module &lt;/span&gt;&lt;span class="module"&gt;Geocoder&lt;/span&gt;
    &lt;span class="keyword"&gt;class &lt;/span&gt;&lt;span class="class"&gt;GoogleUk&lt;/span&gt; &lt;span class="punct"&gt;&amp;lt;&lt;/span&gt; &lt;span class="constant"&gt;Graticule&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Geocoder&lt;/span&gt;&lt;span class="punct"&gt;::&lt;/span&gt;&lt;span class="constant"&gt;Google&lt;/span&gt;
      &lt;span class="keyword"&gt;def &lt;/span&gt;&lt;span class="method"&gt;initialize&lt;/span&gt;&lt;span class="punct"&gt;(*&lt;/span&gt;&lt;span class="ident"&gt;args&lt;/span&gt;&lt;span class="punct"&gt;)&lt;/span&gt;
        &lt;span class="keyword"&gt;super&lt;/span&gt;
        &lt;span class="attribute"&gt;@url&lt;/span&gt; &lt;span class="punct"&gt;=&lt;/span&gt; &lt;span class="constant"&gt;URI&lt;/span&gt;&lt;span class="punct"&gt;.&lt;/span&gt;&lt;span class="ident"&gt;parse&lt;/span&gt;&lt;span class="punct"&gt;(&amp;quot;&lt;/span&gt;&lt;span class="string"&gt;http://maps.google.co.uk/maps/geo&lt;/span&gt;&lt;span class="punct"&gt;&amp;quot;)&lt;/span&gt;
      &lt;span class="keyword"&gt;end&lt;/span&gt;
    &lt;span class="keyword"&gt;end&lt;/span&gt;
  &lt;span class="keyword"&gt;end&lt;/span&gt;
&lt;span class="keyword"&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Put this code in the file &amp;#8220;google_uk.rb&amp;#8221;, in the directory graticule/geocoder somewhere in Ruby&amp;#8217;s include path.  For instance, if you&amp;#8217;re using Rails, you can put it in lib/graticule/geocoder.&lt;/p&gt;


	&lt;p&gt;Now you should be able to use the new UK-oriented geocoder as follows:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;&amp;gt; g = Graticule.service(:google_uk).new GMAP_API_KEY
#=&amp;gt; #&amp;lt;Graticule::Geocoder::GoogleUk:0xb734bcb0 @key=&amp;quot;xxxxxxxxxx&amp;quot;,
@url=#&amp;lt;URI::HTTP:0xfdb9a4b3e URL:http://maps.google.co.uk/maps/geo&amp;gt;&amp;gt;

&amp;gt; g.locate &amp;quot;NW1 8BX&amp;quot;
#=&amp;gt; #&amp;lt;Graticule::Location:0xb7367064 @longitude=-0.145855,
@region=&amp;quot;England&amp;quot;, @precision=:zip, @latitude=51.542272,
@locality=&amp;quot;London&amp;quot;, @country=&amp;quot;GB&amp;quot;, @street=nil, @postal_code=&amp;quot;NW1 8BX&amp;quot;&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;The UK geocoder works best if you pass it a string containing a complete postcode, or a street address (no apartment number or anything else) plus a partial post code.  Eg, &amp;#8220;AB1 3CD&amp;#8221;, or &amp;#8220;10 Whatever Street, &lt;span class="caps"&gt;AB1&lt;/span&gt;&amp;#8221;.&lt;/p&gt;


	&lt;p&gt;A good way to learn more about the Graticule gem is to explore the &lt;a href="http://rdoc.momoweb.co.uk/graticule"&gt;RDoc documentation&lt;/a&gt; and visit the &lt;a href="http://graticule.com"&gt;Graticule web site&lt;/a&gt;.&lt;/p&gt;</description>
      <pubDate>Wed, 12 Sep 2007 16:15:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8186bf85-754f-494c-871b-df50456a8f12</guid>
      <author>Momo</author>
      <link>http://plasmicpeach.com/articles/2007/09/12/uk-geocoding-with-google-and-graticule</link>
      <category>programming</category>
      <category>Ruby</category>
    </item>
  </channel>
</rss>
