Parsing geonames

1

12 34

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.
 
S

Sven Schott

[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
 
M

Michael Fellinger

41.505928,-81.594582

iota ~ % irb=> [#<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>]=> "United States"
 
1

12 34

Michael said:
41.505928,-81.594582

iota ~ % irb=> [#<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>]=> "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.
 
B

botp

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
 
J

Josh Cheek

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

Michael said:
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.
 
1

12 34

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.
 
M

Michael Fellinger

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.
 
1

12 34

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.
 
B

botp

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
 
M

Michael Fellinger

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.
 
1

12 34

Michael said:
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.
 
1

12 34

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?
 
M

Michael Fellinger

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.
 
M

Michael Fellinger

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"}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top