-
Notifications
You must be signed in to change notification settings - Fork 10
/
demo-dumpstations.py
executable file
·107 lines (94 loc) · 3.14 KB
/
demo-dumpstations.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
97
98
99
100
101
102
103
104
105
106
107
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import sys
import datetime
import os
import json
import string
import csv
from operator import itemgetter
from TrainMonitor import viaggiatreno
region_codes = {
1: "Lombardia",
2: "Liguria",
3: "Piemonte",
4: "Valle d'Aosta",
5: "Lazio",
6: "Umbria",
7: "Molise",
8: "Emilia Romagna",
10: "Friuli-Venezia Giulia",
11: "Marche",
12: "Veneto",
13: "Toscana",
14: "Sicilia",
15: "Basilicata",
16: "Puglia",
17: "Calabria",
18: "Campania",
19: "Abruzzo",
20: "Sardegna",
22: "Trentino Alto Adige"
}
destdir = os.path.join('data', 'stations-dump')
if not os.path.exists(destdir):
os.makedirs(destdir)
as_csv = []
as_geojson = {
'type': 'FeatureCollection',
'features': []
}
api = viaggiatreno.API()
sortkey = itemgetter(0)
num_stations = 0
for letter in string.ascii_uppercase:
#Get all stations which name starts with 'letter'
stations = api.call('autocompletaStazione', letter)
num_stations += len(stations)
print(letter, len(stations), num_stations)
for s in sorted(stations, key=sortkey):
station_name, station_id = s
station = {
'name': station_name,
'id': station_id,
'region': 'N/A',
'region_code': 'N/A',
'lat': 'N/A',
'lon': 'N/A',
'city': 'N/A'
}
#Get region code, needed to obtain station details
reg_code = api.call('regione', station_id)
if reg_code is not None:
station['region_code'] = int(reg_code)
station['region'] = region_codes.get(int(reg_code), 'N/A')
#Get station details
details = api.call('dettaglioStazione', station_id, reg_code)
if details is not None:
station['city'] = details['nomeCitta']
station['lat'] = details['lat']
station['lon'] = details['lon']
as_geojson['features'].append ({
'type': 'Feature',
'properties': {
'name': station['name'],
'id': station['id'],
'region_code': station['region_code'],
'region': station['region'],
'city': station['city']
},
'geometry': {
'type': 'Point',
'coordinates': [details['lon'], details['lat']]
}
})
as_csv.append(station)
with open(os.path.join(destdir, 'stations.geojson'), 'w') as fp:
json.dump(as_geojson, fp)
with open(os.path.join(destdir, 'stations.csv'), 'w') as fp:
csv_fields = itemgetter('name', 'id', 'region', 'region_code', 'city', 'lat', 'lon')
wr = csv.writer(fp, delimiter=',', lineterminator='\n')
wr.writerow(('name', 'id', 'region', 'region_code', 'city', 'lat', 'lon'))
for row in as_csv:
wr.writerow(csv_fields(row))