DenisCarriere / geocoder

:earth_asia: Python Geocoder
http://geocoder.readthedocs.org
MIT License
1.63k stars 288 forks source link

Google requires API Key #428

Closed rgraulus closed 4 years ago

rgraulus commented 4 years ago

Does your code supports Google's reverse geocoding since they require the use of an API key? I quickly browsed through your documentation but couldn't find anything on how to use Google API key. Please advise.

GregNed commented 4 years ago

Yes, just pass it as the 'key' kwarg. And it applies to all Google's methods in this geocoder. In the example below, I put my key in an environment variable and retrieve via os module:

import os 
key_google = os.environ['GOOGLE_API_KEY']
geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)
rgraulus commented 4 years ago

For some reason I can't get geocoder to work. Here's what I tried:

import os
import geocoder
g = geocoder.google('Mountain View, CA')
print(g.latlng)

None

key_google = os.environ['MY_GOOGLE_KEY'] geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)

KeyError Traceback (most recent call last) ipython-input-20-d2b1c1f1a489> in module> ----> 1 key_google = os.environ['MY_GOOGLE_KEY'] 2 geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google) ~/miniconda3/envs/sandbox/lib/python3.8/os.py in getitem(self, key) 673 except KeyError: 674 # raise KeyError with the original key value --> 675 raise KeyError(key) from None 676 return self.decodevalue(value) 677

     KeyError: 'MY_GOOGLE_KEY'
emesterhazy commented 4 years ago

@rgraulus It looks like you don't have the MY_GOOGLE_KEY environ set. What do you see if you type echo $MY_GOOGLE_KEY in bash? If you see nothing, you need to set the variable (i.e. export MY_GOGLE_KEY="yourkeyvalue")

rgraulus commented 4 years ago

When I type echo $MY_GOOGLE_KEY in the command line it prints out my Google Maps API key. So, the variable is set. However, this did not resolve the issue. Also, I checked my Google credentials and I do have a Google Maps Geocoding API key.

I also tried a few other commands e.g.: g = geocoder.google('Mountain View, CA') print(g.latlng)

None

g = geocoder.google([45.15, -75.14], method='reverse')
print(g.city)
print(g.state)
print(g.state_long)
print(g.country)
print(g.country_long)
>>>None
>>>None
>>>None
>>>None

I always get the output None.

rgraulus commented 4 years ago

Also, I tested my API key by adding it to the following request and it works:

https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=YOUR_API_KEY

emesterhazy commented 4 years ago

Based on the trace back you shared (below), it looks like the python process that ran your code does not have access to the MY_GOOGLE_KEY environment variable. You can see this on line 3: ----> 1 key_google = os.environ['MY_GOOGLE_KEY'].

KeyError Traceback (most recent call last)
ipython-input-20-d2b1c1f1a489> in module>
----> 1 key_google = os.environ['MY_GOOGLE_KEY']
2 geocoder.google({'lng': -76.33318, 'lat': 42.50656}, method='reverse', key=key_google)
~/miniconda3/envs/sandbox/lib/python3.8/os.py in getitem(self, key)
673 except KeyError:
674 # raise KeyError with the original key value
--> 675 raise KeyError(key) from None
676 return self.decodevalue(value)
677

Can you try running your code again from the same shell where you confirmed that the environment variable is set?

Regarding the None output, I'm pretty sure that Google requires an API key now for all requests. If the key isn't set up properly, you probably will not get a useful response from Google's API.

rgraulus commented 4 years ago

Great, it now works!

Two more questions:

  1. Can you also enter multiple lat/lons in a single request? If yes, what is the syntax?
  2. Can you write the output directly into a csv file?

Thanks!

emesterhazy commented 4 years ago

I haven't used this project for a while, so I can't comment on your first point. My guess is that you can't, but you can review the documentation and or source code for the Google api portion of the project to confirm. Regarding your second question, you can definitely write the output (and whatever else you want) to a CSV. Checkout Python's built in csv module: https://docs.python.org/3/library/csv.html

rgraulus commented 4 years ago

Thanks!

rgraulus commented 4 years ago

Hi,

I have the same issue pop up again!

Since I get the same None output as before as well as KeyError: 'MY_GOOGLE_KEY' I assume this has to do with the PATH again...Not sure why??

I added the directory of the conda env where geocoder is installed to the .bashrc file: export PATH=$PATH:/home/rik/miniconda3/envs/sandbox

But this does not solve the problem.

What am I doing wrong?

Regards

Rik