-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathground.py
More file actions
28 lines (23 loc) · 989 Bytes
/
ground.py
File metadata and controls
28 lines (23 loc) · 989 Bytes
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
import csv
def find_city_location(
city_name: str,
#filepath: str = "lat_lon_data/uscities_lat_lng.csv"
filepath: str = "lat_lon_data/worldcities.csv"
) -> tuple:
"""
Return a location object (lat lon) for a named city (city must be in the CSV).
:param city_name: String of city for which the Lat Lon is required
:param filepath: String of the path where the locations file is located
:return:
"""
with open(filepath, newline='') as csvfile:
locations = csv.reader(csvfile, quotechar='|')
for k, row in enumerate(locations):
if k == 0: # If this is the first row, find the indices that indicate the city name, lat and long
col_name = row.index("city_ascii")
col_lat = row.index("lat")
col_lon = row.index("lng")
continue
if row[col_name] == city_name: # If we found the correct city, return the lat and long values
return float(row[col_lat]), float(row[col_lon])
raise ValueError(f"City {city_name} not found in the file")