0

I am trying to test the following code on my system but I cannot get the correct output.

  1. Is this code correct for using geopy with GoogleV3?
  2. If so, how can I return country and continent name?

import subprocess
from geopy.geocoders import GoogleV3

def set_proxy(proxy_addr):
    cmd = "set HTTPS_PROXY=" + proxy_addr
    call = subprocess.call(cmd, shell=True)
    cmd = "set HTTP_PROXY=" + proxy_addr
    call = subprocess.call(cmd, shell=True)

set_proxy("my.proxy")

api_key = "my key"
geolocator = GoogleV3(api_key)
location = geolocator.reverse("52.509669, 13.376294", timeout = 10)
#would like to return just country and continent name
4
  • 1
    What output are you getting? Are you receiving an error message?
    – MaryBeth
    Commented Apr 13, 2016 at 19:01
  • @MaryBeth I want to return the country and continent (if possible) name for that lat long.
    – dustin
    Commented Apr 13, 2016 at 19:04
  • 2
    "I am trying to test the following code on my system but I cannot get the correct output." What output are you receiving versus what are you expecting? If you are receiving an error, please post it. From our tour: Focus on questions about an actual problem you have faced. Include details about what you have tried and exactly what you are trying to do.
    – MaryBeth
    Commented Apr 13, 2016 at 19:10
  • 1
    What is the value of location, i.e has the reverse geocode been successful? Commented Apr 13, 2016 at 20:40

1 Answer 1

2

Don't use an external command to set your proxy. If you are on Windows and your proxy is already setup in your internet options, you don't need to set it up in the script at all, it will be autodetected.

If you don't have a proxy already set up, you can pass the proxy directly to the geocoder:

geolocator = GoogleV3(api_key, proxies={"http": proxy_addr, "https": proxy_addr})

Or you can set the proxy as an environment variable without an external command:

os.environ['HTTP_PROXY'] = proxy_addr
os.environ['HTTPS_PROXY'] = proxy_addr
geolocator = GoogleV3(api_key)

Then you can access the returned address like:

location = geolocator.reverse([52.509669, 13.376294], timeout = 10, exactly_one=True)
print location.longitude,location.latitude,location.address

Output:

13.3756952 52.5102143 Potsdamer Platz 4, 10785 Berlin, Germany

If you want to extract the country name and post code, have a look in the location.raw dictionary. The response components are specified in the Google API documentation.

from geopy.geocoders import GoogleV3

##################################################
# Some dummy values just so the script is
# self contained and runnable
api_key=None
rows=[{'avg_lat':52.509669, 'avg_lon':13.376294}]
##################################################

def get_component(location, component_type):
    for component in location.raw['address_components']:
        if component_type in component['types']:
            return component['long_name']

geolocator = GoogleV3(api_key)
for row in rows:
    location = geolocator.reverse((row['avg_lat'], row['avg_lon']), timeout = 10,  exactly_one=True)
    post_code = get_component(location, 'postal_code')
    country = get_component(location, 'country')

    print location.longitude,location.latitude,location.address
    print post_code,country

Note this is only applicable to the GoogleV3 geocoder and subject to breakage if Google changes their API...

Note - Google doesn't appear to return the continent name.

9
  • Thanks for the help. Been trying to get this to work weeks now
    – dustin
    Commented Apr 14, 2016 at 11:17
  • Luke, where is the documentation for accessing the raw components?
    – dustin
    Commented Apr 18, 2016 at 14:00
  • @dustin geopy.readthedocs.org discusses the raw dictionary
    – user2856
    Commented Apr 18, 2016 at 20:17
  • I was looking there but it wasn't verbose enough for me to understand. I couldn't find much on zip codes (postal codes) or how to access them if possible.
    – dustin
    Commented Apr 19, 2016 at 11:25
  • That's because raw is just the original response from the server parsed into a python dictionary. The response from each geocoding API will be different. You can use standard python techniques to inspect it, i.e print, location.raw.keys(), dir(location.raw) etc...
    – user2856
    Commented Apr 19, 2016 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.