Using my Google Geocoding API key with Python geocoder
Asked Answered
G

3

7

I'm a beginner at Python and I've been working to geocode a database using Pandas and Geocoder on Jupyter.

Since the df is a little long (around 3000 rows), I'd like to use Google's Geocoding API.

I've already created a free key, but I have no idea what I'm supposed to do with it. Help?

By the way, my code looks like this:

import geocoder
import pandas as pd

geo = geocoder

df=pd.read_excel('hsp2.xlsx')

df['Coordinates']=df['Address'].apply(geo.google).apply(lambda x: x.latlng if x != None else None)

df.to_csv('output.csv', sep='|', encoding='iso8859_15')
Grammar answered 26/7, 2017 at 20:26 Comment(0)
Y
11

You need to set the environment variable before you import geocoder:

import os

os.environ["GOOGLE_API_KEY"] = "api_key_from_google_cloud_platform"

import geocoder

geo = geocoder.google(address)
latlong = geo.latlng

Note:

As Murmel mentioned in the comments, environment variables containing keys (and in general) should not be set inside of your code.
If you are deploying this somewhere then set up enviroment variables in your configuration file. Or even better, as a secret in something like Kubernetes.

Else set the environment variable in bash with
export GOOGLE_API_KEY=api_key_from_google_cloud_platform

Yaekoyael answered 4/8, 2017 at 13:31 Comment(2)
As I wrote in my answer, I think your answer somehow misuses the concept of providing the API KEY. According to the docs, the env variable is meant to be used for "[...] mak[ing] sure your API key is stored safely on your computer, you can define that API key using your system’s environment variables." Thus, I think it would be good to at least mention this or describe how to do this not within python code.Corvin
That's a bit lame to have to import the key before importing the lib..Kibitz
C
4

Basically there are 2 options:

  • passing the API KEY as environment variable:

    GOOGLE_API_KEY=YOUR-API-KEY-HERE  python your_program.py
    
  • passing the API KEY as argument:

    geocoder.google('some address', key='YOUR-API-KEY-HERE')
    

Details

  1. You are using the python library called geocoder, which itself is a wrapper around multiple geocoding services.

  2. If you look at the pypi page of geocoder, you can (ignoring the rendering problems) find the docs for geocoder. In your case you probably want to have a look at the Google related part of the docs.

  3. For basic usage this seams to work even without an API KEY, but you can specify one using 2 variants:

    • Environment variable: Like Roman already showed. This approach is meant to be used to not have the API KEY in code - for security reasons. (Probably you want to upload your code into a public repository, but without exposing your API KEY to everyone.)

    • "Key" parameter: You can also provide your API KEY by specifying it using the key parameter, like:

      geocoder.google('some address', key='YOUR-API-KEY-HERE')
      
Corvin answered 10/1, 2018 at 15:35 Comment(0)
U
0

I am agree with Roman answer. You can use that and it is working. I am bit afraid if I use geocoder in loop then google will definately block my ip address ,so I go through git hub code and found that geocoder get google api key from os.environ.get('GOOGLE_API_KEY'). You can see that in the picture:

here

Uncertain answered 10/1, 2018 at 14:7 Comment(1)
Hm not quite sure, what you are trying to say? It is more a comment on Roman's answer, isn't it? Meta: Instead of linking to Roman's profile, I would recommend you tolink to his actual answer. Furthermore, instead of posting an image of the code, simply copy and paste the code and provide the link to the github for further reference is in general a better approach.Corvin

© 2022 - 2024 — McMap. All rights reserved.