-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert-geo.py
54 lines (42 loc) · 1.44 KB
/
convert-geo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import time
import pandas as pd
from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="kthrnsnclr@gmail.com")
# Use pandas to read the first column ("City")
locations = pd.read_csv("charley.csv", sep=",", header=0, usecols=['Location'])
# Access the DataFrame of the csv, created by pandas
df = pd.DataFrame(locations)
# Get amount of cities
LocationTotal=len(df)
# These are the lists that we will fill and add to csv
lat_list = [' ']*LocationTotal
lon_list = [' ']*LocationTotal
# Loop through each row of the DataFrame
for index, row in df.iterrows():
location = row['Location']
location_string = str(location)
# Use geolocator to grab more verbose location information
try:
location_geo = geolocator.geocode(location_string, timeout=None)
except:
print('sleep')
time.sleep(5)
continue
# Check that the location value exists. If it does, add to a list. If it doesn't, add empty string
if location_geo != None:
# print(index)
lat_list[index] = location_geo.latitude
lon_list[index] = location_geo.longitude
else:
print(location_string)
lat_list[index] = ('')
lon_list[index] = ('')
continue
# Check that the list is full and the loop actually ends!
print('loop done!')
# Add lists to CSV
df = pd.read_csv("charley.csv")
df["Long"] = lon_list
df["Lat"] = lat_list
df.to_csv("charley.csv", index=False)
print('csv done!')