Layers¶
Every drawable is a Layer added to a map with
mappyng.Map.add(). Layers carry their data and parameters, validate
themselves, and render into the map when it is built.
Base classes¶
- class mappyng.layers.Layer(gdf=None, *, z_index=0, visible=True, **params)[source]¶
Bases:
ABCAbstract description of a cartographic layer.
A layer holds its data (
gdf) and parameters (params) and draws nothing untilrender()is called. It is validated at construction and at everyupdate().- Parameters:
gdf (GeoDataFrame, optional) – The layer data. Required for data layers (
requires_gdf); ignored by reference layers such as the basemap.z_index (int, default 0) – Render order. Layers with equal
z_indexkeep insertion order.visible (bool, default True) – Whether the layer is drawn.
**params – Layer-specific parameters (see each concrete subclass).
- id¶
Identifier, assigned by the map when the layer is added.
- Type:
str or None
- kind¶
Identifier prefix for the layer family (class attribute).
- Type:
str
- classmethod from_dict(data, gdf=None)[source]¶
Rebuild a layer from
to_dict()output.- Parameters:
data (dict) – A serialised layer.
gdf (GeoDataFrame, optional) – Geometry to reattach (the serialised form does not carry it).
- Return type:
- kind: str = 'layer'¶
Identifier prefix for this layer family.
- abstractmethod render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = False¶
Whether a non-empty
gdfis required at construction.
- to_dict()[source]¶
Serialise the layer’s intent (not its geometry).
The geometry is flagged by
data['in_memory']; persisting it by reference or path is handled by serialisation.- Return type:
Dict[str,Any]
- update(**kwargs)[source]¶
Update parameters and re-validate. Returns
self.z_indexandvisibleare treated as layer metadata; any other keyword updatesparams.- Raises:
LayerStateError – If the layer has already been rendered.
- Return type:
- class mappyng.layers.RenderContext(map)[source]¶
Bases:
objectCarries what a
Layerneeds to render itself.The context is the decoupling point between a layer and the map: a layer never reaches into
Mapdirectly, it goes through the context. It transports the host map (whose viewports, CRS, internal state and font scaling a layer reaches through).- Parameters:
map (Map) – The host map the layer draws into.
- exception mappyng.layers.LayerStateError[source]¶
Bases:
RuntimeErrorRaised when a layer is mutated after it has been rendered.
A layer’s parameters describe an intent that is consumed at render time. Once rendered, changing them would silently desynchronise the object from the SVG already produced, so
Layer.update()is refused.
Thematic layers¶
- class mappyng.ChoroplethLayer(gdf=None, *, column=<unset>, cmap=<unset>, reverse=<unset>, method=<unset>, num_classes=<unset>, stroke=<unset>, stroke_width=<unset>, legend=<unset>, dissolve=<unset>, simplify=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerChoropleth (graduated colour) layer.
Maps a relative quantity (rate, ratio, density) to colour value.
- Parameters:
gdf (GeoDataFrame) – Polygons with a numeric column.
column (str) – Column to classify (required).
cmap (str or list, optional) – Colour ramp name or explicit list of hex colours (default from defaults). With
"StdMean"a diverging ramp ("RdBu","BrBG","RdYlGn") reads the two sides of the mean apart; a sequential ramp raises a warning.reverse (bool, optional) – Reverse the colour order, so the lowest class takes the ramp’s last colour (default False). Applies to named ramps and explicit lists.
method (str, optional) – Classification scheme:
"Quantiles","EqualInterval","FisherJenks","Q6","StdMean"or"PrettyBreaks".num_classes (int, optional) – Number of classes, usually 4 to 7 (default 5).
"StdMean"supports 5 or 7 (the central class straddles the mean, so the count is odd); any other count snaps to the nearest valid one with a warning."Q6"always yields 6.stroke (str, optional) – Outline colour of each area as
#RRGGBB(default from style).stroke_width (float, optional) – Outline width (default from style).
legend (dict, optional) – Legend configuration (see
ChoroplethLegendConfig), or a legend config instance.dissolve (bool, optional) – Merge adjacent areas that share a class before drawing (default False).
simplify (None, str or float, optional) – Geometry simplification:
None(default, no simplification),"auto"(half a pixel at the render scale) or a tolerance in the data CRS units.on_zoom (bool, optional) – Draw on the zoom viewport (default True).
on_cartouches (bool, optional) – Draw on cartouche viewports (default True).
z_index (int, optional) – Render order (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(ChoroplethLayer(gdf, column="density", method="FisherJenks"))
- __init__(gdf=None, *, column=<unset>, cmap=<unset>, reverse=<unset>, method=<unset>, num_classes=<unset>, stroke=<unset>, stroke_width=<unset>, legend=<unset>, dissolve=<unset>, simplify=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'choropleth'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = True¶
Whether a non-empty
gdfis required at construction.
- class mappyng.ProportionalLayer(gdf=None, *, column=<unset>, fill=<unset>, fill_opacity=<unset>, stroke=<unset>, stroke_width=<unset>, max_radius=<unset>, reference_value=<unset>, reference_radius=<unset>, legend=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerProportional symbols layer.
Maps an absolute quantity (stock) to symbol size, preserving magnitude where colour value would flatten it.
- Parameters:
gdf (GeoDataFrame) – Geometries with a numeric column.
column (str) – Column driving symbol size (required).
fill (str, optional) – Symbol fill colour as
#RRGGBB(default from defaults).fill_opacity (float, optional) – Symbol fill opacity in
[0, 1](default from defaults).stroke (str, optional) – Symbol outline colour (default from defaults).
stroke_width (float, optional) – Symbol outline width (default from defaults).
max_radius (float, optional) – Radius in pixels of the largest symbol (default from defaults).
reference_value (float, optional) – Value mapped to
reference_radiusfor the legend. Defaults to the data maximum.reference_radius (float, optional) – Radius in pixels for
reference_value. Defaults tomax_radius.legend (dict, optional) – Legend configuration (see
ProportionalLegendConfig).on_zoom (bool, optional) – Draw on the zoom viewport (default True).
on_cartouches (bool, optional) – Draw on cartouche viewports (default True).
z_index (int, optional) – Render order (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(ProportionalLayer(gdf, column="population", max_radius=30))
- __init__(gdf=None, *, column=<unset>, fill=<unset>, fill_opacity=<unset>, stroke=<unset>, stroke_width=<unset>, max_radius=<unset>, reference_value=<unset>, reference_radius=<unset>, legend=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'proportional'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = True¶
Whether a non-empty
gdfis required at construction.
- class mappyng.SituationLayer(gdf=None, *, column=<unset>, symbol=<unset>, marker=<unset>, fill=<unset>, size=<unset>, stroke=<unset>, stroke_width=<unset>, fill_opacity=<unset>, inner_fill=<unset>, inner_stroke=<unset>, label=<unset>, legend=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerSituation (qualitative point/area) layer.
- Parameters:
gdf (GeoDataFrame) – Geometries to draw.
column (str, optional) – Category column. When set together with
symbol, each category gets its own marker.symbol (dict, optional) – Mapping of category value to a per-category marker style.
marker (str, optional) – Marker shape used in uniform mode (default
"circle").fill (str, optional) – Marker fill colour as
#RRGGBB(default"#e31a1c").size (float, optional) – Marker size in pixels (default 8).
stroke (str, optional) – Marker outline colour (default
"#ffffff").stroke_width (float, optional) – Marker outline width (default 0.5).
fill_opacity (float, optional) – Marker fill opacity in
[0, 1](default 1).inner_fill (str, optional) – Fill colour of an inner marker, for layered symbols.
inner_stroke (str, optional) – Outline colour of the inner marker.
label (str, optional) – Column holding a short text drawn next to each marker.
legend (dict, optional) – Legend configuration (see
SituationLegendConfig).on_zoom (bool, optional) – Draw on the zoom viewport (default True).
on_cartouches (bool, optional) – Draw on cartouche viewports (default True).
z_index (int, optional) – Render order (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(SituationLayer(gdf, marker="square", fill="#1f78b4"))
- __init__(gdf=None, *, column=<unset>, symbol=<unset>, marker=<unset>, fill=<unset>, size=<unset>, stroke=<unset>, stroke_width=<unset>, fill_opacity=<unset>, inner_fill=<unset>, inner_stroke=<unset>, label=<unset>, legend=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'situation'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = True¶
Whether a non-empty
gdfis required at construction.
- class mappyng.LabelLayer(gdf=None, *, column=<unset>, target=<unset>, font_size=<unset>, font_family=<unset>, font_weight=<unset>, color=<unset>, halo_color=<unset>, halo_width=<unset>, priority_col=<unset>, min_priority=<unset>, max_labels=<unset>, initial_position=<unset>, candidate_positions=<unset>, label_offset=<unset>, max_displacement=<unset>, avoid_overlap=<unset>, allow_hide=<unset>, leader_lines=<unset>, leader_threshold=<unset>, leader_color=<unset>, leader_width=<unset>, wrap=<unset>, wrap_max_chars=<unset>, numbered_above=<unset>, numbered_legend_position=<unset>, numbered_circle_r=<unset>, numbered_circle_fill=<unset>, numbered_circle_stroke=<unset>, numbered_legend_bg=<unset>, numbered_legend_bg_opacity=<unset>, numbered_legend_border=<unset>, numbered_legend_title=<unset>, n_sweeps=<unset>, initial_temperature=<unset>, final_temperature=<unset>, seed=<unset>, w_LL=<unset>, w_LO=<unset>, w_LC=<unset>, w_LA=<unset>, w_D=<unset>, w_OR=<unset>, w_OOB=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerText label layer placed by simulated annealing.
- Parameters:
gdf (GeoDataFrame) – Geometries to label.
column (str) – Column holding the label text (required).
target (str, optional) – Viewport to place labels on:
"main"(default),"zoom"or"cartouche:<index>".font_size (float, optional) – Label font size in pixels (default 10).
font_family (str, optional) – Font family (default
"sans-serif").font_weight (str, optional) – Font weight (default
"normal").color (str, optional) – Text colour as
#RRGGBB(default"#000000").halo_color (str or None, optional) – Halo (outline) colour;
Nonedisables the halo.halo_width (float, optional) – Halo width in pixels (default 2).
priority_col (str, optional) – Column ranking which labels are placed first (higher first).
min_priority (float, optional) – Skip rows whose priority is below this threshold.
max_labels (int, optional) – Hard cap on the number of labels placed.
initial_position (str, optional) – Starting candidate position (default
"NE").candidate_positions (list of str, optional) – Candidate positions tried around each anchor.
label_offset (float, optional) – Distance from anchor to label edge in pixels (default 4).
max_displacement (float, optional) – Maximum displacement before a label is hidden (default 40).
avoid_overlap (bool, optional) – Enable label-label overlap avoidance (default True).
allow_hide (bool, optional) – Allow hiding labels that cannot be placed (default True).
leader_lines (bool, optional) – Draw leader lines for displaced labels (default True).
leader_threshold (float, optional) – Displacement (in label heights) that triggers a leader line.
leader_color (str, optional) – Leader line colour (default
"#666666").leader_width (float, optional) – Leader line width (default 0.5).
wrap (bool, optional) – Wrap long labels at dashes (default True).
wrap_max_chars (int, optional) – Maximum characters per line before wrapping (default 12).
numbered_above (int, optional) – Switch to numbered-circle mode past this label count.
numbered_legend_position (dict, optional) – Legend placement
{"x": frac, "y": frac}in figure fractions.numbered_circle_r (float, optional) – Numbered circle radius in pixels (default 5).
numbered_circle_fill (str, optional) – Numbered circle fill colour (default
"#ffffff").numbered_circle_stroke (str, optional) – Numbered circle outline colour (default
"#333333").numbered_legend_bg (str, optional) – Numbered legend background colour (default
"white").numbered_legend_bg_opacity (float, optional) – Numbered legend background opacity (default 0.88).
numbered_legend_border (str, optional) – Numbered legend border colour (default
"#cccccc").numbered_legend_title (str, optional) – Numbered legend title text.
n_sweeps (int, optional) – Number of annealing sweeps (default 50).
initial_temperature (float, optional) – Annealing start temperature (default 1.0).
final_temperature (float, optional) – Annealing end temperature (default 0.01).
seed (int, optional) – Random seed for reproducible placement. Defaults to the map seed.
w_LL, w_LO, w_LC, w_LA, w_D, w_OR, w_OOB (float, optional) – Energy weights tuning the placement: label-label, label-obstacle, label-chrome, label-anchor, distance, orientation bias and out-of-bounds penalties.
z_index (int, optional) – Render order (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(LabelLayer(gdf, column="name", priority_col="population", ... max_labels=20, font_size=9))
- __init__(gdf=None, *, column=<unset>, target=<unset>, font_size=<unset>, font_family=<unset>, font_weight=<unset>, color=<unset>, halo_color=<unset>, halo_width=<unset>, priority_col=<unset>, min_priority=<unset>, max_labels=<unset>, initial_position=<unset>, candidate_positions=<unset>, label_offset=<unset>, max_displacement=<unset>, avoid_overlap=<unset>, allow_hide=<unset>, leader_lines=<unset>, leader_threshold=<unset>, leader_color=<unset>, leader_width=<unset>, wrap=<unset>, wrap_max_chars=<unset>, numbered_above=<unset>, numbered_legend_position=<unset>, numbered_circle_r=<unset>, numbered_circle_fill=<unset>, numbered_circle_stroke=<unset>, numbered_legend_bg=<unset>, numbered_legend_bg_opacity=<unset>, numbered_legend_border=<unset>, numbered_legend_title=<unset>, n_sweeps=<unset>, initial_temperature=<unset>, final_temperature=<unset>, seed=<unset>, w_LL=<unset>, w_LO=<unset>, w_LC=<unset>, w_LA=<unset>, w_D=<unset>, w_OR=<unset>, w_OOB=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'labels'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = True¶
Whether a non-empty
gdfis required at construction.
- class mappyng.VectorLayer(gdf=None, *, position=<unset>, z=<unset>, fill=<unset>, stroke=<unset>, stroke_width=<unset>, fill_opacity=<unset>, stroke_opacity=<unset>, stroke_linecap=<unset>, stroke_dasharray=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerRaw vector layer drawn with explicit styling.
- Parameters:
gdf (GeoDataFrame) – Geometries to draw.
position (str, optional) –
"background"(default) or"top"relative to the data.z (int, optional) – Explicit stacking order, overriding
position.fill (str, optional) – Fill colour as
#RRGGBB(default"none").stroke (str, optional) – Stroke colour (default from style).
stroke_width (float, optional) – Stroke width (default from style).
fill_opacity (float, optional) – Fill opacity in
[0, 1].stroke_opacity (float, optional) – Stroke opacity in
[0, 1]. A wide faint stroke under a thin bright one fakes a glowing line.stroke_linecap (str, optional) –
"butt","round"or"square".stroke_dasharray (str, optional) – SVG dash pattern, e.g.
"4 2".on_zoom (bool, optional) – Draw on the zoom viewport (default False).
on_cartouches (bool, optional) – Draw on cartouche viewports (default False).
z_index (int, optional) – Render order among layers (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(VectorLayer(gdf, stroke="#333", stroke_width=0.4, ... position="top"))
- __init__(gdf=None, *, position=<unset>, z=<unset>, fill=<unset>, stroke=<unset>, stroke_width=<unset>, fill_opacity=<unset>, stroke_opacity=<unset>, stroke_linecap=<unset>, stroke_dasharray=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'vector'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = True¶
Whether a non-empty
gdfis required at construction.
Reference layers¶
- class mappyng.BasemapLayer(gdf=None, *, fill=<unset>, stroke=<unset>, stroke_width=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerPlain background drawn from the map’s main GeoDataFrame.
- Parameters:
fill (str, optional) – Fill colour as
#RRGGBB(default from style or the map’s basemap colour override).stroke (str, optional) – Outline colour (default from style).
stroke_width (float, optional) – Outline width (default from style).
z_index (int, optional) – Render order (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(BasemapLayer(fill="#f2efe9", stroke="#999999"))
- __init__(gdf=None, *, fill=<unset>, stroke=<unset>, stroke_width=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'basemap'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = False¶
Whether a non-empty
gdfis required at construction.
- class mappyng.GraticuleLayer(gdf=None, *, step=<unset>, stroke=<unset>, opacity=<unset>, stroke_width=<unset>, dash=<unset>, position=<unset>, z=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerLatitude/longitude grid.
- Parameters:
step (float, optional) – Spacing between lines in degrees (default 10).
stroke (str, optional) – Line colour as
#RRGGBB(default"#aaaaaa").opacity (float, optional) – Stroke opacity in
[0, 1](default 0.5).stroke_width (float, optional) – Line width (default 0.5).
dash (str, optional) – SVG dash pattern (default
"4 2").position (str, optional) –
"background"(default) draws the grid above the ocean but below the data;"top"draws it over everything, which suits globe-style maps.z (int, optional) – Explicit stacking order, overriding
position.z_index (int, optional) – Render order among layers (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(GraticuleLayer(step=5.0, stroke="#888888", opacity=0.4))
- __init__(gdf=None, *, step=<unset>, stroke=<unset>, opacity=<unset>, stroke_width=<unset>, dash=<unset>, position=<unset>, z=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'graticule'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = False¶
Whether a non-empty
gdfis required at construction.
- class mappyng.TileLayer(gdf=None, *, source=<unset>, zoom=<unset>, zoom_offset=<unset>, opacity=<unset>, z_order=<unset>, timeout=<unset>, cache=<unset>, cache_dir=<unset>, on_cartouches=<unset>, on_zoom=<unset>, max_tiles=<unset>, warp_scale=<unset>, extra_zoom_levels=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerXYZ raster tile backdrop.
- Parameters:
source (str, optional) – Provider alias (e.g.
"cartodb_positron","osm","satellite") or a URL template with{x},{y},{z}placeholders.zoom (int or str, optional) – Zoom level, or
"auto"(default) to derive it from the bbox.zoom_offset (int, optional) – Added to the auto-calculated zoom (e.g.
-1for lighter tiles).opacity (float, optional) – Tile opacity in
[0, 1](default 1).z_order (int, optional) – SVG stacking order (default below the basemap).
timeout (float, optional) – HTTP request timeout in seconds (default 10).
cache (bool, optional) – Cache tiles on disk (default True).
cache_dir (str, optional) – Cache directory (default
~/.cache/mappyng/tiles).on_cartouches (bool, optional) – Draw on cartouche viewports (default True).
on_zoom (bool, optional) – Draw on the zoom viewport (default True).
max_tiles (int, optional) – Safety cap on tiles downloaded per viewport (default 64).
warp_scale (float, optional) – Output resolution relative to the content area (default 1.5).
extra_zoom_levels (int, optional) – Number of coarser zoom levels to also fetch and blend (default 0).
z_index (int, optional) – Render order among layers (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(TileLayer()) >>> m.add(TileLayer(source="satellite", opacity=0.7))
- __init__(gdf=None, *, source=<unset>, zoom=<unset>, zoom_offset=<unset>, opacity=<unset>, z_order=<unset>, timeout=<unset>, cache=<unset>, cache_dir=<unset>, on_cartouches=<unset>, on_zoom=<unset>, max_tiles=<unset>, warp_scale=<unset>, extra_zoom_levels=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'tile'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = False¶
Whether a non-empty
gdfis required at construction.
- class mappyng.RasterLayer(gdf=None, *, paths=<unset>, opacity=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
Bases:
LayerRaster image overlay.
- Parameters:
paths (dict) – Mapping of region name to GeoTIFF path (required). Each viewport picks the raster whose footprint overlaps its bbox.
opacity (float, optional) – Raster opacity in
[0, 1](default 1).on_zoom (bool, optional) – Draw on the zoom viewport (default True).
on_cartouches (bool, optional) – Draw on cartouche viewports (default True).
z_index (int, optional) – Render order among layers (default 0).
visible (bool, optional) – Whether the layer is drawn (default True).
Examples
>>> m.add(RasterLayer(paths={"metropole": "data/dem.tif"}, opacity=0.8))
- __init__(gdf=None, *, paths=<unset>, opacity=<unset>, on_zoom=<unset>, on_cartouches=<unset>, z_index=0, visible=True)[source]¶
- kind: str = 'raster'¶
Identifier prefix for this layer family.
- render(ctx)[source]¶
Draw the layer into the map carried by
ctx.Called when the map is built. Implementations must set
self._rendered = Trueonce drawn.- Return type:
None
- requires_gdf: bool = False¶
Whether a non-empty
gdfis required at construction.