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')