Terrain and cloud-native tiles¶

This notebook builds two maps that are difficult to express with traditional notebook mapping libraries: pitched 3D terrain and a map streamed from one PMTiles archive.

Both examples use public demonstration infrastructure and require no account or API key. They do require internet access when displayed because the browser fetches the terrain or vector tiles.

1. A terrain-aware alpine map¶

The style combines OpenStreetMap raster tiles with MapLibre's public elevation demo. The same elevation source drives the terrain surface and the hillshade layer.

In [1]:
from maplibreum import Map, controls, layers, sources

terrain_url = "https://demotiles.maplibre.org/terrain-tiles/tiles.json"
terrain_style = {
    "version": 8,
    "sources": {
        "osm": sources.RasterSource(
            tiles="https://tile.openstreetmap.org/{z}/{x}/{y}.png",
            tile_size=256,
            max_zoom=19,
            attribution="© OpenStreetMap contributors",
        ).to_dict(),
        "terrain": sources.RasterDemSource(
            url=terrain_url, tile_size=256
        ).to_dict(),
        "hillshade": sources.RasterDemSource(
            url=terrain_url, tile_size=256
        ).to_dict(),
    },
    "layers": [
        layers.RasterLayer(id="osm", source="osm").to_dict(),
        layers.HillshadeLayer(
            id="relief",
            source="hillshade",
            paint={"hillshade-exaggeration": 0.35, "hillshade-shadow-color": "#3a2a1d"},
        ).to_dict(),
    ],
    "terrain": {"source": "terrain", "exaggeration": 1.25},
}

alps = Map(
    map_style=terrain_style,
    center=[11.39, 47.28],
    zoom=12,
    pitch=68,
    bearing=-25,
    title="Alpine terrain",
    height="600px",
    map_options={"maxPitch": 85},
)
alps.add_control(
    controls.NavigationControl(visualizePitch=True, showZoom=True, showCompass=True),
    position="top-right",
)
alps.add_control(controls.TerrainControl(source="terrain", exaggeration=1.25))
alps.add_control("fullscreen")

alps
Out[1]:

2. One archive, an entire vector map¶

PMTiles stores a tile pyramid in one range-request-friendly file. MapLibreum registers the custom pmtiles:// protocol before MapLibre starts, so the archive can be referenced exactly like any other vector source.

In [2]:
from maplibreum import PMTilesSource

archive = PMTilesSource(
    archive_url="https://pmtiles.io/protomaps(vector)ODbL_firenze.pmtiles"
)
firenze_style = {
    "version": 8,
    "sources": {
        "firenze": {
            "type": "vector",
            "url": archive.style_url,
            "attribution": "© OpenStreetMap contributors",
        }
    },
    "layers": [
        {"id": "paper", "type": "background", "paint": {"background-color": "#f7f3e8"}},
        {
            "id": "landuse", "type": "fill", "source": "firenze", "source-layer": "landuse",
            "paint": {"fill-color": "#bdd5a5", "fill-opacity": 0.7},
        },
        {
            "id": "roads", "type": "line", "source": "firenze", "source-layer": "roads",
            "paint": {"line-color": "#6b4f3a", "line-width": ["interpolate", ["linear"], ["zoom"], 9, 0.4, 15, 3]},
        },
    ],
}

firenze = Map(
    map_style=firenze_style,
    center=[11.255, 43.7696],
    zoom=13,
    title="Florence from a PMTiles archive",
    height="580px",
)
firenze.add_pmtiles_source(archive)
firenze.add_control("navigation", options={"showZoom": True})
firenze.add_control("fullscreen")

firenze
Out[2]:

Production notes¶

  • Replace demonstration endpoints with infrastructure whose availability and usage policy fit your project.
  • Keep attribution alongside every source.
  • PMTiles is especially attractive for static hosting because the server only needs HTTP range requests—no tile server is required.

Next: add interaction that remains useful after the notebook becomes a standalone HTML page.