-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoordinates.py
More file actions
32 lines (25 loc) · 859 Bytes
/
coordinates.py
File metadata and controls
32 lines (25 loc) · 859 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
29
30
31
32
import pandas as pd
import os
import requests
def add_coords(df, key):
n_df = df.copy()
cache = {}
lat = []
lng = []
for d in df["Dam"].values:
if d not in cache:
url = "https://maps.googleapis.com/maps/api/geocode/json?address=" + d + "®ion=za&key=" + key
data = requests.get(url).json()
if data["status"] == "OK":
d_lat = data["results"][0]["geometry"]["location"]["lat"]
d_lng = data["results"][0]["geometry"]["location"]["lng"]
cache[d] = (d_lat, d_lng)
else:
print(data["status"] + " for " + d)
cache[d] = (0, 0)
pair = cache[d]
lat.append(pair[0])
lng.append(pair[1])
n_df = n_df.assign(Latitude = lat)
n_df = n_df.assign(Longitude = lng)
return n_df