How maplibre-contour works
mainThe library uses MapLibre's addProtocol to intercept vector tile requests and generate them on-the-fly from raster DEM data.
The Pipeline:
- Fetching:
DemManagerfetches the raster-dem tile and its neighbors to ensure continuity across boundaries. Ifworker: trueis set, this happens in a web worker viaRemoteDemManager. - Decoding:
decode-image.tsconverts RGB values from the raster tile into meters above sea level. - Stitching:
HeightTilecreates a "virtual tile" by stitching raw DEM tiles, aligning measurements to the grid, and smoothing values. - Isoline Generation:
isoline.tsuses a marching-squares implementation (derived fromd3-contour) to generate contour lines from theHeightTile. - Encoding:
vtpbf.tsencodes the resulting isolines into Mapbox Vector Tile (MVT) bytes.
This approach allows for infinite variations of contour lines (units, thresholds, smoothing) without needing to host massive pre-generated vector tile datasets.