Display an elevation COG and discard no-data values.

Options

The NoDataOptions of the Layer class can be used to replace no-data values with interpolated neighbouring pixels.

index.js
import colormap from 'colormap';
import { Color } from 'three';
import { MapControls } from 'three/examples/jsm/controls/MapControls.js';

import Extent from '@giro3d/giro3d/core/geographic/Extent.js';
import CogSource from '@giro3d/giro3d/sources/CogSource.js';
import Instance from '@giro3d/giro3d/core/Instance.js';
import ElevationLayer from '@giro3d/giro3d/core/layer/ElevationLayer';
import ColorLayer from '@giro3d/giro3d/core/layer/ColorLayer.js';
import MaskLayer from '@giro3d/giro3d/core/layer/MaskLayer.js';
import Map from '@giro3d/giro3d/entities/Map.js';
import Inspector from '@giro3d/giro3d/gui/Inspector.js';
import ColorMap, { ColorMapMode } from '@giro3d/giro3d/core/layer/ColorMap.js';



// Define projection that we will use (taken from https://epsg.io/26910, Proj4js section)
Instance.registerCRS(
    'EPSG:26910',
    '+proj=utm +zone=10 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs',
);

const extent = new Extent('EPSG:26910', 532622, 569790, 5114416, 5137240);

const center = extent.centerAsVector3();

// `viewerDiv` will contain Giro3D' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');

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

// Instantiate the camera
instance.camera.camera3D.position.set(center.x, center.y - 1, 50000);

// Instantiate the controls
const controls = new MapControls(instance.camera.camera3D, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.target.set(center.x, center.y, center.z);
instance.useTHREEControls(controls);

// Use an elevation COG with nodata values
const source = new CogSource({
    // https://pubs.er.usgs.gov/publication/ds904
    url: 'https://3d.oslandia.com/dem/msh2009dem.tif',
    crs: extent.crs(),
});

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

const min = 227;
const max = 2538;

const colorMap = new ColorMap(colors, min, max, ColorMapMode.Elevation);

const noDataOptions = {
    alpha: 0,
    maxSearchDistance: 10000,
    replaceNoData: true,
};

const map = new Map('map', {
    extent,
    doubleSided: true,
    backgroundOpacity: 0,
    hillshading: true,
    discardNoData: true,
    segments: 128,
});

instance.add(map);

let elevationLayer;
let maskLayer;
let colorLayer;

let activeLayer = 0;

function updateActiveLayer() {
    elevationLayer.visible = false;
    maskLayer.visible = false;
    colorLayer.visible = false;

    switch (activeLayer) {
        case 0:
            elevationLayer.visible = true;
            map.materialOptions.backgroundOpacity = 0;
            map.materialOptions.discardNoData = true;
            break;
        case 1:
            maskLayer.visible = true;
            map.materialOptions.backgroundOpacity = 1;
            map.materialOptions.discardNoData = false;
            break;
        case 2:
        default:
            colorLayer.visible = true;
            map.materialOptions.backgroundOpacity = 0;
            map.materialOptions.discardNoData = false;
            break;
    }
}

function buildLayers() {
    map.removeLayer(elevationLayer);
    map.removeLayer(maskLayer);
    map.removeLayer(colorLayer);

    maskLayer = new MaskLayer({
        name: 'mask',
        extent,
        source,
        noDataOptions,
        preloadImages: false,
    });

    elevationLayer = new ElevationLayer({
        name: 'elevation',
        extent,
        source,
        noDataOptions,
        colorMap,
        preloadImages: false,
        minmax: { min, max },
    });

    colorLayer = new ColorLayer({
        name: 'color',
        extent,
        source,
        noDataOptions,
        colorMap,
        preloadImages: false,
    });

    map.addLayer(elevationLayer);
    map.addLayer(maskLayer);
    map.addLayer(colorLayer);

    updateActiveLayer();

    instance.notifyChange(map);
}

// Attach the inspector
Inspector.attach(document.getElementById('panelDiv'), instance);

const alphaReplacementInput = document.getElementById('alphaReplacement');

alphaReplacementInput.addEventListener('change', e => {
    const value = parseInt(e.target.value, 10);
    noDataOptions.alpha = value;
});

const radiusSlider = document.getElementById('maxDistanceSlider');
radiusSlider.oninput = function oninput() {
    noDataOptions.maxSearchDistance = radiusSlider.valueAsNumber;
};

const enableFillNoDataCheckbox = document.getElementById('enableFillNoData');

enableFillNoDataCheckbox.oninput = function oninput() {
    const state = enableFillNoDataCheckbox.checked;
    noDataOptions.replaceNoData = state;
    if (!state) {
        radiusSlider.setAttribute('disabled', !state);
        alphaReplacementInput.setAttribute('disabled', !state);
    } else {
        radiusSlider.removeAttribute('disabled');
        alphaReplacementInput.removeAttribute('disabled');
    }
};

function bindDropdown(id, action) {
    document.getElementById(id).addEventListener('change', e => {
        const value = parseInt(e.target.value, 10);
        action(value);
        instance.notifyChange(map);
    });
}

bindDropdown('noDataLayerSource', v => {
    activeLayer = v;
});

bindDropdown('alphaReplacement', v => {
    noDataOptions.value = v;
});

// Bind events


buildLayers();

document.getElementById('applyChanges').onclick = function onclick() {
    buildLayers();
};
index.html
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Giro3D - No-data elimination</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">Options</div>

        <fieldset class="container m-1" id="options">
            <div class="mb-3">
                <label for="noDataLayerSource" class="form-label">Layer type</label>
                <select id="noDataLayerSource" class="form-select">
                    <option value="0">Elevation layer</option>
                    <option value="1">Mask layer</option>
                    <option value="2">Color layer</option>
                </select>
            </div>

            <div class="mb-3">
                <div class="form-check form-switch">
                    <input
                        class="form-check-input"
                        type="checkbox"
                        checked="true"
                        role="switch"
                        id="enableFillNoData"
                    />
                    <label class="form-check-label" for="enableFillNoData"
                        >Enable fill no-data</label
                    >
                </div>
            </div>

            <div class="mb-3">
                <label for="alphaReplacement" class="form-label">Replace no-data pixels with</label>
                <select id="alphaReplacement" class="form-select">
                    <option value="1">Opaque alpha</option>
                    <option value="0" selected>Transparent alpha</option>
                </select>
            </div>

            <div class="mb-3">
                <label for="maxDistanceSlider" class="form-label">Max distance (meters)</label>
                <div class="input-group">
                    <input
                        type="range"
                        min="0"
                        max="10000"
                        value="10000"
                        step="100"
                        class="slider"
                        id="maxDistanceSlider"
                    />
                </div>
            </div>

            <div class="mb-3">
                <button type="button" class="btn btn-primary" id="applyChanges">
                    Apply changes
                </button>
            </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": "cog_nodata",
  "dependencies": {
    "@giro3d/giro3d": "0.35.0"
  },
  "devDependencies": {
    "vite": "^3.2.3"
  },
  "scripts": {
    "start": "vite",
    "build": "vite build"
  }
}