-
Notifications
You must be signed in to change notification settings - Fork 0
/
sae_ctaz.py
54 lines (45 loc) · 1.47 KB
/
sae_ctaz.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
import json
import pycurl
from io import BytesIO
def sae_to_geojson():
"""
Transforms http://api.consorciozaragoza.es/api/1/sae JSON data to GeoJSON
"""
buffer = BytesIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://api.consorciozaragoza.es/api/1/sae')
c.setopt(c.WRITEDATA, buffer)
c.perform()
response_code = c.getinfo(pycurl.RESPONSE_CODE)
c.close()
# Creates GeoJSON with no features
return_json = {
'type': 'FeatureCollection',
'features': []
}
if response_code == 200: # OK
body = buffer.getvalue()
sae_json = json.loads(body.decode('utf-8'))
# For every bus in sae_json, transform it to a point feature and append it to the GeoJSON
for i in sae_json['sae']:
point = {
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [float(i['longitud']), float(i['latitud'])]
},
'properties': {
'busNumber': i['bus'],
'lineNumber': i['linea'],
'lineName': i['nombre_linea'],
'lastUpdated': i['momento']
}
}
return_json['features'].append(point)
return return_json
else:
# Something went wrong
print("Error", response_code)
return return_json
if __name__ == "__main__":
print(json.dumps(sae_to_geojson()))