Skip to content

deckgl module

Map

Bases: Map

The Map class inherits lonboard.Map.

Returns:

Name Type Description
object

lonboard.Map object.

Source code in leafmap/deckgl.py
 78
 79
 80
 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
125
126
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
240
241
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
280
281
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
315
316
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
class Map(lonboard.Map):
    """The Map class inherits lonboard.Map.

    Returns:
        object: lonboard.Map object.
    """

    def __init__(
        self,
        center: Tuple[float, float] = (20, 0),
        zoom: float = 1.2,
        height: int = 600,
        layers: List = [],
        show_tooltip: bool = True,
        view_state: Optional[Dict] = {},
        **kwargs,
    ) -> None:
        """Initialize a Map object.

        Args:
            center (tuple, optional): Center of the map in the format of (lat, lon). Defaults to (20, 0).
            zoom (float, optional): The map zoom level. Defaults to 1.2.
            height (int, optional): Height of the map. Defaults to 600.
            layers (list, optional): List of additional layers to add to the map. Defaults to [].
            show_tooltip (bool, optional): Flag to show tooltips on the map. Defaults to True.
            view_state (dict, optional): The view state of the map. Defaults to {}.
            **kwargs: Additional keyword arguments to pass to lonboard.Map.

        Returns:
            None
        """

        view_state["latitude"] = center[0]
        view_state["longitude"] = center[1]
        view_state["zoom"] = zoom
        kwargs["view_state"] = view_state

        super().__init__(
            _height=height,
            show_tooltip=show_tooltip,
            layers=layers,
            **kwargs,
        )

    def add_gdf(
        self,
        gdf: gpd.GeoDataFrame,
        zoom_to_layer: bool = True,
        pickable: bool = True,
        color_column: Optional[str] = None,
        color_scheme: Optional[str] = "Quantiles",
        color_map: Optional[Union[str, Dict]] = None,
        color_k: Optional[int] = 5,
        color_args: dict = {},
        alpha: Optional[float] = 1.0,
        rescale: bool = True,
        zoom: Optional[float] = 10.0,
        **kwargs: Any,
    ) -> None:
        """Adds a GeoPandas GeoDataFrame to the map.

        Args:
            gdf (GeoDataFrame): A GeoPandas GeoDataFrame with geometry column.
            zoom_to_layer (bool, optional): Flag to zoom to the added layer. Defaults to True.
            pickable (bool, optional): Flag to enable picking on the added layer. Defaults to True.
            color_column (Optional[str], optional): The column to be used for color encoding. Defaults to None.
            color_map (Optional[Union[str, Dict]], optional): The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.
            color_scheme (Optional[str], optional): The color scheme to use for color encoding. Defaults to "Quantiles".
                Name of a choropleth classification scheme (requires mapclassify).
                A mapclassify.MapClassifier object will be used
                under the hood. Supported are all schemes provided by mapclassify (e.g.
                'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled',
                'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced',
                'JenksCaspallSampled', 'MaxP', 'MaximumBreaks',
                'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean',
                'UserDefined'). Arguments can be passed in classification_kwds.
            color_k (Optional[int], optional): The number of classes to use for color encoding. Defaults to 5.
            color_args (dict, optional): Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.
            zoom (Optional[float], optional): The zoom level to zoom to. Defaults to 10.0.
            **kwargs: Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

        Returns:
            None
        """

        from lonboard import ScatterplotLayer, PathLayer, SolidPolygonLayer
        import matplotlib.pyplot as plt

        geom_type = gdf.geometry.iloc[0].geom_type
        kwargs["pickable"] = pickable

        if geom_type in ["Point", "MultiPoint"]:
            if "get_radius" not in kwargs:
                kwargs["get_radius"] = 10
            if color_column is not None:
                if isinstance(color_map, str):
                    kwargs["get_fill_color"] = apply_continuous_cmap(
                        gdf[color_column], color_map, alpha, rescale
                    )
                elif isinstance(color_map, dict):
                    kwargs["get_fill_color"] = apply_categorical_cmap(
                        gdf[color_column], color_map, alpha
                    )

            if "get_fill_color" not in kwargs:
                kwargs["get_fill_color"] = [255, 0, 0, 180]
            layer = ScatterplotLayer.from_geopandas(gdf, **kwargs)
        elif geom_type in ["LineString", "MultiLineString"]:
            if "get_width" not in kwargs:
                kwargs["get_width"] = 5
            if color_column is not None:
                if isinstance(color_map, str):
                    cmap = plt.get_cmap(color_map)
                    kwargs["get_color"] = apply_continuous_cmap(
                        gdf[color_column], cmap, alpha, rescale
                    )
                elif isinstance(color_map, dict):
                    kwargs["get_color"] = apply_categorical_cmap(
                        gdf[color_column], color_map, alpha
                    )
            layer = PathLayer.from_geopandas(gdf, **kwargs)
        elif geom_type in ["Polygon", "MultiPolygon"]:
            if color_column is not None:
                if isinstance(color_map, str):
                    kwargs["get_fill_color"] = apply_continuous_cmap(
                        gdf[color_column], color_map, alpha, rescale
                    )
                elif isinstance(color_map, dict):
                    kwargs["get_fill_color"] = apply_categorical_cmap(
                        gdf[color_column], color_map, alpha
                    )
            if "get_fill_color" not in kwargs:
                kwargs["get_fill_color"] = [0, 0, 255, 128]
            layer = SolidPolygonLayer.from_geopandas(gdf, **kwargs)

        self.layers = self.layers + (layer,)

        if zoom_to_layer:
            try:
                bounds = gdf.total_bounds.tolist()
                x = (bounds[0] + bounds[2]) / 2
                y = (bounds[1] + bounds[3]) / 2

                src_crs = gdf.crs
                if src_crs is None:
                    src_crs = "EPSG:4326"

                lon, lat = common.convert_coordinates(x, y, src_crs, "EPSG:4326")

                self.view_state = {
                    "latitude": lat,
                    "longitude": lon,
                    "zoom": zoom,
                }
            except Exception as e:
                print(e)

    def add_vector(
        self,
        vector: Union[str, gpd.GeoDataFrame],
        zoom_to_layer: bool = True,
        pickable: bool = True,
        color_column: Optional[str] = None,
        color_scheme: Optional[str] = "Quantiles",
        color_map: Optional[Union[str, Dict]] = None,
        color_k: Optional[int] = 5,
        color_args: dict = {},
        open_args: dict = {},
        **kwargs: Any,
    ) -> None:
        """Adds a vector layer to the map.

        Args:
            vector (Union[str, GeoDataFrame]): The file path or URL to the vector data, or a GeoDataFrame.
            zoom_to_layer (bool, optional): Flag to zoom to the added layer. Defaults to True.
            pickable (bool, optional): Flag to enable picking on the added layer. Defaults to True.
            color_column (Optional[str], optional): The column to be used for color encoding. Defaults to None.
            color_map (Optional[Union[str, Dict]], optional): The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.
            color_scheme (Optional[str], optional): The color scheme to use for color encoding. Defaults to "Quantiles".
                Name of a choropleth classification scheme (requires mapclassify).
                A mapclassify.MapClassifier object will be used
                under the hood. Supported are all schemes provided by mapclassify (e.g.
                'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled',
                'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced',
                'JenksCaspallSampled', 'MaxP', 'MaximumBreaks',
                'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean',
                'UserDefined'). Arguments can be passed in classification_kwds.
            color_k (Optional[int], optional): The number of classes to use for color encoding. Defaults to 5.
            color_args (dict, optional): Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.
            open_args (dict, optional): Additional keyword arguments that will be passed to geopandas.read_file(). Defaults to {}.
            **kwargs: Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

        Returns:
            None
        """

        if isinstance(vector, gpd.GeoDataFrame):
            gdf = vector
        else:
            gdf = gpd.read_file(vector, **open_args)
        self.add_gdf(
            gdf,
            zoom_to_layer,
            pickable,
            color_column,
            color_scheme,
            color_map,
            color_k,
            color_args,
            **kwargs,
        )

    def add_layer(
        self,
        layer: Any,
        zoom_to_layer: bool = True,
        pickable: bool = True,
        **kwargs: Any,
    ) -> None:
        """Adds a layer to the map.

        Args:
            layer (Any): A lonboard layer object.
            zoom_to_layer (bool, optional): Whether to zoom to the layer extent. Defaults to True.
            pickable (bool, optional): Flag to enable picking on the added layer if it's a vector layer. Defaults to True.
            **kwargs: Additional keyword arguments that will be passed to the vector layer if it's a vector layer.

        Returns:
            None
        """

        from lonboard import (
            BitmapLayer,
            BitmapTileLayer,
            HeatmapLayer,
            PathLayer,
            PointCloudLayer,
            PolygonLayer,
            ScatterplotLayer,
            SolidPolygonLayer,
        )

        if type(layer) in [
            BitmapLayer,
            BitmapTileLayer,
            HeatmapLayer,
            ScatterplotLayer,
            PathLayer,
            PointCloudLayer,
            PolygonLayer,
            SolidPolygonLayer,
        ]:
            self.layers = self.layers + (layer,)

            if zoom_to_layer:
                from lonboard._viewport import compute_view

                if hasattr(layer, "table"):
                    try:
                        self.view_state = compute_view([self.layers[-1].table])
                    except Exception as e:
                        print(e)
        else:
            self.add_vector(
                layer, zoom_to_layer=zoom_to_layer, pickable=pickable, **kwargs
            )

    def to_html(self, filename: Optional[str] = None) -> None:
        """Saves the map as an HTML file.

        Args:
            filename (Optional[str], optional): The output file path to the HTML file. Defaults to None.

        Returns:
            str: The HTML content if filename is None.
        """

        if filename is None:
            filename = common.temp_file_path("html")
            super().to_html(filename)
            with open(filename) as f:
                html = f.read()
            return html
        else:
            super().to_html(filename)

    def to_streamlit(
        self,
        width: Optional[int] = None,
        height: Optional[int] = 600,
        scrolling: Optional[bool] = False,
        **kwargs,
    ):
        """Renders `deckgl.Map`in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

        Args:
            width (int, optional): Width of the map. Defaults to None.
            height (int, optional): Height of the map. Defaults to 600.
            scrolling (bool, optional): Whether to allow the map to scroll. Defaults to False.

        Returns:
            streamlit.components: components.html object.
        """

        try:
            import streamlit.components.v1 as components

            return components.html(
                self.to_html(), width=width, height=height, scrolling=scrolling
            )

        except Exception as e:
            raise e

    def add_basemap(self, basemap="HYBRID", visible=True, **kwargs) -> None:
        """Adds a basemap to the map.

        Args:
            basemap (str, optional): Can be one of string from basemaps. Defaults to 'HYBRID'.
            visible (bool, optional): Whether the basemap is visible or not. Defaults to True.
            **kwargs: Keyword arguments for the TileLayer.
        """
        import xyzservices

        try:

            map_dict = {
                "ROADMAP": "Google Maps",
                "SATELLITE": "Google Satellite",
                "TERRAIN": "Google Terrain",
                "HYBRID": "Google Hybrid",
            }

            if isinstance(basemap, str):
                if basemap.upper() in map_dict:
                    tile = common.get_google_map(basemap.upper())

                    layer = lonboard.BitmapTileLayer(
                        data=tile.url,
                        min_zoom=tile.min_zoom,
                        max_zoom=tile.max_zoom,
                        visible=visible,
                        **kwargs,
                    )

                    self.add_layer(layer)
                    return

            if isinstance(basemap, xyzservices.TileProvider):
                url = basemap.build_url()
                if "max_zoom" in basemap.keys():
                    max_zoom = basemap["max_zoom"]
                else:
                    max_zoom = 22
                    layer = lonboard.BitmapTileLayer(
                        data=url,
                        min_zoom=tile.min_zoom,
                        max_zoom=max_zoom,
                        visible=visible,
                        **kwargs,
                    )

                    self.add_layer(layer)
            elif basemap in basemaps and basemaps[basemap].name:
                tile = basemaps[basemap]
                layer = lonboard.BitmapTileLayer(
                    data=tile.url,
                    min_zoom=tile.get("min_zoom", 0),
                    max_zoom=tile.get("max_zoom", 24),
                    visible=visible,
                    **kwargs,
                )
                self.add_layer(layer)
            else:
                print(
                    "Basemap can only be one of the following:\n  {}".format(
                        "\n  ".join(basemaps.keys())
                    )
                )

        except Exception as e:
            raise ValueError(
                "Basemap can only be one of the following:\n  {}".format(
                    "\n  ".join(basemaps.keys())
                )
            )

    def add_tile_layer(
        self,
        url: str,
        opacity: float = 1.0,
        visible: bool = True,
        tile_size: int = 256,
        **kwargs: Any,
    ) -> None:
        """
        Adds a TileLayer to the map.

        This method adds a TileLayer to the map. The TileLayer is created from
            the specified URL, and it is added to the map with the specified
            name, attribution, visibility, and tile size.

        Args:
            url (str): The URL of the tile layer.
            opacity (float, optional): The opacity of the layer. Defaults to 1.0.
            visible (bool, optional): Whether the layer should be visible by
                default. Defaults to True.
            tile_size (int, optional): The size of the tiles in the layer.
                Defaults to 256.
            **kwargs: Additional keyword arguments that are passed to the lonboard.BitmapTileLayer class.
                See https://developmentseed.org/lonboard/latest/api/layers/bitmap-tile-layer/#lonboard.BitmapTileLayer.

        Returns:
            None
        """

        layer = lonboard.BitmapTileLayer(
            data=url,
            tile_size=tile_size,
            opacity=opacity,
            visible=visible,
            **kwargs,
        )
        self.add_layer(layer)

    def add_raster(
        self,
        source: str,
        indexes: Optional[int] = None,
        colormap: Optional[str] = None,
        vmin: Optional[float] = None,
        vmax: Optional[float] = None,
        nodata: Optional[float] = None,
        tile_size: Optional[int] = 256,
        attribution: Optional[str] = None,
        layer_name: Optional[str] = "Raster",
        zoom_to_layer: Optional[bool] = True,
        visible: Optional[bool] = True,
        opacity: Optional[float] = 1.0,
        array_args: Optional[Dict] = {},
        client_args: Optional[Dict] = {"cors_all": True},
        **kwargs,
    ) -> None:
        """Add a local raster dataset to the map.
            If you are using this function in JupyterHub on a remote server (e.g., Binder, Microsoft Planetary Computer) and
            if the raster does not render properly, try installing jupyter-server-proxy using `pip install jupyter-server-proxy`,
            then running the following code before calling this function. For more info, see https://bit.ly/3JbmF93.

            import os
            os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

        Args:
            source (str): The path to the GeoTIFF file or the URL of the Cloud Optimized GeoTIFF.
            indexes (int, optional): The band(s) to use. Band indexing starts at 1. Defaults to None.
            colormap (str, optional): The name of the colormap from `matplotlib` to use when plotting a single band. See https://matplotlib.org/stable/gallery/color/colormap_reference.html. Default is greyscale.
            vmin (float, optional): The minimum value to use when colormapping the palette when plotting a single band. Defaults to None.
            vmax (float, optional): The maximum value to use when colormapping the palette when plotting a single band. Defaults to None.
            nodata (float, optional): The value from the band to use to interpret as not valid data. Defaults to None.
            attribution (str, optional): Attribution for the source raster. This defaults to a message about it being a local file.. Defaults to None.
            layer_name (str, optional): The layer name to use. Defaults to 'Raster'.
            layer_index (int, optional): The index of the layer. Defaults to None.
            zoom_to_layer (bool, optional): Whether to zoom to the extent of the layer. Defaults to True.
            visible (bool, optional): Whether the layer is visible. Defaults to True.
            opacity (float, optional): The opacity of the layer. Defaults to 1.0.
            array_args (dict, optional): Additional arguments to pass to `array_to_memory_file` when reading the raster. Defaults to {}.
            client_args (dict, optional): Additional arguments to pass to localtileserver.TileClient. Defaults to { "cors_all": False }.
        """
        import numpy as np
        import xarray as xr

        if isinstance(source, np.ndarray) or isinstance(source, xr.DataArray):
            source = common.array_to_image(source, **array_args)

        tile_layer, tile_client = common.get_local_tile_layer(
            source,
            indexes=indexes,
            colormap=colormap,
            vmin=vmin,
            vmax=vmax,
            nodata=nodata,
            opacity=opacity,
            attribution=attribution,
            layer_name=layer_name,
            client_args=client_args,
            return_client=True,
        )

        self.add_tile_layer(
            tile_layer.url,
            opacity=opacity,
            visible=visible,
            tile_size=tile_size,
            **kwargs,
        )
        if zoom_to_layer:
            center = tile_client.center()
            zoom = tile_client.default_zoom

            self.view_state = {
                "latitude": center[0],
                "longitude": center[1],
                "zoom": zoom,
            }

__init__(center=(20, 0), zoom=1.2, height=600, layers=[], show_tooltip=True, view_state={}, **kwargs)

Initialize a Map object.

Parameters:

Name Type Description Default
center tuple

Center of the map in the format of (lat, lon). Defaults to (20, 0).

(20, 0)
zoom float

The map zoom level. Defaults to 1.2.

1.2
height int

Height of the map. Defaults to 600.

600
layers list

List of additional layers to add to the map. Defaults to [].

[]
show_tooltip bool

Flag to show tooltips on the map. Defaults to True.

True
view_state dict

The view state of the map. Defaults to {}.

{}
**kwargs

Additional keyword arguments to pass to lonboard.Map.

{}

Returns:

Type Description
None

None

Source code in leafmap/deckgl.py
 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
def __init__(
    self,
    center: Tuple[float, float] = (20, 0),
    zoom: float = 1.2,
    height: int = 600,
    layers: List = [],
    show_tooltip: bool = True,
    view_state: Optional[Dict] = {},
    **kwargs,
) -> None:
    """Initialize a Map object.

    Args:
        center (tuple, optional): Center of the map in the format of (lat, lon). Defaults to (20, 0).
        zoom (float, optional): The map zoom level. Defaults to 1.2.
        height (int, optional): Height of the map. Defaults to 600.
        layers (list, optional): List of additional layers to add to the map. Defaults to [].
        show_tooltip (bool, optional): Flag to show tooltips on the map. Defaults to True.
        view_state (dict, optional): The view state of the map. Defaults to {}.
        **kwargs: Additional keyword arguments to pass to lonboard.Map.

    Returns:
        None
    """

    view_state["latitude"] = center[0]
    view_state["longitude"] = center[1]
    view_state["zoom"] = zoom
    kwargs["view_state"] = view_state

    super().__init__(
        _height=height,
        show_tooltip=show_tooltip,
        layers=layers,
        **kwargs,
    )

add_basemap(basemap='HYBRID', visible=True, **kwargs)

Adds a basemap to the map.

Parameters:

Name Type Description Default
basemap str

Can be one of string from basemaps. Defaults to 'HYBRID'.

'HYBRID'
visible bool

Whether the basemap is visible or not. Defaults to True.

True
**kwargs

Keyword arguments for the TileLayer.

{}
Source code in leafmap/deckgl.py
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
def add_basemap(self, basemap="HYBRID", visible=True, **kwargs) -> None:
    """Adds a basemap to the map.

    Args:
        basemap (str, optional): Can be one of string from basemaps. Defaults to 'HYBRID'.
        visible (bool, optional): Whether the basemap is visible or not. Defaults to True.
        **kwargs: Keyword arguments for the TileLayer.
    """
    import xyzservices

    try:

        map_dict = {
            "ROADMAP": "Google Maps",
            "SATELLITE": "Google Satellite",
            "TERRAIN": "Google Terrain",
            "HYBRID": "Google Hybrid",
        }

        if isinstance(basemap, str):
            if basemap.upper() in map_dict:
                tile = common.get_google_map(basemap.upper())

                layer = lonboard.BitmapTileLayer(
                    data=tile.url,
                    min_zoom=tile.min_zoom,
                    max_zoom=tile.max_zoom,
                    visible=visible,
                    **kwargs,
                )

                self.add_layer(layer)
                return

        if isinstance(basemap, xyzservices.TileProvider):
            url = basemap.build_url()
            if "max_zoom" in basemap.keys():
                max_zoom = basemap["max_zoom"]
            else:
                max_zoom = 22
                layer = lonboard.BitmapTileLayer(
                    data=url,
                    min_zoom=tile.min_zoom,
                    max_zoom=max_zoom,
                    visible=visible,
                    **kwargs,
                )

                self.add_layer(layer)
        elif basemap in basemaps and basemaps[basemap].name:
            tile = basemaps[basemap]
            layer = lonboard.BitmapTileLayer(
                data=tile.url,
                min_zoom=tile.get("min_zoom", 0),
                max_zoom=tile.get("max_zoom", 24),
                visible=visible,
                **kwargs,
            )
            self.add_layer(layer)
        else:
            print(
                "Basemap can only be one of the following:\n  {}".format(
                    "\n  ".join(basemaps.keys())
                )
            )

    except Exception as e:
        raise ValueError(
            "Basemap can only be one of the following:\n  {}".format(
                "\n  ".join(basemaps.keys())
            )
        )

add_gdf(gdf, zoom_to_layer=True, pickable=True, color_column=None, color_scheme='Quantiles', color_map=None, color_k=5, color_args={}, alpha=1.0, rescale=True, zoom=10.0, **kwargs)

Adds a GeoPandas GeoDataFrame to the map.

Parameters:

Name Type Description Default
gdf GeoDataFrame

A GeoPandas GeoDataFrame with geometry column.

required
zoom_to_layer bool

Flag to zoom to the added layer. Defaults to True.

True
pickable bool

Flag to enable picking on the added layer. Defaults to True.

True
color_column Optional[str]

The column to be used for color encoding. Defaults to None.

None
color_map Optional[Union[str, Dict]]

The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.

None
color_scheme Optional[str]

The color scheme to use for color encoding. Defaults to "Quantiles". Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. 'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled', 'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced', 'JenksCaspallSampled', 'MaxP', 'MaximumBreaks', 'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean', 'UserDefined'). Arguments can be passed in classification_kwds.

'Quantiles'
color_k Optional[int]

The number of classes to use for color encoding. Defaults to 5.

5
color_args dict

Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.

{}
zoom Optional[float]

The zoom level to zoom to. Defaults to 10.0.

10.0
**kwargs Any

Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

{}

Returns:

Type Description
None

None

Source code in leafmap/deckgl.py
122
123
124
125
126
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
def add_gdf(
    self,
    gdf: gpd.GeoDataFrame,
    zoom_to_layer: bool = True,
    pickable: bool = True,
    color_column: Optional[str] = None,
    color_scheme: Optional[str] = "Quantiles",
    color_map: Optional[Union[str, Dict]] = None,
    color_k: Optional[int] = 5,
    color_args: dict = {},
    alpha: Optional[float] = 1.0,
    rescale: bool = True,
    zoom: Optional[float] = 10.0,
    **kwargs: Any,
) -> None:
    """Adds a GeoPandas GeoDataFrame to the map.

    Args:
        gdf (GeoDataFrame): A GeoPandas GeoDataFrame with geometry column.
        zoom_to_layer (bool, optional): Flag to zoom to the added layer. Defaults to True.
        pickable (bool, optional): Flag to enable picking on the added layer. Defaults to True.
        color_column (Optional[str], optional): The column to be used for color encoding. Defaults to None.
        color_map (Optional[Union[str, Dict]], optional): The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.
        color_scheme (Optional[str], optional): The color scheme to use for color encoding. Defaults to "Quantiles".
            Name of a choropleth classification scheme (requires mapclassify).
            A mapclassify.MapClassifier object will be used
            under the hood. Supported are all schemes provided by mapclassify (e.g.
            'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled',
            'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced',
            'JenksCaspallSampled', 'MaxP', 'MaximumBreaks',
            'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean',
            'UserDefined'). Arguments can be passed in classification_kwds.
        color_k (Optional[int], optional): The number of classes to use for color encoding. Defaults to 5.
        color_args (dict, optional): Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.
        zoom (Optional[float], optional): The zoom level to zoom to. Defaults to 10.0.
        **kwargs: Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

    Returns:
        None
    """

    from lonboard import ScatterplotLayer, PathLayer, SolidPolygonLayer
    import matplotlib.pyplot as plt

    geom_type = gdf.geometry.iloc[0].geom_type
    kwargs["pickable"] = pickable

    if geom_type in ["Point", "MultiPoint"]:
        if "get_radius" not in kwargs:
            kwargs["get_radius"] = 10
        if color_column is not None:
            if isinstance(color_map, str):
                kwargs["get_fill_color"] = apply_continuous_cmap(
                    gdf[color_column], color_map, alpha, rescale
                )
            elif isinstance(color_map, dict):
                kwargs["get_fill_color"] = apply_categorical_cmap(
                    gdf[color_column], color_map, alpha
                )

        if "get_fill_color" not in kwargs:
            kwargs["get_fill_color"] = [255, 0, 0, 180]
        layer = ScatterplotLayer.from_geopandas(gdf, **kwargs)
    elif geom_type in ["LineString", "MultiLineString"]:
        if "get_width" not in kwargs:
            kwargs["get_width"] = 5
        if color_column is not None:
            if isinstance(color_map, str):
                cmap = plt.get_cmap(color_map)
                kwargs["get_color"] = apply_continuous_cmap(
                    gdf[color_column], cmap, alpha, rescale
                )
            elif isinstance(color_map, dict):
                kwargs["get_color"] = apply_categorical_cmap(
                    gdf[color_column], color_map, alpha
                )
        layer = PathLayer.from_geopandas(gdf, **kwargs)
    elif geom_type in ["Polygon", "MultiPolygon"]:
        if color_column is not None:
            if isinstance(color_map, str):
                kwargs["get_fill_color"] = apply_continuous_cmap(
                    gdf[color_column], color_map, alpha, rescale
                )
            elif isinstance(color_map, dict):
                kwargs["get_fill_color"] = apply_categorical_cmap(
                    gdf[color_column], color_map, alpha
                )
        if "get_fill_color" not in kwargs:
            kwargs["get_fill_color"] = [0, 0, 255, 128]
        layer = SolidPolygonLayer.from_geopandas(gdf, **kwargs)

    self.layers = self.layers + (layer,)

    if zoom_to_layer:
        try:
            bounds = gdf.total_bounds.tolist()
            x = (bounds[0] + bounds[2]) / 2
            y = (bounds[1] + bounds[3]) / 2

            src_crs = gdf.crs
            if src_crs is None:
                src_crs = "EPSG:4326"

            lon, lat = common.convert_coordinates(x, y, src_crs, "EPSG:4326")

            self.view_state = {
                "latitude": lat,
                "longitude": lon,
                "zoom": zoom,
            }
        except Exception as e:
            print(e)

add_layer(layer, zoom_to_layer=True, pickable=True, **kwargs)

Adds a layer to the map.

Parameters:

Name Type Description Default
layer Any

A lonboard layer object.

required
zoom_to_layer bool

Whether to zoom to the layer extent. Defaults to True.

True
pickable bool

Flag to enable picking on the added layer if it's a vector layer. Defaults to True.

True
**kwargs Any

Additional keyword arguments that will be passed to the vector layer if it's a vector layer.

{}

Returns:

Type Description
None

None

Source code in leafmap/deckgl.py
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
315
316
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
def add_layer(
    self,
    layer: Any,
    zoom_to_layer: bool = True,
    pickable: bool = True,
    **kwargs: Any,
) -> None:
    """Adds a layer to the map.

    Args:
        layer (Any): A lonboard layer object.
        zoom_to_layer (bool, optional): Whether to zoom to the layer extent. Defaults to True.
        pickable (bool, optional): Flag to enable picking on the added layer if it's a vector layer. Defaults to True.
        **kwargs: Additional keyword arguments that will be passed to the vector layer if it's a vector layer.

    Returns:
        None
    """

    from lonboard import (
        BitmapLayer,
        BitmapTileLayer,
        HeatmapLayer,
        PathLayer,
        PointCloudLayer,
        PolygonLayer,
        ScatterplotLayer,
        SolidPolygonLayer,
    )

    if type(layer) in [
        BitmapLayer,
        BitmapTileLayer,
        HeatmapLayer,
        ScatterplotLayer,
        PathLayer,
        PointCloudLayer,
        PolygonLayer,
        SolidPolygonLayer,
    ]:
        self.layers = self.layers + (layer,)

        if zoom_to_layer:
            from lonboard._viewport import compute_view

            if hasattr(layer, "table"):
                try:
                    self.view_state = compute_view([self.layers[-1].table])
                except Exception as e:
                    print(e)
    else:
        self.add_vector(
            layer, zoom_to_layer=zoom_to_layer, pickable=pickable, **kwargs
        )

add_raster(source, indexes=None, colormap=None, vmin=None, vmax=None, nodata=None, tile_size=256, attribution=None, layer_name='Raster', zoom_to_layer=True, visible=True, opacity=1.0, array_args={}, client_args={'cors_all': True}, **kwargs)

Add a local raster dataset to the map. If you are using this function in JupyterHub on a remote server (e.g., Binder, Microsoft Planetary Computer) and if the raster does not render properly, try installing jupyter-server-proxy using pip install jupyter-server-proxy, then running the following code before calling this function. For more info, see https://bit.ly/3JbmF93.

1
2
import os
os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

Parameters:

Name Type Description Default
source str

The path to the GeoTIFF file or the URL of the Cloud Optimized GeoTIFF.

required
indexes int

The band(s) to use. Band indexing starts at 1. Defaults to None.

None
colormap str

The name of the colormap from matplotlib to use when plotting a single band. See https://matplotlib.org/stable/gallery/color/colormap_reference.html. Default is greyscale.

None
vmin float

The minimum value to use when colormapping the palette when plotting a single band. Defaults to None.

None
vmax float

The maximum value to use when colormapping the palette when plotting a single band. Defaults to None.

None
nodata float

The value from the band to use to interpret as not valid data. Defaults to None.

None
attribution str

Attribution for the source raster. This defaults to a message about it being a local file.. Defaults to None.

None
layer_name str

The layer name to use. Defaults to 'Raster'.

'Raster'
layer_index int

The index of the layer. Defaults to None.

required
zoom_to_layer bool

Whether to zoom to the extent of the layer. Defaults to True.

True
visible bool

Whether the layer is visible. Defaults to True.

True
opacity float

The opacity of the layer. Defaults to 1.0.

1.0
array_args dict

Additional arguments to pass to array_to_memory_file when reading the raster. Defaults to {}.

{}
client_args dict

Additional arguments to pass to localtileserver.TileClient. Defaults to { "cors_all": False }.

{'cors_all': True}
Source code in leafmap/deckgl.py
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
def add_raster(
    self,
    source: str,
    indexes: Optional[int] = None,
    colormap: Optional[str] = None,
    vmin: Optional[float] = None,
    vmax: Optional[float] = None,
    nodata: Optional[float] = None,
    tile_size: Optional[int] = 256,
    attribution: Optional[str] = None,
    layer_name: Optional[str] = "Raster",
    zoom_to_layer: Optional[bool] = True,
    visible: Optional[bool] = True,
    opacity: Optional[float] = 1.0,
    array_args: Optional[Dict] = {},
    client_args: Optional[Dict] = {"cors_all": True},
    **kwargs,
) -> None:
    """Add a local raster dataset to the map.
        If you are using this function in JupyterHub on a remote server (e.g., Binder, Microsoft Planetary Computer) and
        if the raster does not render properly, try installing jupyter-server-proxy using `pip install jupyter-server-proxy`,
        then running the following code before calling this function. For more info, see https://bit.ly/3JbmF93.

        import os
        os.environ['LOCALTILESERVER_CLIENT_PREFIX'] = 'proxy/{port}'

    Args:
        source (str): The path to the GeoTIFF file or the URL of the Cloud Optimized GeoTIFF.
        indexes (int, optional): The band(s) to use. Band indexing starts at 1. Defaults to None.
        colormap (str, optional): The name of the colormap from `matplotlib` to use when plotting a single band. See https://matplotlib.org/stable/gallery/color/colormap_reference.html. Default is greyscale.
        vmin (float, optional): The minimum value to use when colormapping the palette when plotting a single band. Defaults to None.
        vmax (float, optional): The maximum value to use when colormapping the palette when plotting a single band. Defaults to None.
        nodata (float, optional): The value from the band to use to interpret as not valid data. Defaults to None.
        attribution (str, optional): Attribution for the source raster. This defaults to a message about it being a local file.. Defaults to None.
        layer_name (str, optional): The layer name to use. Defaults to 'Raster'.
        layer_index (int, optional): The index of the layer. Defaults to None.
        zoom_to_layer (bool, optional): Whether to zoom to the extent of the layer. Defaults to True.
        visible (bool, optional): Whether the layer is visible. Defaults to True.
        opacity (float, optional): The opacity of the layer. Defaults to 1.0.
        array_args (dict, optional): Additional arguments to pass to `array_to_memory_file` when reading the raster. Defaults to {}.
        client_args (dict, optional): Additional arguments to pass to localtileserver.TileClient. Defaults to { "cors_all": False }.
    """
    import numpy as np
    import xarray as xr

    if isinstance(source, np.ndarray) or isinstance(source, xr.DataArray):
        source = common.array_to_image(source, **array_args)

    tile_layer, tile_client = common.get_local_tile_layer(
        source,
        indexes=indexes,
        colormap=colormap,
        vmin=vmin,
        vmax=vmax,
        nodata=nodata,
        opacity=opacity,
        attribution=attribution,
        layer_name=layer_name,
        client_args=client_args,
        return_client=True,
    )

    self.add_tile_layer(
        tile_layer.url,
        opacity=opacity,
        visible=visible,
        tile_size=tile_size,
        **kwargs,
    )
    if zoom_to_layer:
        center = tile_client.center()
        zoom = tile_client.default_zoom

        self.view_state = {
            "latitude": center[0],
            "longitude": center[1],
            "zoom": zoom,
        }

add_tile_layer(url, opacity=1.0, visible=True, tile_size=256, **kwargs)

Adds a TileLayer to the map.

This method adds a TileLayer to the map. The TileLayer is created from the specified URL, and it is added to the map with the specified name, attribution, visibility, and tile size.

Parameters:

Name Type Description Default
url str

The URL of the tile layer.

required
opacity float

The opacity of the layer. Defaults to 1.0.

1.0
visible bool

Whether the layer should be visible by default. Defaults to True.

True
tile_size int

The size of the tiles in the layer. Defaults to 256.

256
**kwargs Any

Additional keyword arguments that are passed to the lonboard.BitmapTileLayer class. See https://developmentseed.org/lonboard/latest/api/layers/bitmap-tile-layer/#lonboard.BitmapTileLayer.

{}

Returns:

Type Description
None

None

Source code in leafmap/deckgl.py
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
def add_tile_layer(
    self,
    url: str,
    opacity: float = 1.0,
    visible: bool = True,
    tile_size: int = 256,
    **kwargs: Any,
) -> None:
    """
    Adds a TileLayer to the map.

    This method adds a TileLayer to the map. The TileLayer is created from
        the specified URL, and it is added to the map with the specified
        name, attribution, visibility, and tile size.

    Args:
        url (str): The URL of the tile layer.
        opacity (float, optional): The opacity of the layer. Defaults to 1.0.
        visible (bool, optional): Whether the layer should be visible by
            default. Defaults to True.
        tile_size (int, optional): The size of the tiles in the layer.
            Defaults to 256.
        **kwargs: Additional keyword arguments that are passed to the lonboard.BitmapTileLayer class.
            See https://developmentseed.org/lonboard/latest/api/layers/bitmap-tile-layer/#lonboard.BitmapTileLayer.

    Returns:
        None
    """

    layer = lonboard.BitmapTileLayer(
        data=url,
        tile_size=tile_size,
        opacity=opacity,
        visible=visible,
        **kwargs,
    )
    self.add_layer(layer)

add_vector(vector, zoom_to_layer=True, pickable=True, color_column=None, color_scheme='Quantiles', color_map=None, color_k=5, color_args={}, open_args={}, **kwargs)

Adds a vector layer to the map.

Parameters:

Name Type Description Default
vector Union[str, GeoDataFrame]

The file path or URL to the vector data, or a GeoDataFrame.

required
zoom_to_layer bool

Flag to zoom to the added layer. Defaults to True.

True
pickable bool

Flag to enable picking on the added layer. Defaults to True.

True
color_column Optional[str]

The column to be used for color encoding. Defaults to None.

None
color_map Optional[Union[str, Dict]]

The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.

None
color_scheme Optional[str]

The color scheme to use for color encoding. Defaults to "Quantiles". Name of a choropleth classification scheme (requires mapclassify). A mapclassify.MapClassifier object will be used under the hood. Supported are all schemes provided by mapclassify (e.g. 'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled', 'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced', 'JenksCaspallSampled', 'MaxP', 'MaximumBreaks', 'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean', 'UserDefined'). Arguments can be passed in classification_kwds.

'Quantiles'
color_k Optional[int]

The number of classes to use for color encoding. Defaults to 5.

5
color_args dict

Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.

{}
open_args dict

Additional keyword arguments that will be passed to geopandas.read_file(). Defaults to {}.

{}
**kwargs Any

Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

{}

Returns:

Type Description
None

None

Source code in leafmap/deckgl.py
235
236
237
238
239
240
241
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
280
281
282
283
284
285
286
287
288
def add_vector(
    self,
    vector: Union[str, gpd.GeoDataFrame],
    zoom_to_layer: bool = True,
    pickable: bool = True,
    color_column: Optional[str] = None,
    color_scheme: Optional[str] = "Quantiles",
    color_map: Optional[Union[str, Dict]] = None,
    color_k: Optional[int] = 5,
    color_args: dict = {},
    open_args: dict = {},
    **kwargs: Any,
) -> None:
    """Adds a vector layer to the map.

    Args:
        vector (Union[str, GeoDataFrame]): The file path or URL to the vector data, or a GeoDataFrame.
        zoom_to_layer (bool, optional): Flag to zoom to the added layer. Defaults to True.
        pickable (bool, optional): Flag to enable picking on the added layer. Defaults to True.
        color_column (Optional[str], optional): The column to be used for color encoding. Defaults to None.
        color_map (Optional[Union[str, Dict]], optional): The color map to use for color encoding. It can be a string or a dictionary. Defaults to None.
        color_scheme (Optional[str], optional): The color scheme to use for color encoding. Defaults to "Quantiles".
            Name of a choropleth classification scheme (requires mapclassify).
            A mapclassify.MapClassifier object will be used
            under the hood. Supported are all schemes provided by mapclassify (e.g.
            'BoxPlot', 'EqualInterval', 'FisherJenks', 'FisherJenksSampled',
            'HeadTailBreaks', 'JenksCaspall', 'JenksCaspallForced',
            'JenksCaspallSampled', 'MaxP', 'MaximumBreaks',
            'NaturalBreaks', 'Quantiles', 'Percentiles', 'StdMean',
            'UserDefined'). Arguments can be passed in classification_kwds.
        color_k (Optional[int], optional): The number of classes to use for color encoding. Defaults to 5.
        color_args (dict, optional): Additional keyword arguments that will be passed to assign_continuous_colors(). Defaults to {}.
        open_args (dict, optional): Additional keyword arguments that will be passed to geopandas.read_file(). Defaults to {}.
        **kwargs: Additional keyword arguments that will be passed to lonboard.Layer.from_geopandas()

    Returns:
        None
    """

    if isinstance(vector, gpd.GeoDataFrame):
        gdf = vector
    else:
        gdf = gpd.read_file(vector, **open_args)
    self.add_gdf(
        gdf,
        zoom_to_layer,
        pickable,
        color_column,
        color_scheme,
        color_map,
        color_k,
        color_args,
        **kwargs,
    )

to_html(filename=None)

Saves the map as an HTML file.

Parameters:

Name Type Description Default
filename Optional[str]

The output file path to the HTML file. Defaults to None.

None

Returns:

Name Type Description
str None

The HTML content if filename is None.

Source code in leafmap/deckgl.py
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
def to_html(self, filename: Optional[str] = None) -> None:
    """Saves the map as an HTML file.

    Args:
        filename (Optional[str], optional): The output file path to the HTML file. Defaults to None.

    Returns:
        str: The HTML content if filename is None.
    """

    if filename is None:
        filename = common.temp_file_path("html")
        super().to_html(filename)
        with open(filename) as f:
            html = f.read()
        return html
    else:
        super().to_html(filename)

to_streamlit(width=None, height=600, scrolling=False, **kwargs)

Renders deckgl.Mapin a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

Parameters:

Name Type Description Default
width int

Width of the map. Defaults to None.

None
height int

Height of the map. Defaults to 600.

600
scrolling bool

Whether to allow the map to scroll. Defaults to False.

False

Returns:

Type Description

streamlit.components: components.html object.

Source code in leafmap/deckgl.py
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
def to_streamlit(
    self,
    width: Optional[int] = None,
    height: Optional[int] = 600,
    scrolling: Optional[bool] = False,
    **kwargs,
):
    """Renders `deckgl.Map`in a Streamlit app. This method is a static Streamlit Component, meaning, no information is passed back from Leaflet on browser interaction.

    Args:
        width (int, optional): Width of the map. Defaults to None.
        height (int, optional): Height of the map. Defaults to 600.
        scrolling (bool, optional): Whether to allow the map to scroll. Defaults to False.

    Returns:
        streamlit.components: components.html object.
    """

    try:
        import streamlit.components.v1 as components

        return components.html(
            self.to_html(), width=width, height=height, scrolling=scrolling
        )

    except Exception as e:
        raise e

apply_categorical_cmap(values, cmap, alpha=None, **kwargs)

Apply a categorical colormap to a set of values.

This function applies a specified categorical colormap to the input values.

Parameters:

Name Type Description Default
values array - like

The input values to which the colormap will be applied.

required
cmap str or Colormap

The colormap to apply. Can be a string name of a matplotlib colormap or a Colormap object.

required
alpha float

The alpha transparency to apply to the colormap. Defaults to None.

None
**kwargs

Additional keyword arguments to pass to the colormap function.

{}

Returns:

Name Type Description
array

The colors mapped to the input values.

Source code in leafmap/deckgl.py
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
def apply_categorical_cmap(values, cmap, alpha=None, **kwargs):
    """
    Apply a categorical colormap to a set of values.

    This function applies a specified categorical colormap to the input values.

    Args:
        values (array-like): The input values to which the colormap will be applied.
        cmap (str or Colormap): The colormap to apply. Can be a string name of a matplotlib colormap or a Colormap object.
        alpha (float, optional): The alpha transparency to apply to the colormap. Defaults to None.
        **kwargs: Additional keyword arguments to pass to the colormap function.

    Returns:
        array: The colors mapped to the input values.
    """
    return lonboard.colormap.apply_categorical_cmap(values, cmap, alpha=alpha, **kwargs)

apply_continuous_cmap(values, cmap, alpha=None, rescale=True, **kwargs)

Apply a continuous colormap to a set of values.

This function rescales the input values to the range [0, 1] if rescale is True, and then applies the specified colormap.

Parameters:

Name Type Description Default
values array - like

The input values to which the colormap will be applied.

required
cmap str or Colormap

The colormap to apply. Can be a string name of a matplotlib colormap or a Colormap object.

required
alpha float

The alpha transparency to apply to the colormap. Defaults to None.

None
rescale bool

If True, rescales the input values to the range [0, 1]. Defaults to True.

True
**kwargs

Additional keyword arguments to pass to the colormap function.

{}

Returns:

Name Type Description
array

The colors mapped to the input values.

Source code in leafmap/deckgl.py
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
def apply_continuous_cmap(values, cmap, alpha=None, rescale=True, **kwargs):
    """
    Apply a continuous colormap to a set of values.

    This function rescales the input values to the range [0, 1] if `rescale` is True,
    and then applies the specified colormap.

    Args:
        values (array-like): The input values to which the colormap will be applied.
        cmap (str or Colormap): The colormap to apply. Can be a string name of a matplotlib colormap or a Colormap object.
        alpha (float, optional): The alpha transparency to apply to the colormap. Defaults to None.
        rescale (bool, optional): If True, rescales the input values to the range [0, 1]. Defaults to True.
        **kwargs: Additional keyword arguments to pass to the colormap function.

    Returns:
        array: The colors mapped to the input values.
    """
    import numpy as np
    import matplotlib.pyplot as plt

    if rescale:
        values = np.array(values)
        values = (values - values.min()) / (values.max() - values.min())

    if isinstance(cmap, str):
        cmap = plt.get_cmap(cmap)

    return lonboard.colormap.apply_continuous_cmap(values, cmap, alpha=alpha, **kwargs)