-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathapis.py
More file actions
337 lines (303 loc) · 11.4 KB
/
apis.py
File metadata and controls
337 lines (303 loc) · 11.4 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import os
import json
import heapq
from geopy.distance import geodesic
from chinatravel.environment.tools.poi.apis import Poi
def get_lines_and_stations(city, SUBWAY_PATH):
stations_all = []
metro_lines = {}
with open(SUBWAY_PATH, "r", encoding="utf-8") as file:
subway_data = json.load(file)
for line in subway_data[city]:
metro_lines[line["name"]] = []
for station in line["stations"]:
lat, lon = map(float, station["position"].split(","))
metro_lines[line["name"]].append(station["name"])
stations_all.append({"name": station["name"], "position": (lon, lat)})
station_to_line = {}
for line, stations in metro_lines.items():
for station in stations:
station_to_line[station] = line
return stations_all, metro_lines, station_to_line
def build_graph(metro_lines):
graph = {}
for line, stations in metro_lines.items():
for i in range(len(stations)):
if stations[i] not in graph:
graph[stations[i]] = []
if i > 0:
graph[stations[i]].append(stations[i - 1])
if i < len(stations) - 1:
graph[stations[i]].append(stations[i + 1])
return graph
def get_line_change(station_to_line, path):
line_changes = []
for station in path:
if station in station_to_line:
line_changes.append(station_to_line[station])
def add_time(time1, hours):
hour, minu = int(time1.split(":")[0]), int(time1.split(":")[1])
time_delta = int(hours * 60)
min_new = minu + time_delta
if min_new >= 60:
hour_new = hour + int(min_new / 60)
min_new = min_new % 60
else:
hour_new = hour
if hour_new < 10:
time_new = "0" + str(hour_new) + ":"
else:
time_new = str(hour_new) + ":"
if min_new < 10:
time_new = time_new + "0" + str(min_new)
else:
time_new = time_new + str(min_new)
return time_new
def dijkstra(graph, start, end):
queue = [(0, start, [])]
seen = set()
while queue:
(cost, node, path) = heapq.heappop(queue)
if node in seen:
continue
path = path + [node]
seen.add(node)
if node == end:
return path
for next_node in graph.get(node, []):
if next_node not in seen:
heapq.heappush(queue, (cost + 1, next_node, path))
return []
def find_shortest_path(graph, start, end):
return dijkstra(graph, start, end)
def find_nearest_station(location, stations):
nearest_station = None
min_distance = float("inf")
for station in stations:
distance = geodesic(location, station["position"]).kilometers
if distance < min_distance:
min_distance = distance
nearest_station = station
return nearest_station, min_distance
def calculate_cost_taxi(distance):
if distance <= 1.8:
return 11.0
elif distance <= 10:
return 11.0 + (distance - 1.8) * 3.5
else:
return 11.0 + (10 - 1.8) * 3.5 + (distance - 10) * 4.5
def calculate_cost(distance):
if distance <= 4:
return 2
elif distance <= 9:
return 3
elif distance <= 14:
return 4
elif distance <= 21:
return 5
elif distance <= 28:
return 6
elif distance <= 37:
return 7
elif distance <= 48:
return 8
elif distance <= 61:
return 9
else:
extra_distance = distance - 61
extra_cost = (extra_distance + 14) // 15
return 9 + extra_cost
class Transportation:
def __init__(
self, base_path: str = "../../database/transportation/", en_version=False
):
file_suffix = "_en" if en_version else ""
self.city_list = [
"shanghai",
"beijing",
"shenzhen",
"guangzhou",
"chongqing",
"suzhou",
"chengdu",
"hangzhou",
"wuhan",
"nanjing",
]
self.city_list_chinese = [
"上海",
"北京",
"深圳",
"广州",
"重庆",
"苏州",
"成都",
"杭州",
"武汉",
"南京",
]
curdir = os.path.dirname(os.path.realpath(__file__))
SUBWAY_PATH = os.path.join(curdir, base_path + f"subways{file_suffix}.json")
self.city_stations_dict = {}
self.city_lines_dict = {}
self.city_station_to_line = {}
for city in self.city_list:
stations_all, metro_lines, station_to_line = get_lines_and_stations(
city, SUBWAY_PATH
)
self.city_stations_dict[city] = stations_all
self.city_lines_dict[city] = metro_lines
self.city_station_to_line[city] = station_to_line
self.graphs = {}
for city in self.city_list:
self.graphs[city] = build_graph(self.city_lines_dict[city])
self.poi_search = Poi(en_version=en_version)
def goto(self, city, start, end, start_time, transport_type, verbose=False):
if transport_type not in ["walk", "metro", "taxi"]:
return "only support transport_type in ['walk','metro','taxi']"
locationA = start
locationB = end
coordinate_A = self.poi_search.search(city, locationA)
coordinate_B = self.poi_search.search(city, locationB)
city_cn = city
if city in self.city_list_chinese:
city = self.city_list[self.city_list_chinese.index(city)]
locationA_name, locationB_name = locationA, locationB
locationA, locationB = coordinate_A, coordinate_B
transports = []
if transport_type == "walk":
distance = geodesic(locationA, locationB).kilometers
walking_speed = 5.0
time = distance / walking_speed
cost = 0.0
end_time = add_time(start_time, time)
transport = {
"start": locationA_name,
"end": locationB_name,
"mode": "walk",
"start_time": start_time,
"end_time": end_time,
"cost": cost,
"distance": distance,
}
transports.append(transport)
if verbose:
print(
"Walk Distance {:.3} kilometers, Time {:.3} hour, Cost {}¥".format(
distance, time, int(cost)
)
)
return transports
elif transport_type == "taxi":
distance = geodesic(locationA, locationB).kilometers
taxi_speed = 40.0
time = distance / taxi_speed
cost = calculate_cost_taxi(distance)
end_time = add_time(start_time, time)
transport = {
"start": locationA_name,
"end": locationB_name,
"mode": "taxi",
"start_time": start_time,
"end_time": end_time,
"cost": round(cost, 2),
"distance": round(distance, 2),
}
transports.append(transport)
if verbose:
print(
"Taxi Distance {:.3} kilometers, Time {:.2} hour, Cost {}¥".format(
distance, time, int(cost)
)
)
return transports
elif transport_type == "metro":
graph = self.graphs[city]
stationA, distanceA = find_nearest_station(
locationA, self.city_stations_dict[city]
)
stationB, distanceB = find_nearest_station(
locationB, self.city_stations_dict[city]
)
if stationA == stationB:
if verbose:
print("Too near. Walk.")
return "No solution"
return self.goto(
city_cn,
locationA_name,
locationB_name,
start_time,
transport_type="walk",
verbose=verbose,
)
shortest_path = find_shortest_path(
graph, stationA["name"], stationB["name"]
)
if stationA and stationB:
distance_between_stations = geodesic(
stationA["position"], stationB["position"]
).kilometers
subway_speed = 30.0
time_between_stations = distance_between_stations / subway_speed
walking_speed = 5.0
timeA = distanceA / walking_speed
timeB = distanceB / walking_speed
total_time = timeA + time_between_stations + timeB
cost = calculate_cost(distance_between_stations)
distance = distanceA + distance_between_stations + distanceB
end_timeA = add_time(start_time, timeA)
end_timeB = add_time(end_timeA, time_between_stations)
end_time_final = add_time(end_timeB, timeB)
end_time = end_time_final
transports.append(
{
"start": locationA_name,
"end": stationA["name"] + ("-metro station" if self.poi_search.en_version else "-地铁站"),
"mode": "walk",
"start_time": start_time,
"end_time": end_timeA,
"cost": 0,
"distance": round(distanceA, 2),
}
)
transports.append(
{
"start": stationA["name"] + ("-metro station" if self.poi_search.en_version else "-地铁站"),
"end": stationB["name"] + ("-metro station" if self.poi_search.en_version else "-地铁站"),
"mode": "metro",
"start_time": end_timeA,
"end_time": end_timeB,
"cost": cost,
"distance": round(distance_between_stations, 2),
}
)
transports.append(
{
"start": stationB["name"] + ("-metro station" if self.poi_search.en_version else "-地铁站"),
"end": locationB_name,
"mode": "walk",
"start_time": end_timeB,
"end_time": end_time,
"cost": 0,
"distance": round(distanceB, 2),
}
)
if verbose:
print(
"Walk: From starting point to metro {}, Distance: {}.".format(
stationA["name"] + "-地铁站", distanceA
)
)
print(
f"Subway: From {stationA['name'] + '-地铁站'} to {stationB['name'] + '-地铁站'}: {' -> '.join(shortest_path)}"
)
print("The cost of subway: {}¥".format(cost))
print(
"Walk: From metro {} to ending point, Distance: {}.".format(
stationB["name"] + "-地铁站", distanceB
)
)
return transports
else:
raise NotImplementedError