Skip to content

leaflet module

Map

Bases: AnyWidget

Map widget

Parameters:

Name Type Description Default
anywidget _type_

description

required
Source code in mapwidget/leaflet.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Map(anywidget.AnyWidget):
    """Map widget

    Args:
        anywidget (_type_): _description_
    """

    _cwd = os.path.dirname(os.path.abspath(__file__))
    _esm = pathlib.Path(os.path.join(_cwd, "js", "leaflet.js"))
    _css = pathlib.Path(os.path.join(_cwd, "css", "leaflet.css"))
    center = traitlets.List([40, -100]).tag(sync=True, o=True)
    zoom = traitlets.Int(4).tag(sync=True, o=True)
    bounds = traitlets.List([0, 0, 0, 0]).tag(sync=True, o=True)
    width = traitlets.Unicode("100%").tag(sync=True, o=True)
    height = traitlets.Unicode("600px").tag(sync=True, o=True)
    clicked_latlng = traitlets.List([None, None]).tag(sync=True, o=True)

    def add_basemap(self, name, opacity=1.0, **kwargs):
        from .basemaps import get_xyz_dict

        xyz_tiles = get_xyz_dict()

        if name in xyz_tiles:
            url = xyz_tiles[name]["url"]
            attribution = xyz_tiles[name]["attribution"]
            max_zoom = xyz_tiles[name]["max_zoom"]
            self.send(
                {
                    "type": "add_basemap",
                    "url": url,
                    "attribution": attribution,
                    "maxZoom": max_zoom,
                    "opacity": opacity,
                    "name": name,
                }
            )

        else:
            raise ValueError(
                f"Basemap {name} not found. It must be one of the following: {list(xyz_tiles.keys())}"
            )

    def add_layer(
        self, url, name="Layer", attribution="", max_zoom=24, opacity=1.0, **kwargs
    ):
        self.send(
            {
                "type": "add_layer",
                "url": url,
                "attribution": attribution,
                "maxZoom": max_zoom,
                "name": name,
                "opacity": opacity,
            }
        )