-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode_addresses.py
More file actions
34 lines (27 loc) · 1.3 KB
/
geocode_addresses.py
File metadata and controls
34 lines (27 loc) · 1.3 KB
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
import csv
import openrouteservice
def geocode_addresses(input_csv, output_csv, ors_key):
client = openrouteservice.Client(key=ors_key)
with open(input_csv, newline="", encoding="utf-8") as f_in, \
open(output_csv, "w", newline="", encoding="utf-8") as f_out:
reader = csv.DictReader(f_in)
fieldnames = reader.fieldnames + ["lat", "lon"]
writer = csv.DictWriter(f_out, fieldnames=fieldnames)
writer.writeheader()
for row in reader:
addr = row["address"]
try:
res = client.pelias_search(addr, size=1)
if res["features"]:
coords = res["features"][0]["geometry"]["coordinates"]
row["lon"], row["lat"] = coords[0], coords[1]
else:
row["lon"], row["lat"] = "", ""
except Exception as e:
print(f"Geocoding failed for {addr}: {e}")
row["lon"], row["lat"] = "", ""
writer.writerow(row)
print(f"✅ Output written to {output_csv}")
if __name__ == "__main__":
ORS_KEY = "eyJvcmciOiI1YjNjZTM1OTc4NTExMTAwMDFjZjYyNDgiLCJpZCI6ImJmNmI4YjljODBjODQ5YzZhZGRlNTk4ZWE5ZjY2M2E3IiwiaCI6Im11cm11cjY0In0="
geocode_addresses("students_with_addresses.csv", "students.csv", ORS_KEY)