-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_areas.py
96 lines (74 loc) · 3.11 KB
/
get_areas.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
87
88
89
90
91
92
93
94
95
96
import requests
import numpy
import csv
import json
import progressbar
from pprint import pprint
_DEFAULT_PARAMS = {
'f': 'json',
#'where': "1=1", # 'where' clause is mandatory it takes a postgres-like query
# 'where': "n_o > 10", # example where the number of reports event was over 10
# 'where': "freguesia = 'Bonfim'", # example for all the reports in 'Bonfim'
# 'where': "ano > 2000", # Caveat: For example in this dataset, this will not work as 'ano' is defined as "esriFieldTypeString"
#'returnGeometry': 'true',
#'outFields': '*', # the fields that you want returned
#'orderByFields': 'objectid ASC',
# 'resultOffset': '4000',
#'resultRecordCount': '10', # for the purpose of the demonstration we are limiting to 10 results
'geometryType': 'esriGeometryPoint',
'geometry': '-8.629500540214053, 41.15801561717854',
'inSR': '4326',
'outSR': '4326',
'returnIdsOnly': True
# 'token': str(TOKEN)
}
def request_objects(geometry_object_str, mapNumber=77):
req_params = _DEFAULT_PARAMS
req_params['geometry'] = geometry_object_str
url = 'https://servsig.cm-porto.pt/arcgis/rest/services/OpenData_APD/OpenData_APD/MapServer/{}/query'.format(mapNumber)
r = requests.get(url, params=req_params)
return r.json()
def request_objects_around_coordinate(lat,long, mapNumber=77):
s = "{}, {}".format(long, lat)
return request_objects(s, mapNumber)
def write_csv(category, dataList, t=None):
if t is None:
t = category
fname = "POI_{}_{}.csv".format(category, t)
with open(fname, 'w', newline='') as csvfile:
fieldnames = ['type', 'lat', 'long', 'value', 'category']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for o in dataList:
writer.writerow(o)
print("Writen: ", fname)
if __name__ == "__main__":
coords = []
for lat in numpy.arange(41.131386,41.208026, 0.0018):
for lon in numpy.arange(-8.712680,-8.558663, 0.0025):
coords.append((lat,lon))
print("N Cords: ", len(coords))
## Numbers
## Areas-verdes: {'map_number': 77, 'cat': 'GreenZone'}
## bairos: 43 {'map_number': 43,'cat': 'Bairos'}
descr = {
'map_number': 43,
'cat': 'Bairos'
}
dataList = []
for lat,long in progressbar.progressbar(coords):
data = request_objects_around_coordinate(lat, long, mapNumber=descr['map_number'])
v = 0 if data['objectIds'] is None else len(data['objectIds'])
ret_obj = {'type': descr['cat'], 'lat': lat, 'long': long, 'value': v, 'category': descr['cat']}
dataList.append(ret_obj)
#print("Len: ", len(data['objectIds']))
#pprint(ret_obj)
fname = "POI_{}_{}.csv".format(descr['cat'], descr['cat'])
with open(fname, 'w', newline='') as csvfile:
fieldnames = ['type', 'lat', 'long', 'value', 'category']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for o in dataList:
writer.writerow(o)
print("Writen: ", fname)
print("HelloWorld")