Last active
January 4, 2025 20:35
-
-
Save rivermont/f08b156bf2c68da41ee43fe8ee4e9448 to your computer and use it in GitHub Desktop.
Use Nominatim to fill in missing city coordinates (requires specific data format used in the research). Dependencies: pandas, geopy
This file contains hidden or 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/python3 | |
"""Will Bennett - rivermont.xyz - Jan 2025""" | |
import pandas as pd | |
from time import sleep | |
from geopy.geocoders import Nominatim | |
locater = Nominatim(user_agent='Python 3 - <rivermont.xyz>') | |
df = pd.read_csv('./location_v4.csv', encoding='utf-8', na_filter=False) | |
for i, row in df.iterrows(): | |
if row['City']: # skip rows without city name | |
# print(row['City']) | |
if not row['X']: # skip rows with location data | |
city = row['City'] + ', ' + row['Country.Area'] | |
print(city) | |
location = locater.geocode(city) | |
print((location.latitude, location.longitude)) | |
df.loc[i, 'X'] = location.longitude # set lon | |
df.loc[i, 'Y'] = location.latitude # set lat | |
df.loc[i, 'lonlat_source'] = 'Nominatim' # set data source | |
sleep(0.6) # prevent server kicking | |
df.to_csv('location_geocoded.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment