{"id":296,"date":"2005-12-01T21:48:59","date_gmt":"2005-12-02T03:48:59","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=296"},"modified":"2005-12-01T21:48:59","modified_gmt":"2005-12-02T03:48:59","slug":"set-up-your-own-geocode-service","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/296","title":{"rendered":"Set up your own geocode service"},"content":{"rendered":"<p><em>Update, 2\/9\/06: this post only outlines how to set up a geocode engine for the United States.  I don&#8217;t know how to do it for any other countries.<\/em><\/p>\n<p><a href='http:\/\/geocoder.us'>Geocoder.us<\/a> provides you with a REST based geocoding service, but their commercial services are <a href='http:\/\/geocoder.us\/terms.shtml'>not free<\/a>.  Luckily, the data they use is public domain, and there are some helpful perl modules which make setting up your own service a snap.  This post steps you through setting up your own geocoding service (for the USA), based on public domain census data.  You end up with a Google map of any address in the USA, but of course the lat\/long you find could be used with any mapping service.<\/p>\n<p>First, get the data.<\/p>\n<div><code><\/p>\n<pre>$ wget -r -np -w 5 --random-wait ftp:\/\/www2.census.gov\/geo\/tiger\/tiger2004se\/ <\/pre>\n<p><\/code><\/div>\n<p>If you only want the data for one state, put the two digit state code at the end of the ftp:\/\/ url above (eg <code>ftp:\/\/www2.census.gov\/geo\/tiger\/tiger2004se\/CO\/<\/code> for Colorado&#8217;s data).<\/p>\n<p>Second, install the needed perl modules.  (I did this on cygwin and linux, and it was a snap both times.  See <a href='http:\/\/modperlbook.org\/html\/ch03_09.html'>this page for instructions on installing to a nonstandard location with the CPAN module<\/a> and don&#8217;t forget to set your <code>PERL5LIB<\/code> variable.)<\/p>\n<div><code><\/p>\n<pre>$ perl -MCPAN -e shell<\/code>\n<code>cpan&gt; install S\/SD\/SDERLE\/Geo-Coder-US-1.00.tar.gz<\/code>\n<code>cpan&gt; install S\/SM\/SMPETERS\/Archive-Zip-1.16.tar.gz<\/code>\n<\/pre>\n<p><\/code><\/div>\n<p>Third, import the tiger data (this code comes from the <a href='http:\/\/search.cpan.org\/~sderle\/Geo-Coder-US\/US.pm'>Geo::Coder::US perldoc<\/a>, and took 4.5 hours to execute on a 2.6ghz pentium4 with 1 gig of memory).  Note that if you install via the CPAN module as shown above, the <code>import_tiger_zip.pl<\/code> file is under <code>~\/.cpan\/<\/code>:<\/p>\n<div><code><\/p>\n<pre>$ find www2.census.gov\/geo\/tiger\/tiger2004se\/CO\/ -name \\*.zip \n  | xargs -n1 perl \/path\/to\/import_tiger_zip.pl geocoder.db<\/pre>\n<p><\/code><\/div>\n<p>Now you&#8217;re ready to find the lat\/long of an address.  Find one that you&#8217;d like to map, like say, the <a href='http:\/\/www.revenue.state.co.us\/TPS_Dir\/wrap.asp?incl=forms_download'>Colorado Dept of Revenue: 1375 Sherman St, Denver, CO<\/a>.<\/p>\n<div><code><\/p>\n<pre>\n$ perl -MGeo::Coder::US -e 'Geo::Coder::US-&gt;set_db( \"geocoder.db\" );\nmy($res) = Geo::Coder::US-&gt;geocode(\"1375 Sherman St, Denver, CO\" ); \nprint \"$res-&gt;{lat}, $ res-&gt;{long}\\n\\n\";'\n\n39.691702, -104.985361\n<\/pre>\n<p><\/code><\/div>\n<p>And then you can <a href='http:\/\/maps.google.com\/maps?hl=en&amp;output=html&amp;q=39.691702%2C+-104.985361&amp;btnG=Search'>map it with Google maps<\/a>.<\/p>\n<p>Now, why wouldn&#8217;t you just use Yahoo!&#8217;s service (which provides geocoding and mapping APIs)?  Perhaps you like Google&#8217;s maps better.  Perhaps you don&#8217;t want to use a mapping service at all, you just want to find lat\/longs without reaching out over the network.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update, 2\/9\/06: this post only outlines how to set up a geocode engine for the United States. I don&#8217;t know how to do it for any other countries. Geocoder.us provides [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":0,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}