# Geocoder

**URL:** <https://rubytalk.org/t/geocoder/57788>\
**Category:** ruby-talk\
**Created:** [17 March 2010 16:55 UTC](https://rubytalk.org/t/geocoder/57788 "2010-03-17T16:55:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shandy\_Nantz](https://avatars.discourse-cdn.com/v4/letter/s/7bcc69/32.png) [@Shandy\_Nantz](https://rubytalk.org/u/Shandy_Nantz)\
**Post date:** [17 March 2010 16:55 UTC](https://rubytalk.org/t/geocoder/57788/1 "2010-03-17T16:55:09Z")

</div>

I am trying to write a script that uses the rails-geocoder gem to grab  
the latitude and longitude so I can store those values in my own local  
table, but am having trouble getting anything to work or to find any  
decent documentation describing or even helping me accomplish what I  
need done. Does anybody have any suggestions? I have found this example:  
[http://geocoder.rubyforge.org/](http://geocoder.rubyforge.org/) that seems pretty straight forward. I  
have a list of cities and states and am just trying to loop through each  
and get the lat lon values. Thanks,

-S

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Peter\_Song](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Peter\_Song](https://rubytalk.org/u/Peter_Song)\
**Post date:** [19 March 2010 18:29 UTC](https://rubytalk.org/t/geocoder/57788/2 "2010-03-19T18:29:41Z")

</div>

Shandy Nantz wrote:

> I am trying to write a script that uses the rails-geocoder gem to grab  
> the latitude and longitude so I can store those values in my own local  
> table, but am having trouble getting anything to work or to find any  
> decent documentation describing or even helping me accomplish what I  
> need done. Does anybody have any suggestions? I have found this example:  
> [http://geocoder.rubyforge.org/](http://geocoder.rubyforge.org/) that seems pretty straight forward. I  
> have a list of cities and states and am just trying to loop through each  
> and get the lat lon values. Thanks,
> 
> -S

I think that geocoder is for full addresses. For just cities, you could  
use worldkit. Sending a request like so:  
[http://brainoff.com/worldkit/geocoder/rest/?city=San+Diego,CA,US](http://brainoff.com/worldkit/geocoder/rest/?city=San+Diego,CA,US)

will get you a response like this with the lat & long for the city:  
\<rdf:RDF\>  
&nbsp;&nbsp;\<geo:Point\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<geo:long\>-117.191848\</geo:long\>  
&nbsp;&nbsp;&nbsp;&nbsp;\<geo:lat\>32.751575\</geo:lat\>  
&nbsp;&nbsp;\</geo:Point\>  
\</rdf:RDF\>

You have to make sure there are no spaces in the city field. So cycling  
through your list of cities, you'd make sure to concatenate multi word  
cities with a '+' before sending the request.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Peter\_Song](https://avatars.discourse-cdn.com/v4/letter/p/48db29/32.png) [@Peter\_Song](https://rubytalk.org/u/Peter_Song)\
**Post date:** [19 March 2010 23:02 UTC](https://rubytalk.org/t/geocoder/57788/3 "2010-03-19T23:02:44Z")

</div>

My last post wasn't actually relevant to ruby so here's a little snippet  
using ruby, sqlite and worldkit. I'm just learning ruby myself so this  
might not be the best way to do this.

require 'open-uri'  
require 'sqlite3'

# make a table with two records, Dallas, TX and Lansing, MI  
database = SQLite3::Database.new( "cities.database" )  
database.execute( "create table city\_names (id INTEGER PRIMARY KEY, name  
TEXT, state TEXT, country TEXT, latitude NUMERIC, long NUMERIC);")  
database.execute( "insert into city\_names (name, state, country) values  
('Dallas', 'TX', 'US')")  
database.execute( "insert into city\_names (name, state, country) values  
('Lansing', 'MI', 'US')")

class Geocoder  
&nbsp;&nbsp;def initialize(db)  
&nbsp;&nbsp;&nbsp;&nbsp;@db = db  
&nbsp;&nbsp;&nbsp;&nbsp;@rows = db.execute( "select \* from city\_names")  
&nbsp;&nbsp;end

&nbsp;&nbsp;def update\_coords  
&nbsp;&nbsp;&nbsp;&nbsp;@link = ""  
&nbsp;&nbsp;&nbsp;&nbsp;@rows.each do |item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@link = "#{item[1]},#{item[2]},#{item[3]}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;open("[http://brainoff.com/worldkit/geocoder/rest/?city=#{@link}](http://brainoff.com/worldkit/geocoder/rest/?city=#%7B@link%7D)")  
do |f|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sd = f.read  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@long = sd.scan(/long\>(.\*)\</)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@lat = sd.scan(/lat\>(.\*)\</)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;@db.execute( "UPDATE city\_names SET long ='#{@long[0][0]}' WHERE  
id=#{item[0]}")  
&nbsp;&nbsp;&nbsp;&nbsp;@db.execute( "UPDATE city\_names SET latitude ='#{@lat[0][0]}' WHERE  
id=#{item[0]}")  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

geo = Geocoder.new(database)  
geo.update\_coords

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [20 March 2010 08:23 UTC](https://rubytalk.org/t/geocoder/57788/4 "2010-03-20T08:23:53Z")

</div>

Hi, here is an ActiveRecord based solution [http://gist.github.com/338506](http://gist.github.com/338506) It  
is based on Peter Song's solution. It also populates most of the 50 cities  
found at [http://www.infoplease.com/ipa/A0108477.html](http://www.infoplease.com/ipa/A0108477.html)

Here is the same thing, but using the geocoder gem that Shandy asked about

> <https://gist.github.com/JoshCheek/338523>

  
I also added some use cases for it that work with the data it populates into  
the db, such as  
dallas.distance\_to austin  
texas\_cities = City.find\_all\_by\_state('TX').map { |city| city.lat\_lon }  
Geocoder.geographic\_center texas\_cities  
near\_sacramento = City.find\_by\_name('Sacramento').nearbys(100)

You can see the gem homepage at [http://github.com/alexreisner/geocoder](http://github.com/alexreisner/geocoder)  
Wasn't very straightforward how to use this outside of Rails, but I  
eventually got it. (had to do a little code digging to understand the errors  
and query some mailing lists to find solutions)  
$ gem install rails-geocoder

Then get a google api key at [http://code.google.com/apis/maps/signup.html](http://code.google.com/apis/maps/signup.html)  
The license says your application must be available to the public, free or  
charge, otherwise you should be using  
[http://www.google.com/enterprise/earthmaps/maps.html](http://www.google.com/enterprise/earthmaps/maps.html)  
They want you to list a site your app will be available through. I gave them  
the URL of the gist the example is hosted at :)~

Anyway, a very simple program that correctly uses the gem to get results:  
require 'net/http'  
require 'rubygems'  
require 'active\_record'  
require 'geocoder'

Geocoder::GOOGLE\_MAPS\_API\_KEY = 'your key goes here'  
p Geocoder.fetch\_coordinates("Wichita,KS,US")
