11import geopandas as gpd
22import pandas as pd
33
4+ from typing import Literal
5+
46from calitp_data_analysis import utils
57from utils import PROCESSED_GCS
68from shared_utils .shared_data import GCS_FILE_PATH as SHARED_GCS
79
810def postmiles_to_pems_format (
911 gdf : gpd .GeoDataFrame
1012) -> gpd .GeoDataFrame :
13+ """
14+ """
1115 rename_dict = {
1216 "route" : "freeway_id"
1317 }
@@ -23,7 +27,10 @@ def postmiles_to_pems_format(
2327 return gdf
2428
2529
26- def clean_postmiles (gdf : gpd .GeoDataFrame ):
30+ def clean_postmiles (
31+ dataset_name : Literal ["points" , "segments" ],
32+ crs : str = "EPSG:4326"
33+ ) -> gpd .GeoDataFrame :
2734 """
2835 Clean SHN postmiles dataset.
2936 Be explicit about columns we don't want to keep
@@ -34,20 +41,36 @@ def clean_postmiles(gdf: gpd.GeoDataFrame):
3441 Also, postmiles will contain information about where the postmile
3542 is located, offset, etc, which we probably don't need either
3643 """
37- drop_cols = [
38- "district" , "county" ,
39- "direction" ,
40- #route+suffix, sometimes it's 5
41- # sometimes it's 5S. either way, we have freeway_direction
42- "routes" , "rtesuffix" ,
43- "pmrouteid" ,
44- "pm" , "pmc" ,
45- "pminterval" ,
46- "pmprefix" , "pmsuffix" ,
47- "aligncode" , "pmoffset" ,
48- ]
49-
50- gdf = postmiles_to_pems_format (gdf ).drop (columns = drop_cols )
44+ if dataset_name == "points" :
45+ FILE = "state_highway_network_postmiles"
46+ elif dataset_name == "segments" :
47+ FILE = "state_highway_network_postmile_segments"
48+ else :
49+ raise ValueError (f"dataset_name must be: 'points' or 'segments'" )
50+
51+ gdf = gpd .read_parquet (
52+ f"{ SHARED_GCS } { FILE } .parquet"
53+ ).to_crs (crs ).pipe (postmiles_to_pems_format )
54+
55+ if dataset_name == "points" :
56+
57+ drop_cols = [
58+ "district" , "county" ,
59+ "direction" ,
60+ #route+suffix, sometimes it's 5
61+ # sometimes it's 5S. either way, we have freeway_direction
62+ "routes" , "rtesuffix" ,
63+ "pmrouteid" ,
64+ "pm" , "pmc" ,
65+ "pminterval" ,
66+ "pmprefix" , "pmsuffix" ,
67+ "aligncode" , "pmoffset" ,
68+ ]
69+
70+ gdf = gdf .drop (columns = drop_cols )
71+
72+ elif dataset_name == "segments" :
73+ gdf = gdf .drop (columns = ["direction" , "abs_pm" ])
5174
5275 return gdf
5376
@@ -70,12 +93,11 @@ def clean_station_freeway_info(df: pd.DataFrame) -> pd.DataFrame:
7093
7194def merge_stations_to_shn_postmiles (
7295 station_crosswalk : pd .DataFrame ,
73- postmiles : gpd .GeoDataFrame
7496) -> gpd .GeoDataFrame :
7597 merge_cols = ["freeway_id" , "freeway_direction" , "abs_pm" ]
7698
7799 station2 = clean_station_freeway_info (station_crosswalk )
78- postmiles2 = clean_postmiles (postmiles )
100+ postmiles2 = clean_postmiles (dataset_name = "points" )
79101
80102 m1 = pd .merge (
81103 station2 ,
@@ -117,17 +139,12 @@ def merge_stations_to_shn_postmiles(
117139
118140if __name__ == "__main__" :
119141
120- postmiles = gpd .read_parquet (
121- f"{ SHARED_GCS } state_highway_network_postmiles.parquet"
122- )
123-
124142 station_crosswalk = pd .read_parquet (
125143 f"{ PROCESSED_GCS } station_crosswalk.parquet" ,
126144 )
127145
128146 final = merge_stations_to_shn_postmiles (
129147 station_crosswalk ,
130- postmiles
131148 )
132149
133150 print (f"# stations in final gdf: { final .station_uuid .nunique ()} " )
0 commit comments