%%capture
import warnings
warnings.filterwarnings("ignore")
import calitp_portfolio.magics
import folium
import geopandas as gpd
import gcsfs
import google.auth
import pandas as pd
from great_tables import GT
import gt_extras as gte
import _ct_report_utils as utils
from update_vars import DIGEST_DICT, PROCESSED_GCS, abbrev_month
credentials, _ = google.auth.default()
from dotenv import dotenv_values# Comment out and leave this cell right below pandas
# district = "AD 04"# Parameters
district = "AD 69"
def readable_district_name(district_abbrev: str):
return (
district_abbrev
.replace('AD', 'Assembly District')
.replace('SD', 'Senate District')
)
district_full_name = readable_district_name(district)%%capture_parameters
district crosswalk_url = (
f"{PROCESSED_GCS}"
f"{DIGEST_DICT.crosswalk_legislative}_2026_04.parquet"
)
filesystem = gcsfs.GCSFileSystem()
try:
# First, try an exact match using the Parquet filter.
crosswalk_legislative = pd.read_parquet(
crosswalk_url,
filters=[
("legislative_district", "==", district),
],
filesystem=filesystem,
)
# If the exact-match filter returns no rows,
# reread the needed data and use str.contains instead.
if crosswalk_legislative.empty:
crosswalk_legislative = pd.read_parquet(
crosswalk_url,
columns=[
"legislative_district",
"analysis_name",
],
filesystem=filesystem,
)
crosswalk_legislative = crosswalk_legislative[
crosswalk_legislative["legislative_district"]
.astype("string")
.str.contains(
str(district),
case=False,
regex=False,
na=False,
)
]
list_of_operators = (
crosswalk_legislative["analysis_name"]
.dropna()
.unique()
.tolist()
)
except Exception as error:
print(
f"Unable to read the legislative crosswalk for "
f"district {district}: {error}"
)
raisetry:
operator_summary_url = f"{PROCESSED_GCS}{DIGEST_DICT.operator_summary}.parquet"
operator_df = pd.read_parquet(
operator_summary_url,
filesystem = gcsfs.GCSFileSystem(),
filters=[
("Day Type", "==", "Weekday"),
("Analysis Name", "in", list_of_operators),
],
).merge(
crosswalk_legislative.rename(columns = {"analysis_name": "Analysis Name"}),
on = "Analysis Name",
how = "inner"
).reset_index(drop=True)
operator_df= operator_df.fillna(0)
except:
passtry:
fct_monthly_routes_url = f"{PROCESSED_GCS}{DIGEST_DICT.route_map}.parquet"
fct_monthly_route_df = gpd.read_parquet(
fct_monthly_routes_url,
storage_options = {"token": credentials.token},
filters=[[("Analysis Name", "in", list_of_operators)]]
).reset_index(drop=True).pipe(
utils.prep_gdf
)
except:
passdistrict_gdf = utils.load_legislative_district(district)AD 69¶
Related products: Caltrans district and transit operator reports
try:
operator_df2 = utils.create_summary_table(
operator_df, district_col = "legislative_district"
).iloc[:, :2]
except:
passtry:
display(
GT(operator_df2)
.tab_header(
title = "GTFS Summary Stats",
subtitle=f"District {district}"
)
)
except:
passLoading...
CARTO_TILE_URL = dotenv_values("../_env")["CARTO_TILE_URL"]try:
m = district_gdf.explore(
color = "#3a194f",
tiles = CARTO_TILE_URL,
attr="Basemap",
name = "Legislative District Boundary",
style_kwds = {"weight": 1, "fillOpacity": 0},
highlight_kwds = {"fillOpacity": 0.2},
tooltip=False
)
#color_map = cm.linear.Spectral_11.scale()
fct_monthly_route_df.explore(
"Route Name",
m = m,
name = "Transit Routes in Legislative District",
categorical = True,
cmap = "Spectral",
legend = False
#marker_kwds={"fill": True},
#style_kwds={"opacity": 0.5, "fillOpacity": 0.3}
)
folium.LayerControl().add_to(m)
m
display(m)
except:
print(f"The operators in {district_full_name} does not have route geometries.")Loading...
try:
gtfs_table = utils.create_operator_table(
operator_df, district_col = "legislative_district"
).drop(columns = "legislative_district").fillna(0)
except:
passtry:
display((
GT(gtfs_table.sort_values("Daily Trips", ascending=False))
.fmt_integer(
columns=[c for c in gtfs_table.columns if c not in ["Operator"]]
).tab_header(
title="Daily Weekday GTFS Stats by Operator",
subtitle=f"District {district}",
).cols_align(
columns=[c for c in gtfs_table.columns if c != "Operator"],
align="center",
).pipe(
gte.gt_color_box,
columns=["Daily Trips", "# Routes", "# Shapes", "# Stops",
"Daily Arrivals", "Arrivals per Stop"],
palette="YlGnBu",
)
))
except:
passLoading...