Skip to content

geohierarchy.maps.folium.style

Declarative styling for vector-tile layers.

Leaflet.VectorGrid styles tiles client-side via a vectorTileLayerStyles callback that receives each feature's properties, so a Python-side style can't be a plain callable -- it has to be codegen'd into a small JS snippet. :class:ColorSpec is the declarative way to build that snippet (and the matching branca colormap for the legend); a raw style_js string bypasses it entirely.

ColorSpec(column, cmap='viridis', domain=None, categories=None, kind='continuous', opacity=0.7, weight=1.0, color=None, fill=True, nodata_color='#888888') dataclass

Declarative color/style rule for one hierarchy level's vector tiles.

Attributes:

Name Type Description
column str

Feature property to color by.

cmap Any

Name of a branca/matplotlib colormap (continuous) or a list of hex colors (categorical, cycled/keyed by sorted category).

domain Optional[Tuple[float, float]]

(min, max) for continuous data. Computed from the data by the caller if not given.

categories Optional[Dict[str, str]]

Explicit category -> color mapping for kind="categorical". If omitted, categories are assigned colors from cmap in sorted order.

kind Literal['continuous', 'categorical']

"continuous" (branca linear colormap) or "categorical" (discrete swatches).

opacity float

Fill opacity.

weight float

Stroke weight (px).

color Optional[str]

Stroke color. Defaults to the fill color if not given.

fill bool

Whether polygons/points are filled.

nodata_color str

Color used when column is missing/null on a feature.

branca_colormap(gdf=None)

Build the matching :mod:branca.colormap object.

Parameters:

Name Type Description Default
gdf

Optional GeoDataFrame/DataFrame used to infer domain from self.column when it wasn't given explicitly.

None

Returns:

Type Description

A branca.colormap.ColorMap instance (linear for

continuous, step for categorical).

style_js(gdf=None, var_name='style')

Generate the JS style function body for this spec.

Returns:

Type Description
str

JS source defining function {var_name}(properties) {{ ... }}

str

returning a Leaflet path-style object, suitable for use inside

str

a vectorTileLayerStyles map.

Parameters:

Name Type Description Default
gdf

Optional level data, used to sample the colour scale when the spec has no fixed domain.

None
var_name str

Name of the JavaScript function.

'style'

maplibre_fill_color_expr(gdf=None)

Build a MapLibre GL style expression for this spec's fill color.

Mirrors :meth:style_js (same sampling/lookup logic) but targets MapLibre's declarative expression language instead of a Leaflet. VectorGrid callback, so a Folium-vector-tile level and a MapLibre PMTiles layer sourced from the same tiles reach the same visual result -- this is the piece that lets :mod:geohierarchy.maps.maplibre reach base-layer parity with :mod:geohierarchy.maps.folium without re-deriving color logic.

Returns:

Type Description
Any

A MapLibre expression (JSON-serializable list), usable directly

Any

as a "fill-color"/"circle-color"/"line-color" paint

Any

value.

Parameters:

Name Type Description Default
gdf

Optional level data, used to sample the colour scale when the spec has no fixed domain.

None

maplibre_paint(kind='polygon', gdf=None)

Build a full MapLibre paint dict for this spec.

Parameters:

Name Type Description Default
kind str

One of "polygon", "circle", "line" -- the geometry-rendering primitive, matching :class:geohierarchy.maps.layers.registry.LayerKind.

'polygon'
gdf

Optional data used to infer domain/categories (see :meth:maplibre_fill_color_expr).

None

build_vector_tile_layer_styles_js(layer_name, style_js_body)

Wrap a per-layer style function into a vectorTileLayerStyles entry.

Parameters:

Name Type Description Default
layer_name str

Vector tile layer name (matches the layer name used when encoding tiles in :mod:tiles.py, typically the level name).

required
style_js_body str

JS source produced by :meth:ColorSpec.style_js or a user-supplied raw function body.

required

Returns:

Type Description
str

A JS object-literal fragment: "<layer_name>": <function>.