Limit the display of a color layer or a map within an elevation range.

Per-map range
Per-layer range (color layer only)
100% © Mapbox

By passing the elevationRange option to the ColorLayer and/or Map constructor, you can limit the visibility of this layer/map within this range. A possible use case is to limit the display of a satellite layer above the sea level, then limit the display of a bathymetry dataset below the sea level.

index.js
import XYZ from 'ol/source/XYZ.js';
import colormap from 'colormap';
import { Color, Vector3 } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';

import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.js';
import TiledImageSource from '@giro3d/giro3d/sources/TiledImageSource.js';
import ElevationLayer from '@giro3d/giro3d/core/layer/ElevationLayer.js';
import Interpretation from '@giro3d/giro3d/core/layer/Interpretation.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import ColorMap from '@giro3d/giro3d/core/layer/ColorMap.js';



const center = { x: -13601505, y: 5812315 };

const extent = Extent.fromCenterAndSize('EPSG:3857', center, 20000, 20000);

const viewerDiv = document.getElementById('viewerDiv');

// Creates a giro3d instance
const instance = new Instance(viewerDiv, {
    crs: extent.crs(),
    renderer: {
        clearColor: false,
    },
});

const map = new Map('map', {
    extent,
    segments: 128,
    elevationRange: { min: 500, max: 3000 },
});

instance.add(map);

function makeColorRamp(preset) {
    const values = colormap({ colormap: preset, nshades: 256 });
    const colors = values.map(v => new Color(v));

    return colors;
}

const colorRamp = makeColorRamp('viridis');

const key = 'pk.eyJ1IjoidG11Z3VldCIsImEiOiJjbGJ4dTNkOW0wYWx4M25ybWZ5YnpicHV6In0.KhDJ7W5N3d1z3ArrsDjX_A';
// Adds a XYZ elevation layer with MapBox terrain RGB tileset
const elevationLayer = new ElevationLayer({
    name: 'xyz_elevation',
    extent,
    source: new TiledImageSource({
        source: new XYZ({
            url: `https://api.mapbox.com/v4/mapbox.terrain-rgb/{z}/{x}/{y}.pngraw?access_token=${key}`,
            projection: extent.crs(),
            crossOrigin: 'anonymous',
        }),
    }),
    colorMap: new ColorMap(colorRamp, 700, 2500),
    interpretation: Interpretation.MapboxTerrainRGB,
});
map.addLayer(elevationLayer);

// Adds a XYZ color layer with MapBox satellite tileset
const colorLayer = new ColorLayer({
    name: 'xyz_color',
    extent,
    source: new TiledImageSource({
        source: new XYZ({
            url: `https://api.mapbox.com/v4/mapbox.satellite/{z}/{x}/{y}.webp?access_token=${key}`,
            projection: extent.crs(),
            crossOrigin: 'anonymous',
        }),
    }),
    elevationRange: { min: 500, max: 3000 },
});
map.addLayer(colorLayer);

// Sets the camera position
instance.camera.camera3D.position.set(-13615016, 5835706, 14797);

// Creates controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);

// Then looks at extent's center
controls.target = new Vector3(-13603869, 5814829, 0);
controls.saveState();

controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.maxPolarAngle = Math.PI / 2.3;

instance.useTHREEControls(controls);

Inspector.attach(document.getElementById('panelDiv'), instance);



function bindSlider(name, fn) {
    const slider = document.getElementById(name);
    slider.oninput = function oninput() {
        fn(slider.value);
        instance.notifyChange(map);
    };
}

function bindToggle(name, action) {
    const toggle = document.getElementById(`toggle-${name}`);
    toggle.oninput = () => {
        const state = toggle.checked;
        action(state);
        instance.notifyChange(map);
    };
}

let colorLayerRange = null;

bindToggle('colorlayer-range', enabled => {
    if (enabled) {
        colorLayer.elevationRange = colorLayerRange;
    } else {
        colorLayer.elevationRange = null;
    }

    document.getElementById('layerMin').disabled = !enabled;
    document.getElementById('layerMax').disabled = !enabled;
});

bindSlider('mapMin', v => { map.materialOptions.elevationRange.min = v; });
bindSlider('mapMax', v => { map.materialOptions.elevationRange.max = v; });
bindSlider('layerMin', v => {
    colorLayer.elevationRange = { min: v, max: colorLayer.elevationRange.max };
    colorLayerRange = colorLayer.elevationRange;
});
bindSlider('layerMax', v => {
    colorLayer.elevationRange = { min: colorLayer.elevationRange.min, max: v };
    colorLayerRange = colorLayer.elevationRange;
});
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - Elevation ranges</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
  <style>
    body {
      padding: 0;
      margin: 0;
      width: 100vw;
      height: 100vh;
    }

    #viewerDiv {
      width: 100%;
      height: 100%;
    }

    #panelDiv {
      position: absolute;
      top: 0;
      left: 0;
    }
    #viewerDiv canvas {
  background-image: linear-gradient(45deg, #aaaaaa 25%, transparent 25%), linear-gradient(-45deg, #aaaaaa 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #aaaaaa 75%), linear-gradient(-45deg, transparent 75%, #aaaaaa 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
}

  </style>
</head>

<body>
  <div id="viewerDiv"></div>
  <div id="panelDiv"></div>
  <div class="m-2 position-absolute top-0 end-0">
    <div class="card m-1">
        <div class="card-header">
            Per-map range
        </div>
        <fieldset class="container m-1" id="options">
            <label for="mapMin" class="form-label">Lower bound</label>
            <div class="input-group">
                <input type="range" min="500" max="3000" value="500" class="slider" id="mapMin">
            </div>

            <div class="my-2"></div>

            <label for="mapMax" class="form-label">Upper bound</label>
            <div class="input-group">
                <input type="range" min="500" max="3000" value="3000" class="slider" id="mapMax">
            </div>
        </fieldset>
    </div>

    <div class="card m-1">
        <div class="card-header">
            Per-layer range (color layer only)
        </div>
        <fieldset class="container m-1" id="options">
            <!-- Toggle elevation range feature -->
            <div class="form-check form-switch">
                <input class="form-check-input" type="checkbox" checked="true" role="switch" id="toggle-colorlayer-range">
                <label class="form-check-label" for="toggle-colorlayer-range">Enable elevation range</label>
            </div>

            <label for="layerMin" class="form-label">Lower bound</label>
            <div class="input-group">
                <input type="range" min="500" max="3000" value="500" class="slider" id="layerMin">
            </div>

            <div class="my-2"></div>

            <label for="layerMax" class="form-label">Upper bound</label>
            <div class="input-group">
                <input type="range" min="500" max="3000" value="3000" class="slider" id="layerMax">
            </div>
        </fieldset>
    </div>
</div>

  <script type="module" src="index.js"></script>
  <script>
    /* activate popovers */
    const popoverTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="popover"]'));
    popoverTriggerList.map(
      // bootstrap is used as script in the template, disable warning about undef
      // eslint-disable-next-line no-undef
      popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl, {
        trigger: 'hover',
        placement: 'left',
        content: document.getElementById(popoverTriggerEl.getAttribute('data-bs-content')).innerHTML,
        html: true,
      }),
    );
  </script>
</body>

</html>
package.json
{
  "name": "elevation_ranges",
  "dependencies": {
    "@giro3d/giro3d": "0.33.1"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}