Geocoder

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/ 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

Shandy N. 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/ 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

will get you a response like this with the lat & long for the city:
rdf:RDF
geo:Point
geo:long-117.191848</geo:long>
geo:lat32.751575</geo:lat>
</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.

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
def initialize(db)
@db = db
@rows = db.execute( “select * from city_names”)
end

def update_coords
@link = “”
@rows.each do |item|
@link = “#{item[1]},#{item[2]},#{item[3]}”
open(“http://brainoff.com/worldkit/geocoder/rest/?city=#{@link}”)
do |f|
sd = f.read
@long = sd.scan(/long>(.)</)
@lat = sd.scan(/lat>(.
)</)
end
@db.execute( “UPDATE city_names SET long =’#{@long[0][0]}’ WHERE
id=#{item[0]}”)
@db.execute( “UPDATE city_names SET latitude =’#{@lat[0][0]}’ WHERE
id=#{item[0]}”)
end
end
end

geo = Geocoder.new(database)
geo.update_coords

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

Here is the same thing, but using the geocoder gem that Shandy asked
about
http://gist.github.com/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
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
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
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”)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs