90 pixel inspector
Interactive pixel inspector¶
The interactive pixel inspector can be used to explore the pixel values of an image. It supports Cloud Optimized GeoTIFF (COG), STAC, and other raster data formats, either stored locally or on the cloud. The COG and STAC functionalities are powered by the TiTiler, while the local file support is powered by localtileserver.
In [1]:
Copied!
# %pip install "leafmap[raster]"
# %pip install "leafmap[raster]"
In [2]:
Copied!
import leafmap
import rasterio
import rioxarray
import xarray as xr
import leafmap
import rasterio
import rioxarray
import xarray as xr
COG¶
In [3]:
Copied!
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Libya")
m.add("inspector")
m
m = leafmap.Map()
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
m.add_cog_layer(url, name="Libya")
m.add("inspector")
m
Out[3]:
STAC¶
In [4]:
Copied!
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="SPOT Image")
m.add("inspector")
m
m = leafmap.Map()
url = "https://canada-spot-ortho.s3.amazonaws.com/canada_spot_orthoimages/canada_spot5_orthoimages/S5_2007/S5_11055_6057_20070622/S5_11055_6057_20070622.json"
m.add_stac_layer(url, bands=["B3", "B2", "B1"], name="SPOT Image")
m.add("inspector")
m
Out[4]:
Planetary Computer¶
In [5]:
Copied!
m = leafmap.Map()
collection = "landsat-8-c2-l2"
item = "LC08_L2SP_047027_20201204_02_T1"
m.add_stac_layer(
collection=collection,
item=item,
assets="SR_B7,SR_B5,SR_B4",
name="Landsat Band-754",
)
m.add("inspector")
m
m = leafmap.Map()
collection = "landsat-8-c2-l2"
item = "LC08_L2SP_047027_20201204_02_T1"
m.add_stac_layer(
collection=collection,
item=item,
assets="SR_B7,SR_B5,SR_B4",
name="Landsat Band-754",
)
m.add("inspector")
m
Out[5]:
Local raster¶
In [6]:
Copied!
url = "https://open.gishub.org/data/raster/landsat.tif"
satellite = leafmap.download_file(url, "landsat.tif", overwrite=True)
url = "https://open.gishub.org/data/raster/landsat.tif"
satellite = leafmap.download_file(url, "landsat.tif", overwrite=True)
Downloading... From: https://open.gishub.org/data/raster/landsat.tif To: /home/runner/work/leafmap/leafmap/docs/notebooks/landsat.tif
0%| | 0.00/10.1M [00:00<?, ?B/s]
100%|██████████| 10.1M/10.1M [00:00<00:00, 139MB/s]
In [7]:
Copied!
m = leafmap.Map()
m.add_raster(satellite, indexes=[4, 1, 2], vmin=0, vmax=120, layer_name="Landsat 7")
m.add("inspector")
m
m = leafmap.Map()
m.add_raster(satellite, indexes=[4, 1, 2], vmin=0, vmax=120, layer_name="Landsat 7")
m.add("inspector")
m
Out[7]:
In [8]:
Copied!
dataset = rasterio.open(satellite)
nir = dataset.read(4).astype(float)
red = dataset.read(1).astype(float)
ndvi = (nir - red) / (nir + red)
ndvi_image = leafmap.array_to_image(ndvi, source=satellite)
dataset = rasterio.open(satellite)
nir = dataset.read(4).astype(float)
red = dataset.read(1).astype(float)
ndvi = (nir - red) / (nir + red)
ndvi_image = leafmap.array_to_image(ndvi, source=satellite)
In [9]:
Copied!
m = leafmap.Map()
m.add_raster(satellite, indexes=[4, 1, 2], vmin=0, vmax=120, layer_name="Landsat 7")
m.add_raster(ndvi_image, colormap="Greens", layer_name="NDVI")
m.add("inspector")
m
m = leafmap.Map()
m.add_raster(satellite, indexes=[4, 1, 2], vmin=0, vmax=120, layer_name="Landsat 7")
m.add_raster(ndvi_image, colormap="Greens", layer_name="NDVI")
m.add("inspector")
m
Out[9]:
Xarray DataArray¶
In [10]:
Copied!
url = "https://open.gishub.org/data/raster/srtm90.tif"
dem = leafmap.download_file(url, "srtm90.tif")
url = "https://open.gishub.org/data/raster/srtm90.tif"
dem = leafmap.download_file(url, "srtm90.tif")
srtm90.tif already exists. Skip downloading. Set overwrite=True to overwrite.
In [11]:
Copied!
ds = rioxarray.open_rasterio(dem)
ds
ds = rioxarray.open_rasterio(dem)
ds
Out[11]:
<xarray.DataArray (band: 1, y: 2465, x: 4269)> Size: 21MB [10523085 values with dtype=int16] Coordinates: * band (band) int64 8B 1 * x (x) float64 34kB -120.8 -120.8 -120.8 ... -117.3 -117.3 -117.3 * y (y) float64 20kB 38.63 38.63 38.62 38.62 ... 36.64 36.64 36.63 spatial_ref int64 8B 0 Attributes: OVR_RESAMPLING_ALG: NEAREST AREA_OR_POINT: Area scale_factor: 1.0 add_offset: 0.0 long_name: elevation
In [12]:
Copied!
array = ds.sel(band=1)
masked_array = xr.where(array < 2000, 0, 1)
array = ds.sel(band=1)
masked_array = xr.where(array < 2000, 0, 1)
In [13]:
Copied!
m = leafmap.Map()
m.add_raster(dem, colormap="terrain", layer_name="DEM")
m.add_raster(masked_array, colormap="coolwarm", layer_name="Classified DEM")
m
m = leafmap.Map()
m.add_raster(dem, colormap="terrain", layer_name="DEM")
m.add_raster(masked_array, colormap="coolwarm", layer_name="Classified DEM")
m
Out[13]:
Split map¶
In [14]:
Copied!
m = leafmap.Map(center=[37.6, -119], zoom=9)
m.split_map(
dem,
masked_array,
left_args={
"layer_name": "DEM",
"colormap": "terrain",
},
right_args={
"layer_name": "Classified DEM",
"colormap": "coolwarm",
},
)
m
m = leafmap.Map(center=[37.6, -119], zoom=9)
m.split_map(
dem,
masked_array,
left_args={
"layer_name": "DEM",
"colormap": "terrain",
},
right_args={
"layer_name": "Classified DEM",
"colormap": "coolwarm",
},
)
m
Out[14]: