-
Notifications
You must be signed in to change notification settings - Fork 3
/
geocodeplaces.py
56 lines (49 loc) · 1.84 KB
/
geocodeplaces.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
46
47
48
49
50
51
52
53
54
55
56
import os
import geocoder
import csv
import numpy as np
import time
import json
folder = os.path.join('data', 'monuments')
files = [file for file in os.listdir(folder) if file.endswith('.csv')]
for file in files:
coordinates = {}
state = file.split('_')[0]
filename = file.split('.')[0]
with open(os.path.join(folder, file), 'r') as f:
reader = csv.reader(f, delimiter=';')
header = next(reader)
data = np.array(list(reader))
for place in data[:, 1]:
time.sleep(0.05)
address = '{}, {}, AT'.format(place, state)
if address not in coordinates:
print(address)
g = geocoder.arcgis(address)
trials = 10
for i in range(trials):
print(g.status)
if g.status == 'OK':
entry = {
'lat':g.latlng[0],
'lon':g.latlng[1],
'bbox': g.bbox,
'count': 1}
print(entry)
coordinates[address] = entry
break
elif g.status == 'ZERO_RESULTS':
g = geocoder.arcgis(address)
if i == trials - 1:
print("ERROR")
coordinates[address] = g.status
else:
print('ERROR')
print(g.current_result)
time.sleep(1)
g = geocoder.arcgis(address)
else:
coordinates[address]['count'] += 1
outputfile = os.path.join('data', filename+".json")
with open(outputfile, 'w') as f:
json.dump(coordinates, f, sort_keys=True, indent=4)