05 load raster
Loading local raster datasets with leafmap
Uncomment the following line to install leafmap if needed.
# !pip install leafmap
To follow this tutorial, you need to install the leafmap and xarray_leaflet Python packages. Use the following conda commands to create a conda env and install packages. Note that xarray_leaflet
does not work properly on Windows (source). Also, the add_raster
function is only available for the ipyleaflet plotting backend. Therefore, Google Colab is not supported. Use the binder link above instead.
conda create -n gee python
conda activate gee
conda install mamba -c conda-forge
mamba install leafmap xarray_leaflet -c conda-forge
Use the ipyleaflet plotting backend. The folium plotting backend does not support the add_raster
function.
import os
import leafmap.leafmap as leafmap
Specify input raster datasets
landsat = 'landsat.tif'
dem = 'dem.tif'
Download samples raster datasets
More datasets can be downloaded from https://viewer.nationalmap.gov/basic/
landsat_url = (
'https://drive.google.com/file/d/1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw/view?usp=sharing'
)
leafmap.download_file(landsat_url, 'landsat.tif', unzip=False)
Downloading... From: https://drive.google.com/uc?id=1EV38RjNxdwEozjc9m0FcO3LFgAoAX1Uw To: /home/runner/work/leafmap/leafmap/docs/notebooks/landsat.tif 100%|██████████| 10.4M/10.4M [00:00<00:00, 188MB/s]
'/home/runner/work/leafmap/leafmap/docs/notebooks/landsat.tif'
dem_url = (
'https://drive.google.com/file/d/1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8/view?usp=sharing'
)
leafmap.download_file(dem_url, 'dem.tif', unzip=False)
Downloading... From: https://drive.google.com/uc?id=1vRkAWQYsLWCi6vcTMk8vLxoXMFbdMFn8 To: /home/runner/work/leafmap/leafmap/docs/notebooks/dem.tif 100%|██████████| 15.2M/15.2M [00:00<00:00, 98.4MB/s]
'/home/runner/work/leafmap/leafmap/docs/notebooks/dem.tif'
Create an interactive map
m = leafmap.Map()
Add local raster datasets to the map
More colormap can be found at https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html
m.add_raster(dem, colormap='terrain', layer_name='DEM')
m.add_raster(landsat, bands=[5, 4, 3], layer_name='Landsat')
Display the map
m
Created: 2023-09-22