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.
location
, i.e has the reverse geocode been successful?