Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Parsing geonames

Reply
Thread Tools

Parsing geonames

 
 
12 34
Guest
Posts: n/a
 
      06-02-2010
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/.

 
Reply With Quote
 
 
 
 
Sven Schott
Guest
Posts: n/a
 
      06-02-2010
[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 <(E-Mail Removed)> 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/.
>
>


 
Reply With Quote
 
 
 
 
Michael Fellinger
Guest
Posts: n/a
 
      06-02-2010
On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <(E-Mail Removed)> wrote:
> 41.505928,-81.594582


iota ~ % irb
>> require 'geonames'

=> true
>> places = Geonames::WebService.find_nearby_place_name(41.505 928, -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

 
Reply With Quote
 
12 34
Guest
Posts: n/a
 
      06-02-2010
Michael Fellinger wrote:
> On Wed, Jun 2, 2010 at 2:57 PM, 12 34 <(E-Mail Removed)> wrote:
>> 41.505928,-81.594582

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

> => true
>>> places = Geonames::WebService.find_nearby_place_name(41.505 928, -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/.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      06-02-2010
On Wed, Jun 2, 2010 at 2:30 PM, 12 34 <(E-Mail Removed)> 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

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      06-02-2010
[Note: parts of this message were removed to make it a legal post.]

On Wed, Jun 2, 2010 at 1:30 AM, 12 34 <(E-Mail Removed)> wrote:

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

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

> > => true
> >>> places = Geonames::WebService.find_nearby_place_name(41.505 928,

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


 
Reply With Quote
 
12 34
Guest
Posts: n/a
 
      06-02-2010
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/.

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      06-03-2010
On Thu, Jun 3, 2010 at 8:44 AM, 12 34 <(E-Mail Removed)> 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, opulation,
: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

 
Reply With Quote
 
12 34
Guest
Posts: n/a
 
      06-03-2010
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/.

 
Reply With Quote
 
botp
Guest
Posts: n/a
 
      06-04-2010
On Thu, Jun 3, 2010 at 7:44 AM, 12 34 <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
New article on grabbing city points from geonames, processing withPython, and rendering in MapPoint Eric Frost Python 0 02-03-2011 06:10 PM
geonames DST problem 12 34 Ruby 0 06-03-2010 07:10 PM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments