49 split control
Creating a split-panel map
This notebook demonstrates how to add a split-panel map with leafmap anf folium. It also supports streamlit. Note that the ipyleaflet SplitControl does not support streamlit.
Uncomment the following line to install leafmap if needed.
In [1]:
Copied!
# !pip install leafmap
# !pip install leafmap
In [2]:
Copied!
import folium
import rioxarray
import xarray as xr
import leafmap.foliumap as leafmap
import folium
import rioxarray
import xarray as xr
import leafmap.foliumap as leafmap
The split-panel map requires two layers: left_layer
and right_layer
. The layer instance can be a string representing a basemap, or an HTTP URL to a Cloud Optimized GeoTIFF (COG), or a folium TileLayer instance.
Using basemaps
In [3]:
Copied!
m = leafmap.Map(height=500)
m.split_map(left_layer="TERRAIN", right_layer="OpenTopoMap")
m
m = leafmap.Map(height=500)
m.split_map(left_layer="TERRAIN", right_layer="OpenTopoMap")
m
Out[3]:
Show available basemaps.
In [4]:
Copied!
# leafmap.basemaps.keys()
# leafmap.basemaps.keys()
Using COG
In [5]:
Copied!
m = leafmap.Map(height=600, center=[39.4948, -108.5492], zoom=12)
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
url2 = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-09-13.tif"
m.split_map(url, url2)
m
m = leafmap.Map(height=600, center=[39.4948, -108.5492], zoom=12)
url = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-07-01.tif"
url2 = "https://github.com/opengeos/data/releases/download/raster/Libya-2023-09-13.tif"
m.split_map(url, url2)
m
Out[5]:
Using folium TileLayer
In [6]:
Copied!
m = leafmap.Map(center=[40, -100], zoom=4)
url1 = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2001_Land_Cover_L48/wms?"
url2 = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?"
left_layer = folium.WmsTileLayer(
url=url1,
layers="NLCD_2001_Land_Cover_L48",
name="NLCD 2001",
attr="MRLC",
fmt="image/png",
transparent=True,
)
right_layer = folium.WmsTileLayer(
url=url2,
layers="NLCD_2019_Land_Cover_L48",
name="NLCD 2019",
attr="MRLC",
fmt="image/png",
transparent=True,
)
m.split_map(left_layer, right_layer)
m
m = leafmap.Map(center=[40, -100], zoom=4)
url1 = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2001_Land_Cover_L48/wms?"
url2 = "https://www.mrlc.gov/geoserver/mrlc_display/NLCD_2019_Land_Cover_L48/wms?"
left_layer = folium.WmsTileLayer(
url=url1,
layers="NLCD_2001_Land_Cover_L48",
name="NLCD 2001",
attr="MRLC",
fmt="image/png",
transparent=True,
)
right_layer = folium.WmsTileLayer(
url=url2,
layers="NLCD_2019_Land_Cover_L48",
name="NLCD 2019",
attr="MRLC",
fmt="image/png",
transparent=True,
)
m.split_map(left_layer, right_layer)
m
Out[6]:
Using xarrays
Download a sample dataset.
In [7]:
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")
Downloading... From: https://open.gishub.org/data/raster/srtm90.tif To: /home/runner/work/leafmap/leafmap/docs/notebooks/srtm90.tif
0%| | 0.00/16.6M [00:00<?, ?B/s]
100%|██████████| 16.6M/16.6M [00:00<00:00, 325MB/s]
Use rioxarray to read the raster as a xarray DataArray and then classify the DEM into 2 elevation classes.
In [8]:
Copied!
dem_ds = rioxarray.open_rasterio(dem)
dem_class = xr.where(dem_ds < 2000, 0, 1)
dem_ds = rioxarray.open_rasterio(dem)
dem_class = xr.where(dem_ds < 2000, 0, 1)
Visualize the DEM and the elevation class image as a split map.
In [9]:
Copied!
m = leafmap.Map(center=[37.6, -119], zoom=9)
m.split_map(
dem_ds,
dem_class,
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_ds,
dem_class,
left_args={"layer_name": "DEM", "colormap": "terrain"},
right_args={"layer_name": "Classified DEM", "colormap": "coolwarm"},
)
m
Out[9]: