-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroutes_fetcher.py
273 lines (206 loc) · 7.99 KB
/
routes_fetcher.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import argparse
import sys
import os
import json
import time
from collections import deque
from enum import IntEnum
import requests
class RouteType(IntEnum):
BUS = 1
TROLLEYBUS = 2
MINIBUS_TAXI = 3
TRAM = 10
ROUTE_TYPE_TO_FOLDER_NAME = {
RouteType.BUS: 'bus',
RouteType.TROLLEYBUS: 'trolleybus',
RouteType.MINIBUS_TAXI: 'minibus_taxi',
RouteType.TRAM: 'tram',
}
FETCHED_ROUTE_TYPES = {RouteType.BUS}
def fetch_json_content(url, params=None):
headers = {
'Accept': 'application/json',
}
try:
response = requests.get(url, params=params, headers=headers)
return response.json() if response.ok else None
except requests.exceptions.ConnectionError:
return None
def fetch_route_geometry_info(route_id):
params = {
'route_id': route_id,
}
return fetch_json_content(
url='http://www.maxikarta.ru/msk/transport/query/route-geom',
params=params,
)
def fetch_route_stations_info(route_id):
params = {
'route_id': route_id,
}
return fetch_json_content(
url='http://www.maxikarta.ru/msk/transport/query/stations',
params=params,
)
def get_route_stations_essential_info(route_stations_info):
route_stations_essential_info = [
((station['lat'], station['lon']), station['name'])
for station in route_stations_info
]
return get_list_without_adjacent_identical_items(route_stations_essential_info)
def get_list_without_adjacent_identical_items(source_list):
output_list = []
for item in source_list:
if not output_list:
output_list.append(item)
continue
if item == output_list[-1]:
continue
output_list.append(item)
return output_list
def get_ordered_route_segments(source_route_segments):
"""Performs ordering of route segments in order to obtain one or more
continuous trajectories describing the route geometry.
:param source_route_segments: A list of route segments that represents a
object of type MultiLineString of the GeoJSON format. More information
about the GeoJSON format can be found here:
https://tools.ietf.org/html/rfc7946
"""
segments_deque = deque(source_route_segments)
list_of_continuous_segments = []
while segments_deque:
continuous_segments = []
start_segment = segments_deque.popleft()
continuous_segments.append(start_segment)
for current_segment in continuous_segments:
next_segments = list(
filter(
lambda segment: current_segment[0] == segment[-1],
segments_deque,
),
)
if next_segments:
next_segment = next_segments[0]
continuous_segments.append(next_segment)
segments_deque.remove(next_segment)
list_of_continuous_segments.append(continuous_segments)
return [segment for segments in list_of_continuous_segments for segment in segments]
def get_ordered_coordinates_for_closed_route(source_route_segments):
ordered_route_coordinates = []
ordered_route_segments = get_ordered_route_segments(source_route_segments)
for route_segment in ordered_route_segments:
ordered_route_segment_coordinates = [
(latitude, longitude) for longitude, latitude in reversed(route_segment)
]
ordered_route_coordinates.extend(ordered_route_segment_coordinates)
ordered_route_coordinates.reverse()
route_begin_coordinates = ordered_route_coordinates[0]
ordered_route_coordinates.append(route_begin_coordinates)
return get_list_without_adjacent_identical_items(ordered_route_coordinates)
def get_processed_route_info(
route_info, route_coordinates_info, route_stations_info):
ordered_route_coordinates = get_ordered_coordinates_for_closed_route(
source_route_segments=route_coordinates_info,
)
route_stations_essential_info = get_route_stations_essential_info(
route_stations_info=route_stations_info,
)
return {
'name': route_info['name'],
'station_start_name': route_info['station_start_name'],
'station_stop_name': route_info['station_stop_name'],
'coordinates': ordered_route_coordinates,
'stations': route_stations_essential_info,
}
def get_info_about_fetched_routes(all_routes_info, base_output_path):
fetched_routes_info = []
for route_info in all_routes_info['routes']:
route_type = route_info['type']
output_folder_name = ROUTE_TYPE_TO_FOLDER_NAME[route_type]
route_name = route_info['name']
if route_type not in FETCHED_ROUTE_TYPES:
continue
output_filepath = os.path.join(
os.path.join(base_output_path, output_folder_name),
f'{route_name}.json',
)
fetched_routes_info.append(
{
'id': route_info['route_id'],
'name': route_name,
'station_start_name': route_info['station_start_name'],
'station_stop_name': route_info['station_stop_name'],
'output_filepath': output_filepath,
},
)
return fetched_routes_info
def create_output_dirs(base_output_path):
if not os.path.exists(base_output_path):
os.makedirs(base_output_path, exist_ok=True)
for route_type in FETCHED_ROUTE_TYPES:
route_info_output_path = os.path.join(
base_output_path,
ROUTE_TYPE_TO_FOLDER_NAME[route_type],
)
if not os.path.exists(route_info_output_path):
os.mkdir(route_info_output_path)
def save_route_info(route_info, output_filepath):
with open(output_filepath, 'w') as file_object:
json.dump(route_info, file_object, ensure_ascii=False, indent=2)
def get_command_line_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'--output',
help='a base path for save info about routes (Default: routes_info)',
default='routes_info',
type=str,
)
parser.add_argument(
'--sleep',
help='timeout between requests of info about routes (secs, default: 1s)',
default=1,
type=int,
)
parser.add_argument(
'--force',
help='if set, then all existing files with info about routes will be overwritten',
action='store_true',
)
command_line_arguments = parser.parse_args()
return command_line_arguments
def main():
command_line_arguments = get_command_line_arguments()
base_output_path = command_line_arguments.output
timeout_between_requests = command_line_arguments.sleep
enable_force_mode = command_line_arguments.force
all_routes_info = fetch_json_content(
url='http://www.maxikarta.ru/msk/transport/query/routes',
)
if all_routes_info is None:
sys.exit('Could not get info about routes. Check your internet connection')
fetched_routes_info = get_info_about_fetched_routes(
all_routes_info=all_routes_info,
base_output_path=base_output_path,
)
create_output_dirs(base_output_path=base_output_path)
for route_info in fetched_routes_info:
route_id = route_info['id']
output_filepath = route_info['output_filepath']
if not enable_force_mode and os.path.exists(output_filepath):
continue
print(f'Fetching info about route #{route_info["name"]}...')
route_geometry_info = fetch_route_geometry_info(route_id=route_id)
route_stations_info = fetch_route_stations_info(route_id=route_id)
output_route_info = get_processed_route_info(
route_info=route_info,
route_coordinates_info=route_geometry_info['geom']['coordinates'],
route_stations_info=route_stations_info['stations'],
)
save_route_info(
route_info=output_route_info,
output_filepath=output_filepath,
)
time.sleep(timeout_between_requests)
if __name__ == '__main__':
main()