transitlos.map.build¶
Lightweight, tile-based folium map builder shared by every city.
Uses geohierarchy.maps.folium.render.HierarchyMap/MultiHierarchyMap as the
tile-based rendering backbone (never raw embedded GeoJSON): one zoom-banded
level per H3 resolution (or, for US/census cities, per census geometry level)
so only one resolution's tiles are ever fetched at a time.
Three kinds of layer
- Shape (mutually exclusive, native Leaflet radio via
MultiHierarchyMap's base-layer groups):"hexagons","circles", and -- only for cities with census data --"census"(itself a multi-level hierarchy from the coarsest to the finest available census geometry). - Streets (independent checkbox overlay): always rendered above the shape layer, togglable on its own regardless of which shape is active.
- Development opportunity (independent checkbox overlay, US/census cities only): h3 hexagon borders only, shown above streets when active.
A small custom control panel (opacity-by-field, circle-size-by-field, background-map choice, B&W basemap) and a legend are injected as extra Leaflet controls stacked below the native layer control. All level_of_service coloring uses a fixed red (bad)-yellow-green (good) scale.
register_relative_fields(names)
¶
Declare columns as relative (intensive) values: offered for colour/opacity/regression, never as a count (circle size, distribution).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
names
|
Column names. |
required |
is_relative_field(col)
¶
True if col's name marks it as a share/rate/density/median-style intensive value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
relative_fields(fields)
¶
fields filtered to relative (share/rate/density/...) columns, order preserved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
Sequence[str]
|
Column names. |
required |
field_unit_suffix(col)
¶
" (pop/km²)", " (%)", " (min)", " (km/h)", or "" -- appended to a field's display label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
source_label(col)
¶
Display name of the dataset a column comes from ("derived" for pipeline-computed, unprefixed columns).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
strip_source_prefix(col)
¶
col with its known census-source prefix removed, or col unchanged if none matches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
register_field_labels(labels)
¶
Study-specific display names (logical column name, i.e. without its source prefix -> label), e.g. English names for columns whose raw names are in Spanish. Used by every label function of the pages built afterwards in this process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
Dict[str, str]
|
|
required |
display_field_name(col)
¶
col's cross-country-comparable display name: source prefix stripped, then humanized.
2026-09-28, explicit user request: "the worldpop columns should always
have worldpop in the column name even in dropdown boxes in all maps" --
every OTHER source's prefix is deliberately stripped for cross-country
comparability (see _SOURCE_PREFIXES's docstring -- e.g. cbs_population/
inegi_population both just read "Population"), but WorldPop columns
specifically must keep a visible "WorldPop" marker in their display
label everywhere a column name is shown (dropdowns, legends, metadata
"Column" name), since a map can have BOTH a real census count and a
WorldPop-derived one for the same logical field (e.g. population vs
worldpop_residents) and losing the source label there would make them
indistinguishable in the UI.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The display name. |
field_label(col)
¶
A field's dropdown/legend display label, with its unit suffix (see field_unit_suffix).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
col
|
str
|
Column name. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The label with its unit. |
absolute_fields(fields)
¶
fields filtered to absolute raw-count columns -- the exact complement of relative_fields.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
Sequence[str]
|
Column names. |
required |
build_city_map(h3_by_resolution, edges_gdf, tiles_dir, out_html, score_col='level_of_service', street_min_zoom=14, census_by_level=None, circle_fields=None, opacity_fields=None, stats_by_area=None, is_us=False, stops_chunks=None, routes_chunks=None, stops_gdf=None, routes_gdf=None, region='global', skip_tile_build=False, external_tile_writers=None, force_development_tiles=False, force_census_tiles=False, stats_scope_cfg=None, development_gdf=None, use_pmtiles=True, renderer='maplibre', enable_place_comparison=False, downloads_manifest=None, share_source_map=None, country=None, special_levels=None, special_level_labels=None, include_streets=True, include_development=True)
¶
Build and save one city's transit-LOS map (include_streets / include_development = False: no street / development layer).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
h3_by_resolution
|
Dict[int, GeoDataFrame]
|
Mapping of H3 resolution -> GeoDataFrame (h3
polygon geometry + |
required |
edges_gdf
|
GeoDataFrame
|
Street-edges GeoDataFrame with |
required |
tiles_dir
|
str
|
Directory vector tiles are written to. |
required |
out_html
|
str
|
Output map HTML path. |
required |
score_col
|
str
|
Column name colored (red-yellow-green) on every level. |
'level_of_service'
|
street_min_zoom
|
int
|
Minimum zoom streets become visible at. |
14
|
census_by_level
|
Optional[Dict[str, GeoDataFrame]]
|
Optional mapping of census level name -> GeoDataFrame
with the same geometry/score/field conventions as
|
None
|
circle_fields
|
Optional[List[str]]
|
Numeric columns selectable in the "Circle size by" dropdown. Defaults to every numeric, non-identifier column found on the finest H3 resolution's GeoDataFrame. |
None
|
opacity_fields
|
Optional[List[str]]
|
Numeric columns selectable in the "Opacity by"
dropdown (affects hexagons/circles/census, never streets).
Defaults to the same list as |
None
|
stats_by_area
|
Optional[Dict[str, GeoDataFrame]]
|
Optional |
None
|
is_us
|
bool
|
Whether |
False
|
development_gdf
|
Optional[GeoDataFrame]
|
Optional polygon GeoDataFrame (e.g. census block
groups) with an |
None
|
stops_gdf
|
Optional[GeoDataFrame]
|
Optional stop-level GeoDataFrame (point geometry) with a
|
None
|
routes_gdf
|
Optional[GeoDataFrame]
|
Optional route-level GeoDataFrame -- one Line/
MultiLineString feature per route -- as produced by
|
None
|
region
|
str
|
|
'global'
|
external_tile_writers
|
Optional[Dict[tuple, Any]]
|
Optional |
None
|
skip_tile_build
|
bool
|
If True, skip regenerating vector tiles entirely
and reuse whatever already exists on disk under |
False
|
enable_place_comparison
|
bool
|
Whether the stats panel's "Place rank" tab
(cross-city, population-weighted median level_of_service, ranked
across the |
False
|
use_pmtiles
|
bool
|
If True (default), generate and use PMTiles (.pmtiles) format for vector tiles. PMTiles are single-file archives that are faster to generate and easier to host. If False, use legacy XYZ PBF directory structure for backward compatibility. |
True
|
renderer
|
str
|
"maplibre" (default) or "folium". MapLibre reads the
.pmtiles written by the tile build directly -- no XYZ/pbf
extraction step -- and gives fast base-layer parity (per-level
coloring, popups, hover, layer switcher) but NOT the scenario
editor or stats panel. Pass "folium" to get the full
Leaflet-based editor (route drawing, grade-separation paint,
live recolor, Distribution/Regression/ANOVA tabs) -- Folium's
editor still requires XYZ PBF tiles internally (extracted from
the same .pmtiles by |
'maplibre'
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the saved HTML file. |
Note
HierarchyMap embeds tiles_dir verbatim into the browser-side
Leaflet tile URL, so an absolute filesystem path would produce a
map.html that only loads correctly if a web server happened to be
rooted at /. tiles_dir is converted to a path relative to
out_html's directory, and tile-writing is done with the working
directory temporarily switched to that same directory so on-disk
tiles land exactly where the relative URL expects them.
write_city_popup9(out_html_abs, cells9, stats_by_area)
¶
results/popup9/ of a city map (transitlos.map.popup_cells): the populated res-9 cells with the stats panel's columns, and per
analysis area (urban area, metro area, municipality, city core: a res-9 cell belongs to an area when its res-8 parent does) the
quantile tables of the popup's percentiles. The cells also go to results/map_inputs_checkpoint/popup9_cells.parquet for the
all-cities table of the combined map (world_quantiles9.json).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_html_abs
|
str
|
Absolute path of the city's |
required |
cells9
|
Res-9 cells with the map's columns. |
required | |
stats_by_area
|
Dict[str, GeoDataFrame]
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
The |
write_stats_data_json(out_html_abs, stats_by_area)
¶
Write results/stats_data.json next to results/*.parquet: the same payload baked into map.html.
map.html already inlines this exact payload via _stats_json_data
(baked into window.__statsData so the in-page stats panel needs no
network round-trip). This sibling file re-derives the identical
column-oriented {"metro": {...}, "core": {...}} JSON and writes it to
<city_dir>/results/stats_data.json so other pages -- the combined
multi-city map, or another city's stats panel doing a cross-city
comparison -- can fetch() just this small JSON instead of loading the
whole (tile-heavy) map.html.
Uses the same field selection as the inlined payload
(_stats_field_candidates/_stats_count_fields over the finest area's
columns) so the two stay in sync.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
out_html_abs
|
str
|
Absolute path of the city's |
required |
stats_by_area
|
Dict[str, GeoDataFrame]
|
|
required |
Returns:
| Type | Description |
|---|---|
str
|
Path of the written JSON. |
build_place_rank_panel_block(place_manifest, weight_columns=(), fetch_prefix='')
¶
The bottom-left 📊 stats popup, reduced to ONLY its "Place rank" tab.
Public entry point for a caller with no per-place stats_by_area
GeoDataFrame of its own -- concretely, CS_transitLOS/code/combined_map.py
(a lightweight page generator with no geopandas/per-h3-cell data,
assembling a page that iframes other already-built city map.htmls
rather than owning any map data itself). It reuses the exact same
_stats_panel_html/_stats_panel_js a real per-city build_city_map
call uses for its own "Place rank" tab -- one implementation, two
call sites -- with the other four tabs (Distribution/Regression/ANOVA/
Discretization) omitted entirely via place_rank_only=True, since this
caller has no per-place h3 data to feed them.
place_manifest: [{"key": ..., "label": ...}, ...] -- the live roster
(e.g. from combined_map.py's own _available_cities()), NOT this
module's hand-maintained _DEFAULT_CS_PLACE_MANIFEST fallback.
weight_columns: candidate weight-column keys for the tab's own
weight-column
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
place_manifest
|
List[Dict[str, str]]
|
|
required |
weight_columns
|
Sequence[str]
|
Candidate weight columns. |
()
|
fetch_prefix
|
str
|
Prefix of the |
''
|
Returns:
| Type | Description |
|---|---|
str
|
HTML and script of the panel. |
load_edit_params(path=None)
¶
Read default_edit_params.json (the editor's config) as a dict.
Looked up relative to this file (<repo>/transitLOS/default_edit_params.json)
unless an explicit path is given, so a study can pass its own copy in
without touching code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Optional[str]
|
Optional path of the JSON file. |
None
|
Returns:
| Type | Description |
|---|---|
dict
|
The editor parameters (the built-in fallback if the file cannot be read). |
build_download_panel_block(manifest_json, base_path='results', place_options=None)
¶
Shared HTML+JS for the "Download data" panel.
Used by both _inject_maplibre_download_panel_into_saved_html (one
city's own map, place_options=None) and combined_map.py (which
passes place_options -- one {value, label, base_path} per city --
to add the place dropdown described in the task; base_path there is
each city's own relative path from the combined map's HTML location,
e.g. "../beerseba/results", since the combined map has no downloads
of its own to point at).
manifest_json is a JSON object (baked in, same pattern as
_stats_json_data) shaped:
{"scopes": ["metro","core"], "h3_resolutions": [5,7,9,11],
"census_levels": ["county","tract",...], "has_stops": true,
"has_routes": true}
For the combined map, manifest_json is instead a JSON mapping of
place value -> that same per-place object (window.__downloadManifests),
since different cities can have different resolutions/levels/census
availability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
manifest_json
|
str
|
JSON of what can be downloaded (see above). |
required |
base_path
|
str
|
Relative path of the results folder. |
'results'
|
place_options
|
Optional[List[dict]]
|
Optional |
None
|
Returns:
| Type | Description |
|---|---|
str
|
HTML and script of the panel. |