Skip to content

markers

markers

Marker building utilities.

markers.build_icon_marker

build_icon_marker(
    icon: str,
    css: dict[str, str],
    caption: str | None,
    caption_css: dict[str, str],
) -> 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 | None) –

    Optional caption text below the icon.

  • caption_css (dict[str, str]) –

    CSS property overrides for the caption.

Returns:

  • DivIcon
Source code in mapyta/markers.py
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def build_icon_marker(
    icon: str,
    css: dict[str, str],
    caption: str | None,
    caption_css: dict[str, str],
) -> 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 | None
        Optional caption text below the icon.
    caption_css : dict[str, str]
        CSS property overrides for the caption.

    Returns
    -------
    folium.DivIcon
    """
    merged = {**DEFAULT_ICON_CSS, **css}
    style_str = css_to_style(merged)
    caption_suffix = caption_html(caption, caption_css) if caption else ""
    # Full CSS class string (contains a space) → use as-is
    # Bare name starting with "fa-" → FontAwesome 6 (fa-solid prefix)
    # Other bare name → Glyphicon
    if " " in icon:
        icon_class = icon
    elif icon.startswith("fa-"):
        icon_class = f"fa-solid {icon}"
    else:
        icon_class = f"glyphicon glyphicon-{icon}"
    icon_html = f'<div style="text-align:center;"><i class="{icon_class}" style="{style_str}"></i></div>{caption_suffix}'
    return folium.DivIcon(
        html=icon_html,
        icon_size=(100, 50),
        icon_anchor=(50, 15),
    )

markers.build_text_marker

build_text_marker(
    text: str,
    css: dict[str, str],
    caption: str | None,
    caption_css: dict[str, str],
) -> 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 | None) –

    Optional caption text below the text.

  • caption_css (dict[str, str]) –

    CSS property overrides for the caption.

Returns:

  • DivIcon

    A DivIcon rendering the text and optional caption.

Source code in mapyta/markers.py
127
128
129
130
131
132
133
134
135
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
def build_text_marker(
    text: str,
    css: dict[str, str],
    caption: str | None,
    caption_css: dict[str, str],
) -> 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 | None
        Optional caption text below the text.
    caption_css : dict[str, str]
        CSS property overrides for the caption.

    Returns
    -------
    folium.DivIcon
        A DivIcon rendering the text and optional caption.
    """
    merged = {**DEFAULT_TEXT_CSS, **css}
    style_str = css_to_style(merged) + ";text-align:center"
    caption_suffix = caption_html(caption, caption_css) if caption else ""
    inner = f'<div style="{style_str}">{text}</div>'
    html = f'<div style="text-align:center;">{inner}{caption_suffix}</div>'
    # size estimation for icon_size/anchor from font-size
    fs = int(merged.get("font-size", "16px").replace("px", ""))
    w = max(fs + 10, 100 if caption else 0)
    h = fs + 10 + (20 if caption else 0)
    return folium.DivIcon(
        html=html,
        icon_size=(w, h),
        icon_anchor=(w // 2, (fs + 10) // 2),
    )

markers.caption_html

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

Build an HTML snippet for a caption below a marker icon.

Parameters:

  • text (str) –

    Caption text.

  • css (dict[str, str]) –

    CSS property dict merged with appropriate defaults by the caller.

Returns:

  • str

    HTML <div> string.

Source code in mapyta/markers.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def caption_html(text: str, css: dict[str, str]) -> str:
    """Build an HTML snippet for a caption below a marker icon.

    Parameters
    ----------
    text : str
        Caption text.
    css : dict[str, str]
        CSS property dict merged with appropriate defaults by the caller.

    Returns
    -------
    str
        HTML ``<div>`` string.
    """
    merged = {**DEFAULT_CAPTION_CSS, **css}
    return f'<div style="{css_to_style(merged)}">{text}</div>'

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
38
39
40
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())