-
Notifications
You must be signed in to change notification settings - Fork 0
/
elevation_csv.py
46 lines (34 loc) · 1.58 KB
/
elevation_csv.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
from csv import reader, writer
from urllib import urlopen
from simplejson import load as jload
api_url = 'http://maps.googleapis.com/maps/api/elevation/json?locations=%s&sensor=false'
ds = ('Basurales', 'Industrias', 'Relocalizaciones',) # 'Asentamientos'
for d in ds:
dataset_in = './datasets/QPR - %s.csv' % d
dataset_out = './datasets/QPR - %s-loc.csv' % d
elems = [x for x in reader(open(dataset_in, 'r'))] # Rows
dout = writer(open(dataset_out, 'wb')) # Output
column_names = elems[0] # Columns
location_idx = column_names.index('location') # location column
dout.writerow(column_names + ['elevation', 'resolution']) # Write columns
# Find all elems skipping first row
for row in elems[1:]:
# Get location, change to google comma format
loc = row[location_idx]
if len(loc) < 8 or loc.find(' ') == -1:
print ds, loc
dout.writerow(row + ['', '']) # No location
continue
location = ','.join(row[location_idx].split(' '))
http_response = urlopen(api_url % location)
try:
response = jload(http_response)
except:
print 'loc: ', api_url % location
print 'resp: ', http_response.read()
if response['status'] != 'OK':
print 'CUEC! [', d, ']'
break
elevation = str(response['results'][0]['elevation'])
resolution = str(response['results'][0]['resolution'])
dout.writerow(row + [elevation, resolution]) # Write row