Last active
April 10, 2023 12:09
-
-
Save JonathanGarro/eec289c4781ba6d04973797e75a11a23 to your computer and use it in GitHub Desktop.
A Python script to find coordinates from addresses via Google API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import googlemaps | |
import pandas as pd | |
import csv | |
# save this script in the same folder as your CSV file, then update the name of the file below | |
location_data = 'file_name_here.csv' | |
data = pd.read_csv(location_data) | |
# paste your API key below, leaving the quotes in place | |
gmaps = googlemaps.Client(key="<YOUR API KEY HERE>") | |
list_coordinates = [] | |
counter = 0 | |
for place in data['ADDRESS']: | |
temp_dict = {} | |
geocode_result = gmaps.geocode(place) | |
try: | |
temp_dict['lat'] = geocode_result[0]['geometry']['location']['lat'] | |
temp_dict['lng'] = geocode_result[0]['geometry']['location']['lng'] | |
except: | |
temp_dict['lat'] = 0 | |
temp_dict['lng'] = 0 | |
list_coordinates.append(temp_dict) | |
counter += 1 | |
print(counter) | |
keys = list_coordinates[0].keys() | |
a_file = open("output.csv", "w") | |
dict_writer = csv.DictWriter(a_file, keys) | |
dict_writer.writeheader() | |
dict_writer.writerows(list_coordinates) | |
a_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment