1+ import pandas as pd
12import parameters .assignment as param
23
4+ # Fallback logger for use within EMME
5+ try :
6+ import utils .log as log
7+ except ImportError :
8+ class FallbackLogger :
9+ def info (self , message ):
10+ print (f"[INFO] { message } " )
11+
12+ def debug (self , message ):
13+ print (f"[DEBUG] { message } " )
14+
15+ def warn (self , message ):
16+ print (f"[WARN] { message } " )
17+
18+ log = FallbackLogger ()
19+
320# Utility functions that modify a network. Functions take a network as input and return the modified network
421
522def add_bus_stops (network ):
23+ # Initialize an empty dictionary to store line IDs and maximum stop distances
24+ data = {"line_id" : [], "maximum_stop_distance" : [], "is_motorway" : [], "loops" : []}
25+ high_distance_lines = []
26+ looped_lines = []
27+
628 for line in network .transit_lines ():
7- if line .mode .id in param .stop_codes :
8- stop_codes = param .stop_codes [line .mode .id ]
9- for segment in line .segments ():
10- is_stop = segment .i_node .data2 in stop_codes
11- if line .mode .id in "de" :
12- # Non-HSL bus lines
13- not_hsl = segment .i_node .label not in param .hsl_area
14- if line .id [- 1 ] == '1' :
15- # Line starts in HSL area
16- segment .allow_alightings = not_hsl and is_stop
17- segment .allow_boardings = is_stop
18- elif line .id [- 1 ] == '2' :
19- # Line ends in HSL area
20- segment .allow_alightings = is_stop
21- segment .allow_boardings = not_hsl and is_stop
22- elif line .id [- 1 ] == '3' :
23- #Line is circular and possibly goes through HSL area
24- #not perfect solution, out-in-in-out allows for in-in routes as well
25- segment .allow_alightings = is_stop
26- segment .allow_boardings = is_stop
27- else :
28- raise ValueError (
29- "Unknown direction code for line " + line .id )
30- else :
29+ if line .mode .id in param .stop_codes :
30+ stop_codes = param .stop_codes [line .mode .id ]
31+ stop_distance = 0
32+ max_stop_distance = 0
33+ is_motorway = 0
34+ loop = 0
35+
36+ for segment in line .segments ():
37+ if segment .loop_index > 1 and loop == 0 :
38+ loop = 1
39+ looped_lines .append (line .id )
40+ segment_length = segment .link .length
41+ linktype = segment .link .type % 100
42+ if linktype in param .roadclasses and is_motorway == 0 :
43+ # Car link with standard attributes
44+ roadclass = param .roadclasses [linktype ]
45+ if roadclass .type == "motorway" :
46+ is_motorway = 1
47+
48+ stop_distance += segment_length
49+ is_stop = segment .i_node .data2 in stop_codes
50+
51+ if is_stop :
52+ if stop_distance > max_stop_distance :
53+ max_stop_distance = stop_distance
54+ stop_distance = 0
55+
56+ if line .mode .id in "de" :
57+
58+ not_hsl = segment .i_node .label not in param .hsl_area
59+ if line .id [- 1 ] == '1' :
60+ segment .allow_alightings = not_hsl and is_stop
61+ segment .allow_boardings = is_stop
62+ elif line .id [- 1 ] == '2' :
63+ segment .allow_alightings = is_stop
64+ segment .allow_boardings = not_hsl and is_stop
65+ elif line .id [- 1 ] == '3' :
3166 segment .allow_alightings = is_stop
3267 segment .allow_boardings = is_stop
68+ else :
69+ raise ValueError (
70+ "Unknown direction code for line " + line .id )
71+ else :
72+ segment .allow_alightings = is_stop
73+ segment .allow_boardings = is_stop
74+
75+ # Append data for the current line
76+ data ["line_id" ].append (line .id )
77+ data ["maximum_stop_distance" ].append (max_stop_distance )
78+ data ["is_motorway" ].append (is_motorway )
79+ data ["loops" ].append (loop )
80+
81+ if line .mode .id in "bg" and max_stop_distance > 3 and not is_motorway : # and int(line.id[0]) < 6
82+ log .debug (f"Line: { line .id } ,\t Maximum distance between consecutive stops: { max_stop_distance :.2f} " )
83+ high_distance_lines .append (line .id )
84+
85+ # TODO: Print to results folder
86+ max_stop_distances = pd .DataFrame (data )
87+
88+ if high_distance_lines :
89+ log .info (f"{ len (high_distance_lines )} HSL line(s) have a maximum stop distance greater than 3 km and no motorway sections." )
3390
34- return network
91+ if looped_lines :
92+ log .warn (f"Line(s) { looped_lines } traverse over the same links multiple times." )
93+
94+ return network
0 commit comments