<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hungry Machine &#187; geoloc</title>
	<atom:link href="http://blog.hungrymachine.com/tag/geoloc/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.hungrymachine.com</link>
	<description>The guys behind LivingSocial</description>
	<lastBuildDate>Sun, 25 Oct 2009 15:08:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Super fast IP to lat/lng in Rails</title>
		<link>http://blog.hungrymachine.com/2007/10/22/super-fast-ip-to-lat-lng-in-rails/</link>
		<comments>http://blog.hungrymachine.com/2007/10/22/super-fast-ip-to-lat-lng-in-rails/#comments</comments>
		<pubDate>Mon, 22 Oct 2007 15:33:00 +0000</pubDate>
		<dc:creator>aaron</dc:creator>
				<category><![CDATA[All]]></category>
		<category><![CDATA[geoloc]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">blog.hungrymachine.com/2008/11/13/super-fast-ip-to-lat-lng-in-rails</guid>
		<description><![CDATA[In building the eye candy demo for the Graphing Social conference, I needed a quick way to geo-locate users by IP address.  For Rails, GeoKit is an awesome plugin. It supports a list of providers, and overlays distance calculations, before_filter helpers, all sorts of good stuff.
It uses hostip.info to do IP to lat/lng, using [...]]]></description>
			<content:encoded><![CDATA[<p>In building the <a href="http://blog.hungrymachine.com/2007/10/19/the-importance-of-eye-candy-and-no-not-the-kind-you-re-thinking">eye candy demo</a> for the Graphing Social conference, I needed a quick way to geo-locate users by IP address.  For Rails, <a href="http://geokit.rubyforge.org/">GeoKit</a> is an awesome plugin. It supports a list of providers, and overlays distance calculations, before_filter helpers, all sorts of good stuff.<br/></p>
<p>It uses <a href="http://www.hostip.info/">hostip.info</a> to do IP to lat/lng, using their RESTful interface:</p>
<div class="CodeRay">
<div class="code">
<pre>http<span style="color:#A60">:/</span>/api.hostip.info/get_html.php?ip=<span style="color:#60E; font-weight:bold">12.215</span>.<span style="color:#60E; font-weight:bold">42.19</span>&amp;position=<span style="color:#038; font-weight:bold">true</span>
  <span style="color:#036; font-weight:bold">Country</span>: <span style="color:#036; font-weight:bold">UNITED</span> <span style="color:#036; font-weight:bold">STATES</span> (<span style="color:#036; font-weight:bold">US</span>)
  <span style="color:#036; font-weight:bold">City</span>: <span style="color:#036; font-weight:bold">Sugar</span> <span style="color:#036; font-weight:bold">Grove</span>, <span style="color:#036; font-weight:bold">IL</span>
  <span style="color:#036; font-weight:bold">Latitude</span>: <span style="color:#60E; font-weight:bold">41.7696</span>
  <span style="color:#036; font-weight:bold">Longitude</span>: <span style="color:#60E; font-weight:bold">-88.4588</span></pre>
</div>
</div>
<p><br/><br />
This is great, but its just too darn slow to geolocate 12 users a second. All I needed was a FAST ip to lat/long lookup mechanism. Luckily, HostInfo provides the raw data. Here&#8217;s how I built super fast GeoLoc by IP method.<br />
<br/><br/><br />
Download, create a DB, and import the data.</p>
<div class="CodeRay">
<div class="code">
<pre>wget http<span style="color:#A60">:/</span>/hostip.ww.com/hostip_current.sql.gz
gunzip hostip_current.sql.gz
mysqladmin -uroot create hostip
mysql -uroot hostip &lt; hostip_current.sql</pre>
</div>
</div>
<p><br/><br/><br />
Create a simple HostIp class.  Note:  I need to dynamically build this query b/c the data is sharded across tables by the A class of the IP.</p>
<div class="CodeRay">
<div class="code">
<pre><span style="color:#080; font-weight:bold">class</span> <span style="color:#B06; font-weight:bold">Hostip</span> &lt; <span style="color:#036; font-weight:bold">ActiveRecord</span>::<span style="color:#036; font-weight:bold">Base</span>
  <span style="color:#080; font-weight:bold">def</span> <span style="color:#038; font-weight:bold">self</span>.geocode(ip)
    a,b,c,d = ip.split(<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">.</span><span style="color:#710">&quot;</span></span>)
    <span style="color:#038; font-weight:bold">self</span>.set_table_name <span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">hostip.ip4_</span><span style="background-color:#fff0f0"><span style="color:#710">#{</span>a<span style="color:#710">}</span></span><span style="color:#710">&quot;</span></span>
    ip = find(<span style="color:#A60">:first</span>, <span style="color:#A60">:select</span> =&gt; <span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">lat,lng</span><span style="color:#710">&quot;</span></span>,
                <span style="color:#A60">:joins</span> =&gt; <span style="background-color:#fff0f0"><span style="color:#710">%Q{</span><span style="color:#D20">INNER JOIN hostip.cityByCountry
                               ON hostip.ip4_</span><span style="background-color:#fff0f0"><span style="color:#710">#{</span>a<span style="color:#710">}</span></span><span style="color:#D20">.city = hostip.cityByCountry.city
                               AND hostip.ip4_</span><span style="background-color:#fff0f0"><span style="color:#710">#{</span>a<span style="color:#710">}</span></span><span style="color:#D20">.country = hostip.cityByCountry.country</span><span style="color:#710">}</span></span>,
                <span style="color:#A60">:conditions</span> =&gt; [<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">b = ? and c = ?</span><span style="color:#710">&quot;</span></span>, b,c])
    <span style="color:#080; font-weight:bold">if</span> ip
      [ip.lat, ip.lng]
    <span style="color:#080; font-weight:bold">else</span>
      [<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#710">&quot;</span></span>,<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#710">&quot;</span></span>]
    <span style="color:#080; font-weight:bold">end</span>
  <span style="color:#080; font-weight:bold">end</span>
<span style="color:#080; font-weight:bold">end</span> </pre>
</div>
</div>
<p><br/><br/></p>
<p>Usage</p>
<div class="CodeRay">
<div class="code">
<pre>&gt;&gt; <span style="color:#036; font-weight:bold">Hostip</span>.geocode(<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">4.2.2.2</span><span style="color:#710">&quot;</span></span>)
=&gt; [<span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">39.944</span><span style="color:#710">&quot;</span></span>, <span style="background-color:#fff0f0"><span style="color:#710">&quot;</span><span style="color:#D20">-105.062</span><span style="color:#710">&quot;</span></span>]</pre>
</div>
</div>
<p><br/><br/></p>
<p>Thats it!  Now you can geolocate by IP in your own datacenter.  <br/><br />
Thanks again to the guys at HostIp for sharing this data!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.hungrymachine.com/2007/10/22/super-fast-ip-to-lat-lng-in-rails/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
