Skip to content

Commit a3af097

Browse files
authored
Add xarray support for add_raster (opengeos#678)
1 parent 361438a commit a3af097

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

leafmap/foliumap.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ def add_raster(
514514
vmax: Optional[float] = None,
515515
nodata: Optional[float] = None,
516516
attribution: Optional[str] = None,
517-
layer_name: Optional[str] = "Local COG",
517+
layer_name: Optional[str] = "Raster",
518+
array_args: Optional[Dict] = {},
518519
**kwargs,
519520
):
520521
"""Add a local raster dataset to the map.
@@ -533,9 +534,16 @@ def add_raster(
533534
vmax (float, optional): The maximum value to use when colormapping the colormap when plotting a single band. Defaults to None.
534535
nodata (float, optional): The value from the band to use to interpret as not valid data. Defaults to None.
535536
attribution (str, optional): Attribution for the source raster. This defaults to a message about it being a local file.. Defaults to None.
536-
layer_name (str, optional): The layer name to use. Defaults to 'Local COG'.
537+
layer_name (str, optional): The layer name to use. Defaults to 'Raster'.
538+
array_args (dict, optional): Additional arguments to pass to `array_to_image`. Defaults to {}.
537539
"""
538540

541+
import numpy as np
542+
import xarray as xr
543+
544+
if isinstance(source, np.ndarray) or isinstance(source, xr.DataArray):
545+
source = array_to_image(source, **array_args)
546+
539547
tile_layer, tile_client = get_local_tile_layer(
540548
source,
541549
indexes=indexes,

leafmap/leafmap.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -2176,9 +2176,10 @@ def add_raster(
21762176
vmax=None,
21772177
nodata=None,
21782178
attribution=None,
2179-
layer_name="Local COG",
2179+
layer_name="Raster",
21802180
zoom_to_layer=True,
21812181
visible=True,
2182+
array_args={},
21822183
**kwargs,
21832184
):
21842185
"""Add a local raster dataset to the map.
@@ -2197,10 +2198,17 @@ def add_raster(
21972198
vmax (float, optional): The maximum value to use when colormapping the palette when plotting a single band. Defaults to None.
21982199
nodata (float, optional): The value from the band to use to interpret as not valid data. Defaults to None.
21992200
attribution (str, optional): Attribution for the source raster. This defaults to a message about it being a local file.. Defaults to None.
2200-
layer_name (str, optional): The layer name to use. Defaults to 'Local COG'.
2201+
layer_name (str, optional): The layer name to use. Defaults to 'Raster'.
22012202
zoom_to_layer (bool, optional): Whether to zoom to the extent of the layer. Defaults to True.
22022203
visible (bool, optional): Whether the layer is visible. Defaults to True.
2204+
array_args (dict, optional): Additional arguments to pass to `array_to_memory_file` when reading the raster. Defaults to {}.
22032205
"""
2206+
import numpy as np
2207+
import xarray as xr
2208+
2209+
if isinstance(source, np.ndarray) or isinstance(source, xr.DataArray):
2210+
source = array_to_image(source, **array_args)
2211+
22042212
tile_layer, tile_client = get_local_tile_layer(
22052213
source,
22062214
indexes=indexes,

0 commit comments

Comments
 (0)