@@ -419,7 +419,6 @@ def extend_trip_traces(
419419 logger .info ("Finished attaching timestamps" )
420420 return pd .concat (trips_with_timestamps_list )
421421
422-
423422def build_routee_features_with_osm (
424423 input_directory : Union [str , Path ],
425424 depot_directory : Union [str , Path ],
@@ -466,64 +465,42 @@ def build_routee_features_with_osm(
466465 )
467466 stop_times_df = feed .stop_times
468467
469- # **********---------------Add depot deadhead trips_df, shapes_df, and feed---------------**********
470- # 1.1) Add depot deadhead trips, shapes, and update feed
468+ # 2) Create synthetic trips, shapes, and stop times for deadhead to and from depot
471469 # Create depot deadhead trips
472470 deadhead_trips_df = create_depot_deadhead_trips (trips_df )
471+
473472 # Create depot deadhead stop_times and stops
474473 first_stops_gdf , last_stops_gdf = add_depot_to_blocks (
475474 trips_df , feed , path_to_depots = Path (depot_directory ) / "Transit_Depot.shp"
476475 )
477476 deadhead_stop_times_df , deadhead_stops_df = create_depot_deadhead_stops (
478477 first_stops_gdf , last_stops_gdf , deadhead_trips_df
479478 )
479+
480480 # Generate deadhead trip shapes for trips from depot to first stop
481- all_points = pd .concat (
482- [first_stops_gdf ["geometry_origin" ], first_stops_gdf ["geometry_destination" ]]
483- )
484- lons = all_points .apply (lambda p : p .x )
485- lats = all_points .apply (lambda p : p .y )
486- min_lon , max_lon = lons .min (), lons .max () # Bounding box
487- min_lat , max_lat = lats .min (), lats .max () # Bounding box
488- buffer_deg_lat = 0.018 # Roughly 2 km buffer in degrees
489- buffer_deg_lon = 0.022 # Roughly 2 km buffer in degrees
490- miny = min_lat - buffer_deg_lat
491- maxy = max_lat + buffer_deg_lat
492- minx = min_lon - buffer_deg_lon
493- maxx = max_lon + buffer_deg_lon
494481 from_depot_deadhead_shapes_df = add_deadhead_trips (
495- df = first_stops_gdf , n_processes = 1 , bbox = tuple ([ minx , miny , maxx , maxy ])
496- )
482+ df = first_stops_gdf , n_processes = 1
483+ )
497484 from_depot_deadhead_shapes_df ["shape_id" ] = from_depot_deadhead_shapes_df [
498485 "shape_id"
499486 ].apply (lambda x : "from_depot_" + x )
487+
500488 # Generate deadhead trip shapes for trips from last stop to depot
501- all_points = pd .concat (
502- [last_stops_gdf ["geometry_origin" ], last_stops_gdf ["geometry_destination" ]]
503- )
504- lons = all_points .apply (lambda p : p .x )
505- lats = all_points .apply (lambda p : p .y )
506- min_lon , max_lon = lons .min (), lons .max () # Bounding box
507- min_lat , max_lat = lats .min (), lats .max () # Bounding box
508- buffer_deg_lat = 0.018 # Roughly 2 km buffer in degrees
509- buffer_deg_lon = 0.022 # Roughly 2 km buffer in degrees
510- miny = min_lat - buffer_deg_lat
511- maxy = max_lat + buffer_deg_lat
512- minx = min_lon - buffer_deg_lon
513- maxx = max_lon + buffer_deg_lon
514489 to_depot_deadhead_shapes_df = add_deadhead_trips (
515- df = last_stops_gdf , n_processes = 1 , bbox = tuple ([ minx , miny , maxx , maxy ])
490+ df = last_stops_gdf , n_processes = 1
516491 )
517492 to_depot_deadhead_shapes_df ["shape_id" ] = to_depot_deadhead_shapes_df [
518493 "shape_id"
519494 ].apply (lambda x : "to_depot_" + x )
495+
520496 # Combine all deadhead shapes
521497 deadhead_shapes_df = pd .concat (
522498 [from_depot_deadhead_shapes_df , to_depot_deadhead_shapes_df ], ignore_index = True
523499 )
524500
525501 # Update trips_df, shapes_df, and feed
526- # Before updating, update deadhead_trips_df as some blocks may have the same first and last stop therefore won't shown in deadhead_shapes_df
502+ # Before updating, update deadhead_trips_df as some blocks may have the same first
503+ # and last stop therefore won't shown in deadhead_shapes_df
527504 deadhead_trips_df = deadhead_trips_df [
528505 deadhead_trips_df ["shape_id" ].isin (deadhead_shapes_df ["shape_id" ].unique ())
529506 ]
@@ -536,9 +513,7 @@ def build_routee_features_with_osm(
536513 [feed .stop_times , deadhead_stop_times_df ], ignore_index = True
537514 )
538515 feed .stops = pd .concat ([feed .stops , deadhead_stops_df ], ignore_index = True )
539- # **********---------------End of adding depot deadhead trips_df, shapes_df, and feed---------------**********
540516
541- # **********---------------Add between trip deadhead trips_df, shapes_df, and feed---------------**********
542517 # 1.2) Add between trip deadhead trips, shapes, and update feed
543518 # Create between trip deadhead trips
544519 betweenTrip_deadhead_trips_df = create_betweenTrip_deadhead_trips (
@@ -550,26 +525,13 @@ def build_routee_features_with_osm(
550525 betweenTrip_deadhead_stops_df ,
551526 betweenTrip_ODs ,
552527 ) = create_betweenTrip_deadhead_stops (feed , betweenTrip_deadhead_trips_df )
553- # Generate deadhead trip shapes for trips from depot to first stop
554- all_points = pd .concat (
555- [betweenTrip_ODs ["geometry_origin" ], betweenTrip_ODs ["geometry_destination" ]]
556- )
557- lons = all_points .apply (lambda p : p .x )
558- lats = all_points .apply (lambda p : p .y )
559- min_lon , max_lon = lons .min (), lons .max () # Bounding box
560- min_lat , max_lat = lats .min (), lats .max () # Bounding box
561- buffer_deg_lat = 0.018 # Roughly 2 km buffer in degrees
562- buffer_deg_lon = 0.022 # Roughly 2 km buffer in degrees
563- miny = min_lat - buffer_deg_lat
564- maxy = max_lat + buffer_deg_lat
565- minx = min_lon - buffer_deg_lon
566- maxx = max_lon + buffer_deg_lon
528+
567529 # Remove ODs with same origin and destination
568530 betweenTrip_ODs = betweenTrip_ODs [
569531 betweenTrip_ODs .geometry_origin != betweenTrip_ODs .geometry_destination
570532 ]
571533 betweenTrip_deadhead_shapes_df = add_deadhead_trips (
572- df = betweenTrip_ODs , n_processes = 1 , bbox = tuple ([ minx , miny , maxx , maxy ])
534+ df = betweenTrip_ODs , n_processes = 1
573535 )
574536
575537 # Update trips_df, shapes_df, and feed
0 commit comments