Transfer Street Address to Geo Latitude and Longitude Using Python

I have a column contains street address, and I want to transfer it into geo latitude and longitude using python script in Magic ETL
This is my code and it's not working, anyone has any idea how to make it work

import requests
from domomagic import *

def get_geolocation(address):
api_key = ' '
url = f'https://api.opencagedata.com/geocode/v1/json?q={address}&key={api_key}&language=en&no_annotations=1'

response = requests.get(url)

if response.status_code == 200:
data = response.json()
if data['results']:
lat = data['results'][0]['geometry']['lat']
lng = data['results'][0]['geometry']['lng']
return lat, lng
return None, None
print(f"Error: {response.status_code}")
return None, None

input1 = read_dataframe('Franchise Master Center Info')

latitude_list = []
longitude_list = []

for _, row in input1.iterrows():
address = row['Full Address']
lat, lng = get_geolocation(address)
latitude_list.append(lat)
longitude_list.append(lng)

input1['Latitude'] = latitude_list
input1['Longitude'] = longitude_list

write_dataframe(input1, 'Franchise Master Center Info with GeoData')

Answers

  • Manasi_Panov
    Manasi_Panov Contributor
    edited 1:35AM

    Dear @Chuqi_D,

    As far as I know, you can't directly call APIs through Domo's Magic ETL. You may use Jupyter Notebook within the Domo environment for that purpose. I tested your code, but it doesn’t seem to be working correctly. I'm investigating the issue and will update you if I find a solution.

    If you found this post helpful, please use 💡/💖/👍/😊 below! If it solved your problem, don't forget to accept the answer.

  • Manasi_Panov
    Manasi_Panov Contributor

    Hello @Chuqi_D,

    Yes, it is working with Domo Jupyter Notebook. Here is the result for some random addresses I picked from Google Maps:

    If you found this post helpful, please use 💡/💖/👍/😊 below! If it solved your problem, don't forget to accept the answer.

  • Chuqi_D
    Chuqi_D Member

    Hey I am positive that I put in the correct API Address, it still didn't work for me, here is my code, can you tell me where I did wrong

  • Chuqi_D
    Chuqi_D Member
    edited 8:38PM
    • Be careful: both the address you were searching for and your API key are in that error code.
    • Using your API key, I got the same 401 error as you. Using a valid API key and your exact same script, I was able to retrieve coordinates. I would double check your API key one more time.

    Please 💡/💖/👍/😊 this post if you read it and found it helpful.

    Please accept the answer if it solved your problem.

  • Chuqi_D
    Chuqi_D Member

    Thanks for the heads up. The address is just a testing address, but I delete my API key info. Are you using the google map API?

  • Your script uses the OpenCage API, so that's what I tested:

    https://opencagedata.com/dashboard#geocoding

    Please 💡/💖/👍/😊 this post if you read it and found it helpful.

    Please accept the answer if it solved your problem.