@@ -88,43 +88,57 @@ def preprocess_graph(G: MultiDiGraph, boundary_buffer_dist: float = 200) -> Mult
8888 networkx.MultiDiGraph
8989 The preprocessed graph.
9090 """
91-
91+
92+ # Add edge bearings
9293 G = ox .add_edge_bearings (G )
9394
95+ # Checks if the graph is projected, if not it projects it to the local UTM.
9496 if not ox .projection .is_projected (G .graph ["crs" ]):
9597 G = ox .project_graph (G )
96-
98+
99+ # A geodataframe is created from the projected graph for extracting the
100+ # boundary of the graph. And, if missing, extract the lenghts of the geometies
97101 edges_gdf = ox .graph_to_gdfs (G , nodes = False , edges = True )
98102 names_attribute = edges_gdf .columns
99103
104+ # Checks if the lenght is missing, e.g. the graph was not created with osmnx, and adds it if necessary
100105 if "length" not in names_attribute :
101106 logger .warning (
102107 "Length attribute is not present in the graph. Calculating lengths from geometry."
103108 )
104109 edges_lengths = edges_gdf .geometry .length
105110 set_edge_attributes (G , edges_lengths .to_dict (), name = "length" )
106111
112+ # Checks if the maxspeed attribute is missing, e.g. this is not a graph from OSM data.
113+ # Adds a default maxspeed of 30 km/h if it is missing, as this is needed for calculating
114+ # travel times.
107115 if "maxspeed" not in names_attribute :
108116 logger .warning (
109- "maxspeed attribute is not present in the graph. Global max speeds are going to be set to 30."
117+ "maxspeed attribute is not present in the graph. Global max speeds are going to be set to 30 km/h ."
110118 )
111119 set_edge_attributes (G , 30 , name = "maxspeed" )
112120
121+ # Add speeds and travel times using osmnx
113122 G = ox .add_edge_speeds (G )
114123 G = ox .add_edge_travel_times (G )
115124
116125
126+ # Create a convex hull of the graph to calculate the area and set the boundary for saving the graph.
127+ # This is implemented with an if statement to support older versions of geopandas < 1.0.0
117128 if hasattr (edges_gdf .geometry , "unary_union" ):
118129 edge_union = edges_gdf .geometry .unary_union
119130 else :
120131 edge_union = edges_gdf .geometry .union_all ()
121132
133+ # A convex hull is created arround the bufferred edges.
122134 enclosing_hull = edge_union .buffer (boundary_buffer_dist ).convex_hull
123135
136+ # Assigning the attributes to the graph
124137 G .graph .update (basic_graph_stats (G , area = enclosing_hull .area ))
125138 G .graph ["area" ] = enclosing_hull .area
126139 G .graph ['boundary' ] = enclosing_hull
127140
141+ # Adding the edge population data
128142 add_edge_population (G )
129143
130144 return G
0 commit comments