geohierarchy.maps.maplibre.render¶
MapLibreHierarchyMap: render a HierarchyMap's PMTiles via MapLibre GL JS.
This is the MapLibre counterpart to
:class:geohierarchy.maps.folium.render.HierarchyMap, targeting base-layer
parity with it: same zoom-banded levels, same
:class:~geohierarchy.maps.folium.style.ColorSpec coloring, same
click-popup/hover interactivity -- sourced from the exact same per-level
.pmtiles files HierarchyMap.build() already writes (no separate
tile-generation step). It deliberately does NOT attempt to port the full
Folium scenario editor or stats panel (route drawing, grade-separation
paint mode, Distribution/Regression/ANOVA tabs, live
window.__h3AccessOverrides recolor) -- Folium keeps sole ownership of
that until a later pass; see transitlos/map/build.py.
Which MapLibre layer type (fill/circle/line) a given hierarchy level
gets, and whether it's interactive, comes from the modular registry in
:mod:geohierarchy.maps.layers via each level's
MapLayer.layer_type -- adding a new layer type there (e.g. building
footprints) does not require touching this renderer.
MapLibreHierarchyMap(hmap)
¶
Renders an already-built :class:HierarchyMap via MapLibre GL JS + PMTiles.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hmap
|
HierarchyMap
|
A :class: |
required |
build_html(basemap=DEFAULT_BASEMAP, title='Map')
¶
Return the complete self-contained MapLibre HTML document (as a string).
The PMTiles pmtiles:// protocol is registered via
maplibregl.addProtocol per the user's spec; tile URLs are
relative to the output HTML file's own directory (same convention
Folium's output uses), so this HTML must be saved next to
hmap.tiles_dir (see :meth:save).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
basemap
|
Union[str, Dict[str, Any]]
|
Basemap name or spec. |
DEFAULT_BASEMAP
|
title
|
str
|
Page title. |
'Map'
|
save(path, basemap=DEFAULT_BASEMAP, title='Map')
¶
Write the MapLibre HTML to path.
path must be a sibling of hmap.tiles_dir (same directory),
matching the relative pmtiles://<tiles_dir_name>/<level>.pmtiles
URLs baked into the generated HTML -- exactly the convention
Folium's own HierarchyMap.save() output already relies on for
its XYZ tile URLs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
HTML file to write, next to |
required |
basemap
|
Union[str, Dict[str, Any]]
|
Basemap name or spec. |
DEFAULT_BASEMAP
|
title
|
str
|
Page title. |
'Map'
|
Returns:
| Type | Description |
|---|---|
str
|
The written path. |
save_maplibre(hmap, path, basemap=DEFAULT_BASEMAP, title='Map')
¶
Convenience wrapper: save_maplibre(hmap, "map_maplibre.html").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hmap
|
HierarchyMap
|
A built :class: |
required |
path
|
str
|
HTML file to write. |
required |
basemap
|
Union[str, Dict[str, Any]]
|
Basemap name or spec. |
DEFAULT_BASEMAP
|
title
|
str
|
Page title. |
'Map'
|
Returns:
| Type | Description |
|---|---|
str
|
The written path. |
save_multi_maplibre(multi, path, basemap=DEFAULT_BASEMAP, title='Map', radius_field_domains=None, opacity_field_domains=None, circle_fields=None, opacity_fields=None, default_circle_field=None, field_labels=None, radius_field_domains_by_res=None, circle_zoom_bands=None, special_group_levels=None, special_group_level_labels=None, special_group_name='special', special_group_fallback_level=None, special_group_fallback_zoom=5)
¶
Render a :class:~geohierarchy.maps.folium.render.MultiHierarchyMap via MapLibre.
Combines every named/overlay group's sources+layers into one MapLibre
style (each group's layer ids namespaced {group}:{level}_{gltype},
matching :class:MapLibreHierarchyMap's per-level id convention so
popup/hover lookups stay correct within a group), and adds a small
HTML/JS layer switcher: named groups as radio buttons (mutually
exclusive, mirroring Folium's FeatureGroup(overlay=False)
semantics), overlay groups as checkboxes.
radius_field_domains/opacity_field_domains/circle_fields/
opacity_fields/default_circle_field (all optional, round-11 port of
Folium's _control_panel_html's "Circle size by"/"Hex/circle/census
opacity"/"Opacity by"/"Contrast" controls -- see
transitlos.map.build._control_panel_js/_opacity_helper_js for the
formulas being matched):
- when radius_field_domains/circle_fields are given, a "Circle
size by" dropdown is added; changing it rebuilds the
_circle_radius_expr-shaped interpolate expression client-side and
applies it via map.setPaintProperty(id, 'circle-radius', ...) to
every circle-type layer this call produced (only circle/point
levels have a circle-radius paint property at all, so this never
touches fill/line layers).
- opacity_field_domains/opacity_fields add an "Opacity by"
dropdown + "Contrast" slider, applying a data-driven
fill-opacity/circle-opacity expression (gamma-shaped by
contrast, exactly mirroring opacityFromField's formula) to every
fill/circle layer this call produced.
- a "Hex/circle/census opacity" slider is always added (even with no
fields passed) as a flat multiplier on top of whichever opacity
rule (flat 0.75 baseline, or the opacity-by-field expression above)
field_labels: optional {column: display text} map for the "Circle
size by"/"Opacity by" <option> text -- e.g. {"inegi_unemployment_rate":
"Unemployment rate (%)"}. This module is renderer-agnostic and has no
concept of any study's naming conventions, so it never derives a display
name itself; a field missing from this map (or the map being omitted
entirely) falls back to the raw column name, unchanged from before.
is currently active -- same "boost/fade on top of, not instead of"
relationship Folium's own slider has.
special_group_levels (2026-09-29, explicit user request: place/
congressional-district/state-legislative-district/school-district-style
levels are PEERS, not a coarse-to-fine zoom-banded hierarchy the way
census admin levels are -- the default census dropdown auto-switches
levels purely by zoom, but these "special" levels must require an
explicit, manual pick instead). When given, names every level (within
the named group special_group_name, default "special") that should
NOT all render simultaneously just because their HierarchyMap gives
them an always-eligible zoom range -- only special_group_levels[0]
starts visible, and a new dropdown (shown only while that group is the
active shape) lets the user explicitly switch which ONE level's layers
are visible, via map.setLayoutProperty(..., 'visibility', ...) on
that level's own layer ids (group_level_layer_ids) -- the exact same
mechanism the named-group radio buttons already use one level up,
just scoped to layers within one group instead of whole groups.
special_group_level_labels gives the dropdown's <option> text
(falls back to the raw level name). Omitting special_group_levels
(the default) leaves every other named group's behavior byte-for-byte
unchanged -- this is purely additive.
special_group_fallback_level/special_group_fallback_zoom: when set (a
level named in special_group_levels, e.g. the states layer), picking any
OTHER special level shows this fallback level below
special_group_fallback_zoom and the picked level only from that zoom up
(zoom ranges switched at runtime); picking the fallback level itself shows
it at every zoom.
radius_field_domains_by_res/circle_zoom_bands (2026-09-05, real bug
fix -- "circle size legend does not change with zoom"): radius_field_domains
above is a single FLAT domain, and __applyCircleRadius used to apply
it to EVERY circle layer id -- every H3 resolution's circles -- with
the SAME expression, unconditionally, on page load. This silently
overwrote each resolution's own correctly-domain-calibrated static
paint (transitlos.map.build._score_maplibre_paint, already computed
per resolution) with one flat domain sized for whichever resolution's
data happened to be passed in, the moment the page loaded -- so circle
sizes (and the legend numbers) never actually varied by resolution/zoom
in practice, confirmed live on Boston. When given, each circle layer's
id is parsed for its H3 resolution (h3_(\d+) in the layer id) and
looked up in radius_field_domains_by_res for ITS OWN domain instead
of the flat one; circle_zoom_bands (resolution -> (minZoom, maxZoom))
lets the legend TEXT pick the domain matching the map's current zoom.
Both optional and additive -- omitting them keeps the old flat-domain
behavior for a caller that doesn't pass them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
multi
|
A built :class: |
required | |
path
|
str
|
HTML file to write. |
required |
basemap
|
Union[str, Dict[str, Any]]
|
Basemap name or spec. |
DEFAULT_BASEMAP
|
title
|
str
|
Page title. |
'Map'
|
radius_field_domains
|
Optional[Dict[str, tuple]]
|
|
None
|
opacity_field_domains
|
Optional[Dict[str, tuple]]
|
|
None
|
circle_fields
|
Optional[List[str]]
|
Fields offered in "Circle size by". |
None
|
opacity_fields
|
Optional[List[str]]
|
Fields offered in "Opacity by". |
None
|
default_circle_field
|
Optional[str]
|
Field selected first in "Circle size by". |
None
|
field_labels
|
Optional[Dict[str, str]]
|
|
None
|
radius_field_domains_by_res
|
Optional[Dict[int, Dict[str, tuple]]]
|
|
None
|
circle_zoom_bands
|
Optional[Dict[int, tuple]]
|
|
None
|
special_group_levels
|
Optional[List[str]]
|
Levels of the special group shown one at a time (picked by hand, not by zoom). |
None
|
special_group_level_labels
|
Optional[Dict[str, str]]
|
|
None
|
special_group_name
|
str
|
Name of the special group. |
'special'
|
special_group_fallback_level
|
Optional[str]
|
Special level shown below |
None
|
special_group_fallback_zoom
|
int
|
Zoom below which the fallback level is shown. |
5
|
Returns:
| Type | Description |
|---|---|
str
|
The written path. |