How to pull out Administrative area etc from Google geocoder?

48 views
Skip to first unread message

starenka

unread,
Mar 5, 2010, 3:58:40 AM3/5/10
to geopy
Hi, i've been searching the source quite a bit , but can't find a way
how to pull more details out of (reverse) method. The returned
location is "unicode" not object w/ attributes. How can i get f.e
administrative area etc? Can't find geopy API docs either.

Thanks for hints

Douglas Mayle

unread,
Mar 5, 2010, 7:51:03 AM3/5/10
to ge...@googlegroups.com
Which geopy are you using, the normal or the reverse? If you're using the reverse geocoder, you can take a look at the RichResult class in util.py that sets attributes on the tuple for the various returned values like adminstrative or locality.... You'll have to catch the result, rather than decomposing it so you don't lose the tuple-like object. So:

result = coder.geocode()
place, (lat, lon) = result
locality = result.locality

Take a look at the code I wrote for the community almanac to get an idea:

https://source.openplans.org/hg/communityalmanac/file/58b749aceb3c/communityalmanac/controllers/geocoder.py

Douglas Mayle

> --
> You received this message because you are subscribed to the Google Groups "geopy" group.
> To post to this group, send email to ge...@googlegroups.com.
> To unsubscribe from this group, send email to geopy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/geopy?hl=en.
>

starenka

unread,
Mar 6, 2010, 2:09:14 PM3/6/10
to ge...@googlegroups.com
Hi Douglas,
I dont know, if i got it right (as i really have no idea, how it works..
i just stole your code ;)), but all i get it's the same tuple (using
reversed 93-dev)

from geopy import geocoders,util

lat = 50.326880
lon = 14.543244

def result_with_locality(res):
first = None
for x in res:
if not first: first = x
if x and x.locality and x.administrative: return x
return first

geoc = geocoders.Google('xyz')
result = result_with_locality(geoc.geocode('%f,
%f'%(lat,lon),exactly_one=False))
place, (lat, lng) = result
if result.locality and result.administrative: print '%s, %s' %
(result.locality, result.administrative)

Traceback (most recent call last):
File "./reverse.py", line 17, in <module>
result = result_with_locality(geoc.geocode('%f,
%f'%(lat,lon),exactly_one=False))
File "./reverse.py", line 13, in result_with_locality
if x and x.locality and x.administrative: return x
AttributeError: 'RichResult' object has no attribute 'locality'

--
/) = http://blog.starenka.net = ,~~.
/\___/\ (( *- www.starenka.net -* ( 6 )-_,
\`@_@'/ )) "Life is what happens when you're busy (\___ )=='-'
{_:Y:.}_// making other plans." - John Lennon \ . ) )
-={_}^-'{_}-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-~'`~'`~'`~'`~
PS. Never eat yellow snow ;)


signature.asc

Douglas Mayle

unread,
Mar 8, 2010, 5:25:37 AM3/8/10
to ge...@googlegroups.com
The key difference between the code sample you posted and the one on the web (from the link I gave you) is output_format='json' ... The kml output (the default) doesn't seem to give you extra information... You'll need to install simplejson to be able to use the json output format...

Doug

starenka

unread,
Mar 8, 2010, 1:33:40 PM3/8/10
to ge...@googlegroups.com
Hi Douglas, that was it (kml vs json). Thank You!
signature.asc
Reply all
Reply to author
Forward
0 new messages