Skip to content

mouse_position

mouse_position

Mouse-position readout in a non-WGS84 projected CRS (e.g. RD EPSG:28992).

Classes:

mouse_position.MousePositionProjected

MousePositionProjected(
    crs: str,
    position: str = "bottomleft",
    separator: str = " | ",
    empty_string: str = "Unavailable",
)

Bases: JSCSSMixin, MacroElement

Show the cursor coordinates transformed to a projected CRS.

Folium's bundled :class:folium.plugins.MousePosition only displays WGS84 lat/lon — its per-axis lat_formatter/lng_formatter cannot do a 2D projection. This control loads proj4js on the page, registers the target CRS, and writes the transformed X | Y pair into a div on every mousemove.

Parameters:

  • crs (str) –

    Target CRS, e.g. "EPSG:28992" for Dutch RD New. Anything :func:pyproj.CRS.from_user_input accepts.

  • position (str, default: 'bottomleft' ) –

    Leaflet control position ("bottomleft", "topright", ...).

Source code in mapyta/mouse_position.py
 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
def __init__(
    self,
    crs: str,
    position: str = "bottomleft",
    separator: str = " | ",
    empty_string: str = "Unavailable",
) -> None:
    super().__init__()
    self._name = "MousePositionProjected"

    parsed = CRS.from_user_input(crs)
    authority = parsed.to_authority()
    auth_key = f"{authority[0]}:{authority[1]}" if authority else None
    canonical = _PROJ4_DEFS_WITH_TOWGS84.get(auth_key) if auth_key else None
    if canonical is not None:
        self.proj4_def = canonical
    else:
        with warnings.catch_warnings():
            # pyproj's to_proj4() drops +towgs84; for CRSs not in the
            # curated table the readout may be tens to hundreds of m off
            # the official transformation. Acceptable fallback for
            # display-only purposes.
            warnings.simplefilter("ignore", UserWarning)
            self.proj4_def = parsed.to_proj4()

    self.crs = crs
    self.position = position
    self.separator = separator
    self.empty_string = empty_string
    self.num_digits = 6 if parsed.is_geographic else 0