MapLibre Terrain Visualization Example¶
This notebook demonstrates how to use the set_terrain
method to add 3D terrain visualization to MapLibre GL JS maps. The terrain visualization uses elevation data from AWS terrain tiles to create a 3D effect.
In [1]:
Copied!
import leafmap.maplibregl as leafmap
import leafmap.maplibregl as leafmap
Basic Terrain Example¶
Let's create a map with terrain visualization enabled using the default terrain source (AWS elevation tiles).
In [2]:
Copied!
# Create a map centered on the Swiss Alps
m = leafmap.Map(
center=[8.2275, 46.8182], # Swiss Alps
zoom=12,
pitch=60, # Tilt the map to show 3D effect
bearing=20, # Rotate the map slightly
height="600px",
)
# Add a satellite basemap for better terrain visualization
m.add_basemap("Esri.WorldImagery")
# Enable terrain with default settings
m.set_terrain()
m
# Create a map centered on the Swiss Alps
m = leafmap.Map(
center=[8.2275, 46.8182], # Swiss Alps
zoom=12,
pitch=60, # Tilt the map to show 3D effect
bearing=20, # Rotate the map slightly
height="600px",
)
# Add a satellite basemap for better terrain visualization
m.add_basemap("Esri.WorldImagery")
# Enable terrain with default settings
m.set_terrain()
m
Terrain with Custom Exaggeration¶
You can adjust the terrain exaggeration to make the 3D effect more or less pronounced.
In [3]:
Copied!
# Create a map of the Grand Canyon with enhanced terrain
m2 = leafmap.Map(
center=[-112.1401, 36.0544], # Grand Canyon
zoom=13,
pitch=70,
bearing=0,
height="600px",
)
# Add satellite imagery
m2.add_basemap("Esri.WorldImagery")
# Enable terrain with increased exaggeration
m2.set_terrain(exaggeration=1.5)
m2
# Create a map of the Grand Canyon with enhanced terrain
m2 = leafmap.Map(
center=[-112.1401, 36.0544], # Grand Canyon
zoom=13,
pitch=70,
bearing=0,
height="600px",
)
# Add satellite imagery
m2.add_basemap("Esri.WorldImagery")
# Enable terrain with increased exaggeration
m2.set_terrain(exaggeration=1.5)
m2
Terrain with Custom Source¶
You can also use a custom terrain source if you have your own elevation data tiles.
In [4]:
Copied!
# Create a map of Mount Fuji with custom terrain source
m3 = leafmap.Map(
center=[138.7274, 35.3606], # Mount Fuji
zoom=11,
pitch=50,
bearing=45,
height="600px",
)
# Add OpenStreetMap basemap
m3.add_basemap("OpenStreetMap.Mapnik")
# Enable terrain with custom source and source ID
m3.set_terrain(
source="https://elevation-tiles-prod.s3.amazonaws.com/terrarium/{z}/{x}/{y}.png",
exaggeration=1.2,
source_id="custom-terrain",
)
m3
# Create a map of Mount Fuji with custom terrain source
m3 = leafmap.Map(
center=[138.7274, 35.3606], # Mount Fuji
zoom=11,
pitch=50,
bearing=45,
height="600px",
)
# Add OpenStreetMap basemap
m3.add_basemap("OpenStreetMap.Mapnik")
# Enable terrain with custom source and source ID
m3.set_terrain(
source="https://elevation-tiles-prod.s3.amazonaws.com/terrarium/{z}/{x}/{y}.png",
exaggeration=1.2,
source_id="custom-terrain",
)
m3
Terrain with Minimal Exaggeration¶
For subtle terrain effects, you can use minimal exaggeration.
In [5]:
Copied!
# Create a map of the Appalachian Mountains with subtle terrain
m4 = leafmap.Map(
center=[-82.5515, 35.5951], # Great Smoky Mountains
zoom=11,
pitch=45,
bearing=0,
height="600px",
)
# Add OpenStreetMap basemap
m4.add_basemap("OpenStreetMap.Mapnik")
# Enable terrain with minimal exaggeration
m4.set_terrain(exaggeration=0.5)
m4
# Create a map of the Appalachian Mountains with subtle terrain
m4 = leafmap.Map(
center=[-82.5515, 35.5951], # Great Smoky Mountains
zoom=11,
pitch=45,
bearing=0,
height="600px",
)
# Add OpenStreetMap basemap
m4.add_basemap("OpenStreetMap.Mapnik")
# Enable terrain with minimal exaggeration
m4.set_terrain(exaggeration=0.5)
m4
Export to HTML¶
You can export a terrain-enabled map to HTML for sharing.
In [6]:
Copied!
# Export the Grand Canyon terrain map to HTML
m2.to_html("terrain_grand_canyon.html", title="Grand Canyon Terrain Visualization")
print("Map exported to terrain_grand_canyon.html")
# Export the Grand Canyon terrain map to HTML
m2.to_html("terrain_grand_canyon.html", title="Grand Canyon Terrain Visualization")
print("Map exported to terrain_grand_canyon.html")
Map exported to terrain_grand_canyon.html