A Byte of my 2.2-lb Brain

Just sharing stuff…

Obtaining Geolocation Details using GeoPy

from geopy import geocoders

mycoder = geocoders.GoogleV3()

cebu = g.geocode("Cebu")
manila = g.geocode("Manila")
cdo = g.geocode("Cagayan de Oro")

The variables cdo, manila, and cebu are of type geopy.location.Location with the following attributes: address, altitude, latitude, longitude, point, and raw.

To access these information, for Cagayan de Oro, for example, just execute the following: cdo.address, cdo.latitude, etc. To get the raw information, just call cdo.raw, and you will get a dictionary of dictionaries as shown at the bottom of the post.

Calling cdo.raw will give you dictionary with the following keys:

– address_components
– place_id
– formatted_address
– types

For me, I’m interested in the geometry key which returns a dict with the following keys:

– location_type
– viewport
– location

Finally, the bounds key gives you a dictionary with the northeast (lat, lng) and southwest (lat, lng) bounds of a location.

cdoraw = cdo.raw
print cdoraw['geometry']['bounds']

{u'northeast': {u'lat': 8.520817, u'lng': 124.8184203}, u'southwest': {u'lat': 8.2439401, u'lng': 124.447031}}

Below is the full output of calling cdo.raw

{u'address_components': [{u'long_name': u'Cagayan de Oro',
u'short_name': u'CDO',
u'types': [u'locality', u'political']},
{u'long_name': u'Misamis Oriental',
u'short_name': u'Misamis Oriental',
u'types': [u'administrative_area_level_2', u'political']},
{u'long_name': u'Northern Mindanao',
u'short_name': u'Northern Mindanao',
u'types': [u'administrative_area_level_1', u'political']},
{u'long_name': u'Philippines',
u'short_name': u'PH',
u'types': [u'country', u'political']}],
u'formatted_address': u'Cagayan de Oro, Misamis Oriental, Philippines',
u'geometry': {u'bounds': {u'northeast': {u'lat': 8.520817,
u'lng': 124.8184203},
u'southwest': {u'lat': 8.2439401, u'lng': 124.447031}},
u'location': {u'lat': 8.4542363, u'lng': 124.6318977},
u'location_type': u'APPROXIMATE',
u'viewport': {u'northeast': {u'lat': 8.520817, u'lng': 124.8184203},
u'southwest': {u'lat': 8.2439401, u'lng': 124.447031}}},
u'place_id': u'ChIJgZ4Ogdny_zIRouJtSrjV3C4',
u'types': [u'locality', u'political']}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s


This entry was posted on May 17, 2015 by in Geek and tagged , , .
%d bloggers like this: