@@ -32,6 +32,20 @@ def _parallel_map(
3232 results .append (item )
3333 return results
3434
35+ def _haversine_km (lat1 : Any , lon1 : Any , lat2 : Any , lon2 : Any ) -> float :
36+ """Get the haversine distance between two points in kilometers."""
37+ R = 6371.0
38+ phi1 = math .radians (lat1 )
39+ phi2 = math .radians (lat2 )
40+ dphi = math .radians (lat2 - lat1 )
41+ dlambda = math .radians (lon2 - lon1 )
42+ a = (
43+ math .sin (dphi / 2.0 ) ** 2
44+ + math .cos (phi1 ) * math .cos (phi2 ) * math .sin (dlambda / 2.0 ) ** 2
45+ )
46+ c = 2 * math .atan2 (math .sqrt (a ), math .sqrt (1 - a ))
47+ return R * c
48+
3549
3650def _process_deadhead_trip_row (args : Tuple [Any , ...]) -> list [Any ]:
3751 """Worker that computes shortest-path and returns per-point shape rows.
@@ -45,29 +59,12 @@ def _process_deadhead_trip_row(args: Tuple[Any, ...]) -> list[Any]:
4559 - 'shape_pt_lat'
4660 - 'shape_dist_traveled' (cumulative km from route start)
4761 """
48-
49- def _haversine_km (lat1 : Any , lon1 : Any , lat2 : Any , lon2 : Any ) -> float :
50- R = 6371.0
51- phi1 = math .radians (lat1 )
52- phi2 = math .radians (lat2 )
53- dphi = math .radians (lat2 - lat1 )
54- dlambda = math .radians (lon2 - lon1 )
55- a = (
56- math .sin (dphi / 2.0 ) ** 2
57- + math .cos (phi1 ) * math .cos (phi2 ) * math .sin (dlambda / 2.0 ) ** 2
58- )
59- c = 2 * math .atan2 (math .sqrt (a ), math .sqrt (1 - a ))
60- return R * c
61-
6262 (
63- idx ,
6463 start_x ,
6564 start_y ,
6665 end_x ,
6766 end_y ,
6867 block_id ,
69- network_type ,
70- road_buffer_m ,
7168 ) = args
7269
7370 if ox is None or nx is None :
@@ -160,8 +157,6 @@ def add_deadhead_trips(
160157 df : gpd .GeoDataFrame ,
161158 o_col : str = "geometry_origin" ,
162159 d_col : str = "geometry_destination" ,
163- network_type : str = "drive" ,
164- road_buffer_m : int = 2000 ,
165160 n_processes : int | None = None ,
166161) -> pd .DataFrame :
167162 """Compute deadhead route shapes between origin and destination.
@@ -195,21 +190,18 @@ def add_deadhead_trips(
195190 GLOBAL_GRAPH = ox .graph_from_bbox (bbox , network_type = "drive" )
196191
197192 task_args = []
198- for idx , r in df .iterrows ():
193+ for _ , r in df .iterrows ():
199194 origin = r .get (o_col )
200195 destination = r .get (d_col )
201196 if origin is None or destination is None :
202197 continue
203198 task_args .append (
204199 (
205- idx ,
206200 float (origin .x ),
207201 float (origin .y ),
208202 float (destination .x ),
209203 float (destination .y ),
210204 r .get ("block_id" ) if "block_id" in r else None ,
211- network_type ,
212- road_buffer_m ,
213205 )
214206 )
215207
0 commit comments