Geocoder

Discussion in 'Ruby' started by Shandy Nantz, Mar 17, 2010.

  1. Shandy Nantz

    Shandy Nantz Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Shandy Nantz, Mar 17, 2010
    #1
    1. Advertising

  2. Shandy Nantz

    Peter Song Guest

    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/ 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:lat>32.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.
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Song, Mar 19, 2010
    #2
    1. Advertising

  3. Shandy Nantz

    Peter Song Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
     
    Peter Song, Mar 19, 2010
    #3
  4. Shandy Nantz

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hi, here is an ActiveRecord based solution 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


    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")
     
    Josh Cheek, Mar 20, 2010
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JK
    Replies:
    6
    Views:
    248
    Martin Honnen
    Sep 21, 2005
  2. Paul Smith

    [ANN] Geocoder 0.1.0

    Paul Smith, Feb 8, 2006, in forum: Ruby
    Replies:
    12
    Views:
    181
    tsuraan
    Feb 10, 2006
  3. Eric Hodel

    [ANN] geocoder-us 1.1.1 Released

    Eric Hodel, Nov 28, 2006, in forum: Ruby
    Replies:
    0
    Views:
    109
    Eric Hodel
    Nov 28, 2006
  4. Pablo Q.

    About Native geocoder

    Pablo Q., Nov 7, 2008, in forum: Ruby
    Replies:
    0
    Views:
    89
    Pablo Q.
    Nov 7, 2008
Loading...

Share This Page