Skip to content

markers

markers

Marker building utilities.

Classes:

  • MarkerGlyph –

    A marker glyph's HTML, split at the end of its caller-supplied style value.

markers.MarkerGlyph

Bases: NamedTuple

A marker glyph's HTML, split at the end of its caller-supplied style value.

:meth:Map.add_points splices a per-point colour between the two halves — a later declaration wins in an inline style attribute — so its JavaScript factory carries the shared markup once instead of once per point.

markers.build_icon_marker

build_icon_marker(
    icon: str,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> DivIcon

Build an icon-based DivIcon marker with optional caption.

Parameters:

  • icon (str) –

    Icon name or full CSS class string. Strings containing a space (e.g. "fa-solid fa-house") are used verbatim. Bare names starting with "fa-" get an "fa-solid" prefix; other bare names (e.g. "home") get a "glyphicon" prefix.

  • css (dict[str, str]) –

    CSS property overrides for the icon element.

  • caption (str | RawHTML | None) –

    Optional caption text below the icon. Plain strings are HTML-escaped and shown literally; wrap in :class:~mapyta.markdown.RawHTML to render inline markup such as <sub>.

  • caption_css (dict[str, str]) –

    CSS property overrides for the caption.

  • caption_id (str | None, default: None ) –

    Optional DOM id on the caption <div>, used by zoom-dependent visibility JS to target the caption independently of its marker.

Returns:

  • DivIcon –
Source code in mapyta/markers.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
def build_icon_marker(
    icon: str,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> folium.DivIcon:
    """Build an icon-based DivIcon marker with optional caption.

    Parameters
    ----------
    icon : str
        Icon name or full CSS class string.  Strings containing a space
        (e.g. ``"fa-solid fa-house"``) are used verbatim.  Bare names
        starting with ``"fa-"`` get an ``"fa-solid"`` prefix; other bare
        names (e.g. ``"home"``) get a ``"glyphicon"`` prefix.
    css : dict[str, str]
        CSS property overrides for the icon element.
    caption : str | RawHTML | None
        Optional caption text below the icon.  Plain strings are HTML-escaped
        and shown literally; wrap in :class:`~mapyta.markdown.RawHTML` to render
        inline markup such as ``<sub>``.
    caption_css : dict[str, str]
        CSS property overrides for the caption.
    caption_id : str | None
        Optional DOM ``id`` on the caption ``<div>``, used by zoom-dependent
        visibility JS to target the caption independently of its marker.

    Returns
    -------
    folium.DivIcon
    """
    return _build_marker(icon_glyph(icon, css), caption, caption_css, caption_id)

markers.build_marker

build_marker(
    marker: str | None,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> DivIcon

Build the DivIcon for marker, classified as icon or text/emoji by :func:marker_glyph.

Parameters:

  • marker (str | None) –

    Icon name, full CSS class string, or emoji/text. None falls back to the default arrow-down icon.

  • css (dict[str, str]) –

    CSS property overrides for the glyph element.

  • caption (str | RawHTML | None) –

    Optional caption text below the glyph. Escaped as on :func:build_icon_marker.

  • caption_css (dict[str, str]) –

    CSS property overrides for the caption.

  • caption_id (str | None, default: None ) –

    Optional DOM id on the caption <div>, used by zoom-dependent visibility JS to target the caption independently of its marker.

Returns:

  • DivIcon –
Source code in mapyta/markers.py
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
def build_marker(
    marker: str | None,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> folium.DivIcon:
    """Build the DivIcon for *marker*, classified as icon or text/emoji by :func:`marker_glyph`.

    Parameters
    ----------
    marker : str | None
        Icon name, full CSS class string, or emoji/text.  ``None`` falls back to the
        default ``arrow-down`` icon.
    css : dict[str, str]
        CSS property overrides for the glyph element.
    caption : str | RawHTML | None
        Optional caption text below the glyph.  Escaped as on :func:`build_icon_marker`.
    caption_css : dict[str, str]
        CSS property overrides for the caption.
    caption_id : str | None
        Optional DOM ``id`` on the caption ``<div>``, used by zoom-dependent
        visibility JS to target the caption independently of its marker.

    Returns
    -------
    folium.DivIcon
    """
    return _build_marker(marker_glyph(marker, css), caption, caption_css, caption_id)

markers.build_text_marker

build_text_marker(
    text: str,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> DivIcon

Build a text/emoji DivIcon marker with optional caption.

Parameters:

  • text (str) –

    The actual text/emoji to render.

  • css (dict[str, str]) –

    CSS property overrides for the text element.

  • caption (str | RawHTML | None) –

    Optional caption text below the text. Escaped as on :func:build_icon_marker.

  • caption_css (dict[str, str]) –

    CSS property overrides for the caption.

  • caption_id (str | None, default: None ) –

    Optional DOM id on the caption <div>, used by zoom-dependent visibility JS to target the caption independently of its marker.

Returns:

  • DivIcon –

    A DivIcon rendering the text and optional caption.

Source code in mapyta/markers.py
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
def build_text_marker(
    text: str,
    css: dict[str, str],
    caption: str | RawHTML | None,
    caption_css: dict[str, str],
    caption_id: str | None = None,
) -> folium.DivIcon:
    """Build a text/emoji DivIcon marker with optional caption.

    Parameters
    ----------
    text : str
        The actual text/emoji to render.
    css : dict[str, str]
        CSS property overrides for the text element.
    caption : str | RawHTML | None
        Optional caption text below the text.  Escaped as on
        :func:`build_icon_marker`.
    caption_css : dict[str, str]
        CSS property overrides for the caption.
    caption_id : str | None
        Optional DOM ``id`` on the caption ``<div>``, used by zoom-dependent
        visibility JS to target the caption independently of its marker.

    Returns
    -------
    folium.DivIcon
        A DivIcon rendering the text and optional caption.
    """
    return _build_marker(text_glyph(text, css), caption, caption_css, caption_id)

markers.caption_open_tag

caption_open_tag(
    css: dict[str, str],
    top_px: int,
    element_id: str | None = None,
    class_name: str | None = None,
) -> str

Open a caption <div> pinned to the horizontal center of its parent.

Anchoring the div's own midpoint at left:50% keeps it centred however wide the text grows; the caller must give the parent position:relative and overflow:visible. element_id targets one caption from the zoom-visibility JS, class_name a whole bulk layer's captions at once; either makes the caption zoom-gated, so it starts hidden rather than flashing before that JS first runs.

Parameters:

  • css (dict[str, str]) –

    CSS property overrides merged onto :data:DEFAULT_CAPTION_CSS.

  • top_px (int) –

    Vertical offset, in pixels, from the parent's top edge to the caption's top edge. Typically the glyph height plus a small gap.

  • element_id (str | None, default: None ) –

    DOM id / class for zoom-dependent visibility. Mutually exclusive.

  • class_name (str | None, default: None ) –

    DOM id / class for zoom-dependent visibility. Mutually exclusive.

Returns:

  • str –

    The opening <div ...> tag; the caller supplies the text and </div>.

Source code in mapyta/markers.py
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
def caption_open_tag(
    css: dict[str, str],
    top_px: int,
    element_id: str | None = None,
    class_name: str | None = None,
) -> str:
    """Open a caption ``<div>`` pinned to the horizontal center of its parent.

    Anchoring the div's own midpoint at ``left:50%`` keeps it centred however wide the
    text grows; the caller must give the parent ``position:relative`` and
    ``overflow:visible``.  ``element_id`` targets one caption from the zoom-visibility
    JS, ``class_name`` a whole bulk layer's captions at once; either makes the caption
    zoom-gated, so it starts hidden rather than flashing before that JS first runs.

    Parameters
    ----------
    css : dict[str, str]
        CSS property overrides merged onto :data:`DEFAULT_CAPTION_CSS`.
    top_px : int
        Vertical offset, in pixels, from the parent's top edge to the caption's top
        edge.  Typically the glyph height plus a small gap.
    element_id, class_name : str | None
        DOM ``id`` / ``class`` for zoom-dependent visibility. Mutually exclusive.

    Returns
    -------
    str
        The opening ``<div ...>`` tag; the caller supplies the text and ``</div>``.
    """
    merged = {
        **DEFAULT_CAPTION_CSS,
        **css,
        "position": "absolute",
        "left": "50%",
        "top": f"{top_px}px",
        "transform": "translateX(-50%)",
    }
    if element_id or class_name:
        merged["display"] = "none"
    attr = f' id="{element_id}"' if element_id else (f' class="{class_name}"' if class_name else "")
    return f'<div{attr} style="{css_to_style(merged)}">'

markers.classify_marker

classify_marker(s: str) -> Literal['emoji', 'icon_class', 'icon_name']

Classify a marker string.

Returns:

  • emoji –

    Non-ASCII content (emojis, unicode symbols) → render as text.

  • icon_class –

    Full CSS class string containing a space (e.g. "fa fa-home") → use as-is.

  • icon_name –

    Bare icon name (e.g. "home", "fa-arrow-right") → auto-prefix.

Source code in mapyta/markers.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
def classify_marker(s: str) -> Literal["emoji", "icon_class", "icon_name"]:
    """Classify a marker string.

    Returns
    -------
    "emoji"
        Non-ASCII content (emojis, unicode symbols) → render as text.
    "icon_class"
        Full CSS class string containing a space (e.g. ``"fa fa-home"``) → use as-is.
    "icon_name"
        Bare icon name (e.g. ``"home"``, ``"fa-arrow-right"``) → auto-prefix.
    """
    if not s or not all(c.isascii() for c in s):
        return "emoji"
    if " " in s:
        return "icon_class"
    return "icon_name"

markers.css_to_style

css_to_style(css: dict[str, str]) -> str

Convert a CSS property dict to an inline style string.

Source code in mapyta/markers.py
42
43
44
def css_to_style(css: dict[str, str]) -> str:
    """Convert a CSS property dict to an inline style string."""
    return ";".join(f"{k}:{v}" for k, v in css.items())

markers.icon_glyph

icon_glyph(icon: str, css: dict[str, str]) -> MarkerGlyph

Build the <i> glyph for an icon marker.

Source code in mapyta/markers.py
105
106
107
108
109
110
111
112
113
114
def icon_glyph(icon: str, css: dict[str, str]) -> MarkerGlyph:
    """Build the ``<i>`` glyph for an icon marker."""
    merged = {**DEFAULT_ICON_CSS, **css}
    fs = px_to_int(merged.get("font-size", "20px"), 20)
    return MarkerGlyph(
        f'<i class="{resolve_icon_class(icon)}" style="{css_to_style(merged)}',
        ';line-height:1;vertical-align:top;"></i>',
        box_size=fs,
        caption_top=fs + 2,
    )

markers.marker_glyph

marker_glyph(marker: str | None, css: dict[str, str]) -> MarkerGlyph

Classify marker and build its glyph, falling back to the arrow-down icon.

Source code in mapyta/markers.py
129
130
131
132
133
def marker_glyph(marker: str | None, css: dict[str, str]) -> MarkerGlyph:
    """Classify *marker* and build its glyph, falling back to the ``arrow-down`` icon."""
    if marker and classify_marker(marker) == "emoji":
        return text_glyph(marker, css)
    return icon_glyph(marker or "arrow-down", css)

markers.marker_wrapper_open

marker_wrapper_open(width: int, height: int) -> str

Open the flex-centred outer wrapper shared by every marker path.

Sized to match the DivIcon's icon_size and uses flex centring so the glyph's visual centre coincides with icon_anchor regardless of the FontAwesome viewBox aspect ratio (e.g. fa-xmark is 0.75em wide). Captions are nested inside this wrapper (not siblings) so click/hover events bubble up to the Leaflet marker even though they render outside icon_size via overflow:visible.

Source code in mapyta/markers.py
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
def marker_wrapper_open(width: int, height: int) -> str:
    """Open the flex-centred outer wrapper shared by every marker path.

    Sized to match the DivIcon's ``icon_size`` and uses flex centring so the
    glyph's visual centre coincides with ``icon_anchor`` regardless of the
    FontAwesome viewBox aspect ratio (e.g. ``fa-xmark`` is 0.75em wide).
    Captions are nested inside this wrapper (not siblings) so click/hover
    events bubble up to the Leaflet marker even though they render outside
    ``icon_size`` via ``overflow:visible``.
    """
    return (
        f'<div style="position:relative;display:flex;align-items:center;'
        f"justify-content:center;width:{width}px;height:{height}px;"
        f'overflow:visible;line-height:1;">'
    )

markers.point_layer_js

point_layer_js(
    marker: str | None,
    marker_css: dict[str, str],
    caption_css: dict[str, str],
    caption_class: str | None,
    tooltip: TooltipStyle,
    popup: PopupStyle,
) -> str

Build the browser-side onEachFeature factory used by :meth:Map.add_points.

The JavaScript counterpart of :func:build_marker: same wrapper, same glyph, same caption nested inside, assembled per point in the browser instead of per point in Python. Both live here so the two renderings of one marker cannot drift apart.

Every constant — the wrapper div, the glyph markup, the caption CSS — is written into the function once, so a feature carries only what genuinely differs between points. Styling through Folium's style_function instead would defeat that: it compiles to a switch with one case per distinct style, putting the output back at O(n) bytes.

Source code in mapyta/markers.py
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def point_layer_js(
    marker: str | None,
    marker_css: dict[str, str],
    caption_css: dict[str, str],
    caption_class: str | None,
    tooltip: TooltipStyle,
    popup: PopupStyle,
) -> str:
    """Build the browser-side ``onEachFeature`` factory used by :meth:`Map.add_points`.

    The JavaScript counterpart of :func:`build_marker`: same wrapper, same glyph, same
    caption nested inside, assembled per point in the browser instead of per point in
    Python.  Both live here so the two renderings of one marker cannot drift apart.

    Every constant — the wrapper div, the glyph markup, the caption CSS — is written into
    the function once, so a feature carries only what genuinely differs between points.
    Styling through Folium's ``style_function`` instead would defeat that: it compiles to a
    ``switch`` with one case per distinct style, putting the output back at O(n) bytes.
    """
    glyph = marker_glyph(marker, marker_css)
    size = glyph.box_size
    caption_open = caption_open_tag(caption_css, glyph.caption_top, class_name=caption_class)
    wrapper_open = marker_wrapper_open(size, size)
    tooltip_open = f'<div style="{tooltip.style}">' if tooltip.style else "<div>"

    return (
        "function(feature, layer) {"
        " var p = feature.properties;"
        " var color = p.color ? ';color:' + p.color : '';"
        f" var caption = p.caption ? {json.dumps(caption_open)} + p.caption + '</div>' : '';"
        f" layer.setIcon(L.divIcon({{html: {json.dumps(wrapper_open)} + {json.dumps(glyph.open_html)} + color"
        f" + {json.dumps(glyph.close_html)} + caption + '</div>', iconSize: [{size}, {size}],"
        f" iconAnchor: [{size // 2}, {size // 2}], className: 'empty'}}));"
        f" if (p.tooltip) {{ layer.bindTooltip({json.dumps(tooltip_open)} + p.tooltip + '</div>',"
        f" {{sticky: {json.dumps(tooltip.sticky)}}}); }}"
        f" if (p.popup) {{ layer.bindPopup(p.popup, {{maxWidth: {popup.max_width}}}); }}"
        "}"
    )

markers.px_to_int

px_to_int(value: str, default: int) -> int

Convert a CSS length string like "12px" or "12.5px" to an int.

Falls back to default for non-px units ("1em", "medium") and malformed values so icon size estimation never raises.

Source code in mapyta/markers.py
47
48
49
50
51
52
53
54
55
56
def px_to_int(value: str, default: int) -> int:
    """Convert a CSS length string like ``"12px"`` or ``"12.5px"`` to an int.

    Falls back to ``default`` for non-``px`` units (``"1em"``, ``"medium"``)
    and malformed values so icon size estimation never raises.
    """
    try:
        return int(float(value.strip().removesuffix("px")))
    except (ValueError, AttributeError):
        return default

markers.resolve_icon_class

resolve_icon_class(icon: str) -> str

Resolve a marker icon name to its full CSS class.

A string containing a space is already a class list and is used as-is; a bare "fa-" name gets the FontAwesome 6 fa-solid prefix, anything else Glyphicon's.

Source code in mapyta/markers.py
78
79
80
81
82
83
84
85
86
87
88
def resolve_icon_class(icon: str) -> str:
    """Resolve a marker icon name to its full CSS class.

    A string containing a space is already a class list and is used as-is; a bare
    ``"fa-"`` name gets the FontAwesome 6 ``fa-solid`` prefix, anything else Glyphicon's.
    """
    if " " in icon:
        return icon
    if icon.startswith("fa-"):
        return f"fa-solid {icon}"
    return f"glyphicon glyphicon-{icon}"

markers.text_glyph

text_glyph(text: str, css: dict[str, str]) -> MarkerGlyph

Build the <div> glyph for a text or emoji marker.

Source code in mapyta/markers.py
117
118
119
120
121
122
123
124
125
126
def text_glyph(text: str, css: dict[str, str]) -> MarkerGlyph:
    """Build the ``<div>`` glyph for a text or emoji marker."""
    merged = {**DEFAULT_TEXT_CSS, **css}
    fs = px_to_int(merged.get("font-size", "16px"), 16)
    return MarkerGlyph(
        f'<div style="{css_to_style(merged)};text-align:center;line-height:1',
        f'">{text}</div>',
        box_size=fs + 10,
        caption_top=fs + 2,
    )