Parsing geonames

Discussion in 'Ruby' started by 12 34, Jun 2, 2010.

  1. 12 34

    12 34 Guest

    A Ruby newbie having trouble getting results back from geonames

    #!/usr/bin/env ruby
    require "rubygems"
    require 'geonames'

    places_nearby = Geonames::WebService.find_nearby_place_name
    41.505928,-81.594582
    p places_nearby # --> [#<Geonames::Toponym:0x1021042f0
    @country_code="US", @feature_code_name=nil, @elevation=nil,
    @distance=0.4495, @alternate_names=nil, @feature_code="PPLX",
    @population=nil, @geoname_id="5161005", @longitude=-81.5981817,
    @feature_class_name=nil, @country_name="United States", @name="Little
    Italy", @latitude=41.5089406, @feature_class="P">]

    How do I get any of the values, e.g., country_name? places_nearby is a
    local variable.

    Thank you in advance for help. I've tried several things, but can't
    figure it out.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 2, 2010
    #1
    1. Advertising

  2. 12 34

    Sven Schott Guest

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

    Have you tried just calling the name of the instance var? Sometimes nice
    developer set up easy setters and getters in their implementations.

    e.g.

    places_nearby[0].country_code

    Just a thought. What are you trying that's failing? Have you looked through
    the docs at

    http://www.tbcn.ca/ruby_geonames

    On Wed, Jun 2, 2010 at 3:57 PM, 12 34 <> wrote:

    > A Ruby newbie having trouble getting results back from geonames
    >
    > #!/usr/bin/env ruby
    > require "rubygems"
    > require 'geonames'
    >
    > places_nearby = Geonames::WebService.find_nearby_place_name
    > 41.505928,-81.594582
    > p places_nearby # --> [#<Geonames::Toponym:0x1021042f0
    > @country_code="US", @feature_code_name=nil, @elevation=nil,
    > @distance=0.4495, @alternate_names=nil, @feature_code="PPLX",
    > @population=nil, @geoname_id="5161005", @longitude=-81.5981817,
    > @feature_class_name=nil, @country_name="United States", @name="Little
    > Italy", @latitude=41.5089406, @feature_class="P">]
    >
    > How do I get any of the values, e.g., country_name? places_nearby is a
    > local variable.
    >
    > Thank you in advance for help. I've tried several things, but can't
    > figure it out.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Sven Schott, Jun 2, 2010
    #2
    1. Advertising

  3. On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <> wrote:
    > 41.505928,-81.594582


    iota ~ % irb
    >> require 'geonames'

    => true
    >> places = Geonames::WebService.find_nearby_place_name(41.505928, -81.594582)

    => [#<Geonames::Toponym:0x000000027dd158 @name="Little Italy",
    @alternate_names=nil, @latitude=41.5089406, @longitude=-81.5981817,
    @geoname_id="5161005", @country_code="US", @country_name="United
    States", @feature_class="P", @feature_code="PPLX",
    @feature_class_name=nil, @feature_code_name=nil, @population=nil,
    @elevation=nil, @distance=0.4495>]
    >> places.first.country_name

    => "United States"

    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 2, 2010
    #3
  4. 12 34

    12 34 Guest

    Michael Fellinger wrote:
    > On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <> wrote:
    >> 41.505928,-81.594582

    >
    > iota ~ % irb
    >>> require 'geonames'

    > => true
    >>> places = Geonames::WebService.find_nearby_place_name(41.505928, -81.594582)

    > => [#<Geonames::Toponym:0x000000027dd158 @name="Little Italy",
    > @alternate_names=nil, @latitude=41.5089406, @longitude=-81.5981817,
    > @geoname_id="5161005", @country_code="US", @country_name="United
    > States", @feature_class="P", @feature_code="PPLX",
    > @feature_class_name=nil, @feature_code_name=nil, @population=nil,
    > @elevation=nil, @distance=0.4495>]
    >>> places.first.country_name

    > => "United States"


    Thanks. I had tried places.country_name among others. Why the "first"?

    places_nearby[0].country_code works too.

    The docs didn't suggest how to parse the result.

    Thank you for the answers, but I'm not sure why it's working. But I can
    proceed.

    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 2, 2010
    #4
  5. 12 34

    botp Guest

    On Wed, Jun 2, 2010 at 2:30 PM, 12 34 <> wrote:
    > Thanks. I had tried places.country_name among others. Why the "first"?
    > places_nearby[0].country_code works too.
    > The docs didn't suggest how to parse the result.


    try playing w ruby using irb,

    places_nearby.class
    places_nearby.each_with_index{|x,i| puts"#{i}) #{x}"}
    places_nearby[0]
    places_nearby.size
    places_nearby[0].class
    places_nearby[0].methods


    > Thank you for the answers, but I'm not sure why it's working. But I can
    > proceed.


    welcome to ruby.
    kind regards -botp
     
    botp, Jun 2, 2010
    #5
  6. 12 34

    Josh Cheek Guest

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

    On Wed, Jun 2, 2010 at 1:30 AM, 12 34 <> wrote:

    > Michael Fellinger wrote:
    > > On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <> wrote:
    > >> 41.505928,-81.594582

    > >
    > > iota ~ % irb
    > >>> require 'geonames'

    > > => true
    > >>> places = Geonames::WebService.find_nearby_place_name(41.505928,

    > -81.594582)
    > > => [#<Geonames::Toponym:0x000000027dd158 @name="Little Italy",
    > > @alternate_names=nil, @latitude=41.5089406, @longitude=-81.5981817,
    > > @geoname_id="5161005", @country_code="US", @country_name="United
    > > States", @feature_class="P", @feature_code="PPLX",
    > > @feature_class_name=nil, @feature_code_name=nil, @population=nil,
    > > @elevation=nil, @distance=0.4495>]
    > >>> places.first.country_name

    > > => "United States"

    >
    > Thanks. I had tried places.country_name among others. Why the "first"?
    >
    >

    Because places is an array (think a list of geonames). You can tell it is an
    Array because it is surrounded by brackets. You have to tell it which
    element in the Array you are interested in, in this case the one at index
    zero (the first index).

    http://ruby-doc.org/core/classes/Array.html


    > places_nearby[0].country_code works too.
    >
    > The docs didn't suggest how to parse the result.
    >
    > Thank you for the answers, but I'm not sure why it's working. But I can
    > proceed.
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
     
    Josh Cheek, Jun 2, 2010
    #6
  7. 12 34

    12 34 Guest

    I'm confused. It's an array of one element, but that element has some
    kind of parts. The "parts" and how you access them are not clear to me.
    I can follow the model. It seems a hash would make more sense or
    multiple elements in the array (except one couldn't access them by name.
    And why an array of one element? Why bother with the array?

    Confused, but thanks for the help. I do what I need to do, but
    understanding it would help in the future.

    At the moment I getting timeouts errors, so can't test much, but I did
    earlier.

    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 3, 2010
    #7
  8. On Thu, Jun 3, 2010 at 8:44 AM, 12 34 <> wrote:
    > I'm confused. It's an array of one element, but that element has some
    > kind of parts. The "parts" and how you access them are not clear to me.


    Toponym is just trying to make the access easier, you are right that
    the structure is very Hash-alike, and I would argue that either a Hash
    or a Struct would have been more sensible.
    The author of the geonames library doesn't use much idomatic ruby
    anywhere, so I will argue that he is just not very familiar with Ruby
    and this library might even be his first project to be out in the
    wild.

    Now to Toponym itself, for the purpose of discussion I put the source
    for this class online: http://pastie.org/990140
    It's the kind of code that would make a seasoned Ruby developer run
    screaming with waving hands.
    Not only is he using 4 spaces for indentation ;) but also the
    seldom-used attr in combination with attr_writer for the exact same
    names, which would be much better handled with a single attr_accessor
    statement.

    module Geonames
    class Toponym
    attr_accessor :geoname_id, :name, :alternate_names,
    :country_code, :country_name, :population,
    :elevation, :feature_class, :feature_class_name,
    :feature_code,:feature_code_name, :latitude,
    :longitude, :distance
    end
    end


    Same functionality, less boilerplate.
    But to instantiate this class, Adam is using following code (which
    warranted another pastie http://pastie.org/990146 )
    As you can see, it's pretty verbose and unlike anything you would be
    comfortable with after spending more time with Ruby.
    Actually, after going through this code, I'm really itching to rewrite
    it, we'll see how that goes :)

    > I can follow the model. It seems a hash would make more sense or
    > multiple elements in the array (except one couldn't access them by name.
    > And why an array of one element? Why bother with the array?


    Because it may return more than one toponym or none at all, having an
    Array makes the result easier to handle.
    I couldn't find any examples of coordinates that would return more
    than one toponym.
    Checking the source showed that it parses some XML, which may have
    more than one, so although I cannot confirm that they exists they are
    certainly allowed by the API.


    > Confused, but thanks for the help. I do what I need to do, but
    > understanding it would help in the future.
    >
    > At the moment I getting timeouts errors, so can't test much, but I did
    > earlier.



    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 3, 2010
    #8
  9. 12 34

    12 34 Guest

    Michael

    Thank you so much for taking the time to explain this. I had looked a
    couple of my Ruby books and was finding nothing. I was ready to really
    dig in this morning, but obviously would not have found the answer.

    If you want a beta tester I'm in. I'm not a programmer of any kind and
    can only write basic Ruby with lots of help. If you decide to go ahead
    you might look at gps2photo and ExifTool. Another thing to consider is
    what source is being used and offering alternatives. I think ExifTool
    does this I think. We do need more mature gems to match what's available
    in perl.

    Thanks again.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 3, 2010
    #9
  10. 12 34

    botp Guest

    On Thu, Jun 3, 2010 at 7:44 AM, 12 34 <> wrote:
    > I'm confused. It's an array of one element, but that element has some
    > kind of parts. The "parts" and how you access them are not clear to me.


    read about geonames

    > I can follow the model. It seems a hash would make more sense or
    > multiple elements in the array (except one couldn't access them by name.
    > And why an array of one element? Why bother with the array?


    because you can get possibly multiple places. if you want to get a lot
    of places, specify a radius, possibly a big one just to test ..

    kind regards -botp
     
    botp, Jun 4, 2010
    #10
  11. On Fri, Jun 4, 2010 at 2:15 AM, 12 34 <> wrote:
    > Michael
    >
    > Thank you so much for taking the time to explain this. I had looked a
    > couple of my Ruby books and was finding nothing. I was ready to really
    > dig in this morning, but obviously would not have found the answer.
    >
    > If you want a beta tester I'm in. I'm not a programmer of any kind and
    > can only write basic Ruby with lots of help. If you decide to go ahead
    > you might look at gps2photo and ExifTool. Another thing to consider is
    > what source is being used and offering alternatives. I think ExifTool
    > does this I think. We do need more mature gems to match what's available
    > in perl.


    I'm well along, will put the code up on GitHub today.
    I'm using the JSON API, since it makes the code even simpler.


    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 4, 2010
    #11
  12. > If you want a beta tester I'm in.

    It's not very polished yet, but since I said I'd push today:
    http://github.com/manveru/geonames

    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 4, 2010
    #12
  13. 12 34

    12 34 Guest

    Michael Fellinger wrote:
    >> If you want a beta tester I'm in.

    >
    > It's not very polished yet, but since I said I'd push today:
    > http://github.com/manveru/geonames


    OK, I'm a relative newbie. How do I use it? Just include the geonames.rb
    file? Please give a sample of how to call it.

    Should it be installed as a gem? If so how? I guess a bigger question is
    how does ruby sort out three gems named geonames? The last one
    installed? elecnix being the third.

    Just looked at my gems and could drag in your version. I assume Ruby
    just looks in the folder and doesn't keep any registration file?

    Thanks for pushing this out. And as far as I'm concerned I'll be fairly
    tied up through the weekend, so take your time.
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 4, 2010
    #13
  14. 12 34

    12 34 Guest

    One minor comment. I'm more used to seeing lon than lng.

    Is there a way to get DST given lat lon and the date? isdst exists in
    Ruby, but AFAIK is only for local current time.

    Is there a way to get the state or province?
    --
    Posted via http://www.ruby-forum.com/.
     
    12 34, Jun 4, 2010
    #14
  15. On Sat, Jun 5, 2010 at 4:06 AM, 12 34 <> wrote:
    > Michael Fellinger wrote:
    >>> If you want a beta tester I'm in.

    >>
    >> It's not very polished yet, but since I said I'd push today:
    >> http://github.com/manveru/geonames

    >
    > OK, I'm a relative newbie. How do I use it? Just include the geonames.rb
    > file? Please give a sample of how to call it.


    There is an example for every method in the comments, i didn't finish
    the docs for the last few ones, will try to do that today and write
    some little intro for the readme.

    > Should it be installed as a gem? If so how? I guess a bigger question is
    > how does ruby sort out three gems named geonames? The last one
    > installed? elecnix being the third.


    It's possible, but not really nice to name the gem geonames. I'll find
    some crazy name and rename it.

    > Just looked at my gems and could drag in your version. I assume Ruby
    > just looks in the folder and doesn't keep any registration file?
    >
    > Thanks for pushing this out. And as far as I'm concerned I'll be fairly
    > tied up through the weekend, so take your time.


    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 5, 2010
    #15
  16. On Sat, Jun 5, 2010 at 4:54 AM, 12 34 <> wrote:
    > One minor comment. I'm more used to seeing lon than lng.


    The API uses lng everywhere, so of course I use it as well.

    > Is there a way to get DST given lat lon and the date? isdst exists in
    > Ruby, but AFAIK is only for local current time.


    sigma github/manveru/geonames % irb
    require './lib/geonames'
    # true
    GeoNames.timezone(lat: 47.01, lng: 10.2)
    # {"time"=>"2010-06-05 08:27", "countryName"=>"Austria",
    "rawOffset"=>1, "dstOffset"=>2, "countryCode"=>"AT", "gmtOffset"=>1,
    "lng"=>10.2, "timezoneId"=>"Europe/Vienna", "lat"=>47.01}

    > Is there a way to get the state or province?


    See the API docs: http://www.geonames.org/export/ws-overview.html

    GeoNames.country_subdivision(lat: 47.03, lng: 10.2, maxRows: 10, radius: 40)
    # {"distance"=>0, "adminCode1"=>"07", "countryName"=>"Austria",
    "countryCode"=>"AT", "codes"=>[{"code"=>"07", "type"=>"FIPS10-4"},
    {"code"=>"7", "type"=>"ISO3166-2"}], "adminName1"=>"Tyrol"}


    --
    Michael Fellinger
    CTO, The Rubyists, LLC
     
    Michael Fellinger, Jun 5, 2010
    #16
    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. GIMME
    Replies:
    2
    Views:
    935
    GIMME
    Feb 11, 2004
  2. Naren
    Replies:
    0
    Views:
    615
    Naren
    May 11, 2004
  3. Christopher Diggins
    Replies:
    0
    Views:
    631
    Christopher Diggins
    Jul 9, 2007
  4. Eric Frost
    Replies:
    0
    Views:
    240
    Eric Frost
    Feb 3, 2011
  5. 12 34

    geonames DST problem

    12 34, Jun 3, 2010, in forum: Ruby
    Replies:
    0
    Views:
    122
    12 34
    Jun 3, 2010
Loading...

Share This Page