geemap/geemap.py
def add_raster(
        self,
        image,
        bands=None,
        layer_name=None,
        colormap=None,
        x_dim="x",
        y_dim="y",
    ):
        """Adds a local raster dataset to the map.

        Args:
            image (str): The image file path.
            bands (int or list, optional): The image bands to use. It can be either a number (e.g., 1) or a list (e.g., [3, 2, 1]). Defaults to None.
            layer_name (str, optional): The layer name to use for the raster. Defaults to None.
            colormap (str, optional): The name of the colormap to use for the raster, such as 'gray' and 'terrain'. More can be found at https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html. Defaults to None.
            x_dim (str, optional): The x dimension. Defaults to 'x'.
            y_dim (str, optional): The y dimension. Defaults to 'y'.
        """
        try:
            import xarray_leaflet

        except Exception:
            # import platform
            # if platform.system() != "Windows":
            #     # install_from_github(
            #     #     url='https://github.com/davidbrochart/xarray_leaflet')
            #     check_install('xarray_leaflet')
            #     import xarray_leaflet
            # else:
            raise ImportError(
                "You need to install xarray_leaflet first. See https://github.com/davidbrochart/xarray_leaflet"
            )

        import warnings

        # import xarray as xr
        import matplotlib.pyplot as plt
        import numpy as np
        import rioxarray

        warnings.simplefilter("ignore")

        if not os.path.exists(image):
            print("The image file does not exist.")
            return

        if colormap is None:
            colormap = plt.cm.inferno

        if layer_name is None:
            layer_name = "Layer_" + random_string()

        if isinstance(colormap, str):
            colormap = plt.cm.get_cmap(name=colormap)

        da = rioxarray.open_rasterio(image, masked=True)

        # print(da.rio.nodata)

        multi_band = False
        if len(da.band) > 1:
            multi_band = True
            if bands is None:
                bands = [3, 2, 1]
        else:
            bands = 1

        if multi_band:
            da = da.rio.write_nodata(0)
        else:
            da = da.rio.write_nodata(np.nan)
        da = da.sel(band=bands)

        # crs = da.rio.crs
        # nan = da.attrs['nodatavals'][0]
        # da = da / da.max()
        # # if multi_band:
        # da = xr.where(da == nan, np.nan, da)
        # da = da.rio.write_nodata(0)
        # da = da.rio.write_crs(crs)

        if multi_band and type(bands) == list:
            layer = da.leaflet.plot(self, x_dim=x_dim, y_dim=y_dim, rgb_dim="band")
        else:
            layer = da.leaflet.plot(self, x_dim=x_dim, y_dim=y_dim, colormap=colormap)

        layer.name = layer_name
Adds a local raster dataset to the map.

        Args:
            image (str): The image file path.
            bands (int or list, optional): The image bands to use. It can be either a number (e.g., 1) or a list (e.g., [3, 2, 1]). Defaults to None.
            layer_name (str, optional): The layer name to use for the raster. Defaults to None.
            colormap (str, optional): The name of the colormap to use for the raster, such as 'gray' and 'terrain'. More can be found at https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html. Defaults to None.
            x_dim (str, optional): The x dimension. Defaults to 'x'.
            y_dim (str, optional): The y dimension. Defaults to 'y'.
        
Adds a local raster dataset to the map.
Adds a local raster dataset to the map .
def add_raster(
        self,
        image,
        bands=None,
        layer_name=None,
        colormap=None,
        x_dim="x",
        y_dim="y",
    ):
        try:
            import xarray_leaflet
        except Exception:
            raise ImportError(
                "You need to install xarray_leaflet first. See https://github.com/davidbrochart/xarray_leaflet"
            )
        import warnings
        import matplotlib.pyplot as plt
        import numpy as np
        import rioxarray
        warnings.simplefilter("ignore")
        if not os.path.exists(image):
            print("The image file does not exist.")
            return
        if colormap is None:
            colormap = plt.cm.inferno
        if layer_name is None:
            layer_name = "Layer_" + random_string()
        if isinstance(colormap, str):
            colormap = plt.cm.get_cmap(name=colormap)
        da = rioxarray.open_rasterio(image, masked=True)
        multi_band = False
        if len(da.band) > 1:
            multi_band = True
            if bands is None:
                bands = [3, 2, 1]
        else:
            bands = 1
        if multi_band:
            da = da.rio.write_nodata(0)
        else:
            da = da.rio.write_nodata(np.nan)
        da = da.sel(band=bands)
        if multi_band and type(bands) == list:
            layer = da.leaflet.plot(self, x_dim=x_dim, y_dim=y_dim, rgb_dim="band")
        else:
            layer = da.leaflet.plot(self, x_dim=x_dim, y_dim=y_dim, colormap=colormap)
        layer.name = layer_name
def add_raster ( self , image , bands = None , layer_name = None , colormap = None , x_dim = "x" , y_dim = "y" , ) : try : import xarray_leaflet except Exception : raise ImportError ( "You need to install xarray_leaflet first. See https://github.com/davidbrochart/xarray_leaflet" ) import warnings import matplotlib . pyplot as plt import numpy as np import rioxarray warnings . simplefilter ( "ignore" ) if not os . path . exists ( image ) : print ( "The image file does not exist." ) return if colormap is None : colormap = plt . cm . inferno if layer_name is None : layer_name = "Layer_" + random_string ( ) if isinstance ( colormap , str ) : colormap = plt . cm . get_cmap ( name = colormap ) da = rioxarray . open_rasterio ( image , masked = True ) multi_band = False if len ( da . band ) > 1 : multi_band = True if bands is None : bands = [ 3 , 2 , 1 ] else : bands = 1 if multi_band : da = da . rio . write_nodata ( 0 ) else : da = da . rio . write_nodata ( np . nan ) da = da . sel ( band = bands ) if multi_band and type ( bands ) == list : layer = da . leaflet . plot ( self , x_dim = x_dim , y_dim = y_dim , rgb_dim = "band" ) else : layer = da . leaflet . plot ( self , x_dim = x_dim , y_dim = y_dim , colormap = colormap ) layer . name = layer_name
Adds a local raster dataset to the map.
Adds a local raster dataset to the map .
"""Adds a local raster dataset to the map.

        Args:
            image (str): The image file path.
            bands (int or list, optional): The image bands to use. It can be either a number (e.g., 1) or a list (e.g., [3, 2, 1]). Defaults to None.
            layer_name (str, optional): The layer name to use for the raster. Defaults to None.
            colormap (str, optional): The name of the colormap to use for the raster, such as 'gray' and 'terrain'. More can be found at https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html. Defaults to None.
            x_dim (str, optional): The x dimension. Defaults to 'x'.
            y_dim (str, optional): The y dimension. Defaults to 'y'.
        """ # import platform
 # if platform.system() != "Windows":
 #     # install_from_github(
 #     #     url='https://github.com/davidbrochart/xarray_leaflet')
 #     check_install('xarray_leaflet')
 #     import xarray_leaflet
 # else:
 # import xarray as xr
 # print(da.rio.nodata)
 # crs = da.rio.crs
 # nan = da.attrs['nodatavals'][0]
 # da = da / da.max()
 # # if multi_band:
 # da = xr.where(da == nan, np.nan, da)
 # da = da.rio.write_nodata(0)
 # da = da.rio.write_crs(crs)

self
image
The image file path.
The image file path .
bands
The image bands to use. It can be either a number  or a list . Defaults to None.
The image bands to use . It can be either a number or a list . Defaults to None .
None
layer_name
The layer name to use for the raster. Defaults to None.
The layer name to use for the raster . Defaults to None .
None
colormap
The name of the colormap to use for the raster, such as 'gray' and 'terrain'.
The name of the colormap to use for the raster such as ' gray ' and ' terrain ' .
None
x_dim
The x dimension. Defaults to 'x'.
The x dimension . Defaults to ' x ' .
'x'
y_dim
The y dimension. Defaults to 'y'.
The y dimension . Defaults to ' y ' .
'y'