forked from bvanheu/sherbrooke-gis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_objects_geojson.py
86 lines (60 loc) · 2.4 KB
/
extract_objects_geojson.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python
import requests
import json
import time
ARCGIS_SERVER_URL = 'https://cartes.ville.sherbrooke.qc.ca/arcgis/rest/services/Utilitaires/Localisateur/MapServer/0/query?{}'
HEADERS = {
'DNT': '1',
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US;en;q=0.8,fr;q=0.6',
'User-Agent': 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': '*/*',
'Referer': 'https://carte.ville.sherbrooke.qc.ca/infopropriete/',
'X-Requested-With': 'XMLHttpRequest',
'Connection': 'keep-alive',
}
def export_objects(begin=1, count=1, fields='*'):
params = {
'f': 'geojson',
'objectIds': ','.join([str(i) for i in list(range(begin, begin + count))]),
'outFields': fields,
'returnGeometry': 'true',
}
url = ARCGIS_SERVER_URL.format('&'.join([k + "=" + params[k] for k in params]))
result = do_query(url)
return result
def do_query(endpoint, params={}):
r = requests.get(endpoint, params=params, headers=HEADERS, timeout=1)
if r.status_code != 200:
raise Exception('http error code {} from {}'.format(r.status_code, r.url))
return r.json()
def convert_values_to_numbers(feature):
for key in ["VALEUR_TERRAIN", "VALEUR_BATIMENT", "VALEUR_PROPRIETE"]:
if key in feature["properties"] and feature["properties"][key] not in ['', None]:
feature["properties"][key] = float(feature["properties"][key])
return feature
if __name__ == "__main__":
print("Exporting object ids from Sherbrooke Arcgis server")
step = 300
output_file = 'output.geojson'
with open(output_file, 'w') as f:
f.write('{"type": "FeatureCollection", "features": [')
i = 1
while True:
print('Fetching {} to {}'.format(i, i + step - 1))
geojson = export_objects(i, step)
if not geojson['features']:
break
features_converted = [convert_values_to_numbers(feature) for feature in geojson['features']]
with open(output_file, 'a') as f:
for feature in features_converted:
json.dump(feature, f)
f.write(',')
i += step
with open(output_file, 'rb+') as f:
f.seek(-1, 2)
f.truncate()
with open(output_file, 'a') as f:
f.write(']}')
print("Done")