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
-
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.
2 -
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.
1 -
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
0 -
0
-
- 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.
0 -
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?
0 -
Your script uses the OpenCage API, so that's what I tested:
Please 💡/💖/👍/😊 this post if you read it and found it helpful.
Please accept the answer if it solved your problem.
0
Categories
- All Categories
- 1.9K Product Ideas
- 1.9K Ideas Exchange
- 1.6K Connect
- 1.3K Connectors
- 307 Workbench
- 6 Cloud Amplifier
- 9 Federated
- 3K Transform
- 112 SQL DataFlows
- 649 Datasets
- 2.2K Magic ETL
- 4K Visualize
- 2.5K Charting
- 790 Beast Mode
- 78 App Studio
- 43 Variables
- 746 Automate
- 187 Apps
- 475 APIs & Domo Developer
- 68 Workflows
- 16 DomoAI
- 40 Predict
- 17 Jupyter Workspaces
- 23 R & Python Tiles
- 406 Distribute
- 117 Domo Everywhere
- 279 Scheduled Reports
- 10 Software Integrations
- 139 Manage
- 136 Governance & Security
- 8 Domo Community Gallery
- 47 Product Releases
- 12 Domo University
- 5.4K Community Forums
- 41 Getting Started
- 31 Community Member Introductions
- 113 Community Announcements
- 4.8K Archive