-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathGecodingTesla.py
More file actions
77 lines (62 loc) · 2.87 KB
/
GecodingTesla.py
File metadata and controls
77 lines (62 loc) · 2.87 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pandas as pd
import numpy as np
import googlemaps
import gmaps
API_KEY = 'YOUR API KEY'
gm = googlemaps.Client(key=API_KEY)
data = pd.read_csv('/Users/Ramzi/Dropbox/GGS 675/Location Science Project/VA_Tesla_Current2.csv', low_memory=False, index_col = 'id')
data = data.fillna('') # fill empty entries with ''
print(list(data)) # print Variable
print(data.head())
[maxRow,maxCol] = data.shape
print(maxRow,maxCol)
def Geocode(query):
# do geocoding
try:
geocode_result = gm.geocode(query)[0]
latitude = geocode_result['geometry']['location']['lat']
longitude = geocode_result['geometry']['location']['lng']
return latitude,longitude
except IndexError:
return 0
def GeocodeStreetLocationCity(data):
lat=[] # initialize latitude list
lng=[] # initialize longitude list
start = data.index[0] # start from the first data
end = data.index[maxRow-1] # end at maximum number of row
for i in range(start,end+1,1): # iterate all rows in the data
isSuccess=True # initial Boolean flag
query = data.address[i] + ' ' + data.name[i] + ' ' + data.city[i] # try set up our query street-location-city
result=Geocode(query)
if result==0: # if not successful,
query = data.address[i] + ' ' + data.city[i] # try set up another query location-city
result=Geocode(query)
if result==0: # if still not successful,
query = data.name[i] + ' ' + data.city[i] # try set up another query street-city
result=Geocode(query)
if Geocode(query)==0: # if still not successful,
isSuccess=False # mark as unsuccessful
print(i, 'is failed')
else:
print(i, result)
else:
print(i, result)
else:
print(i, result)
if isSuccess==True: # if geocoding is successful,
# store the results
lat.append(result[0]) # latitude
lng.append(result[1]) # longitude
return lat,lng
[lat,lng]=GeocodeStreetLocationCity(data)
df = pd.DataFrame(
{'latitude': lat,
'longitude': lng
})
df.to_csv('locations2.csv')
print('saved geocoded locations to "locations2.csv"')
data1 = pd.read_csv('/Users/Ramzi/Dropbox/GGS 675/Location Science Project/VA_Tesla_Current2.csv', low_memory=False, index_col = 'id')
data2 = pd.read_csv('/Users/Ramzi/Dropbox/GGS 675/Location Science Project/locations2.csv', low_memory=False, index_col = 'id')
result = pd.concat([data1, data2], axis =1)
print(result)
result.to_csv('/Users/Ramzi/Dropbox/GGS 675/Location Science Project/VA_Tesla_Geolocated2.csv')