Illustrates no-data elimination at the edges of a dataset after reprojection.

Options
100% © U.S. Geological Survey

The shown dataset does not have any no-data pixels. However, reprojecting it to a different CRS leaves empty areas at the edge, which become no-data areas. This example checks that those new no-data areas are correctly handled by the no-data filling algorithm.

index.js
import colormap from "colormap";

import { Box3Helper, Color, DoubleSide } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";

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

function bindToggle(id, onChange) {
  const element = document.getElementById(id);
  if (!(element instanceof HTMLInputElement)) {
    throw new Error(
      "invalid binding element: expected HTMLButtonElement, got: " +
        element.constructor.name,
    );
  }

  element.oninput = function oninput() {
    onChange(element.checked);
  };

  const callback = (v) => {
    element.checked = v;
    onChange(element.checked);
  };

  return [callback, element.checked, element];
}

Instance.registerCRS(
  "EPSG:32742",
  "+proj=utm +zone=42 +south +datum=WGS84 +units=m +no_defs +type=crs",
);

const datasetExtent = new Extent(
  "EPSG:3857",
  -13581040.085,
  -13469591.026,
  5780261.83,
  5942165.048,
);

const extent = datasetExtent.clone().as("EPSG:32742");

const instance = new Instance({
  target: "view",
  crs: extent.crs,
});

instance.view.camera.position.set(1305865, 24791965, 243407);

const controls = new MapControls(instance.view.camera, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.target.set(1305865, 24791964, 1000);
instance.view.setControls(controls);

// Use an elevation COG with nodata values
const source = new GeoTIFFSource({
  // https://pubs.er.usgs.gov/publication/ds904
  url: "https://3d.oslandia.com/cog_data/COG_EPSG3857_USGS_13_n47w122_20220919.tif",
  crs: "EPSG:3857",
});

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

const min = 263;
const max = 4347;

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

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

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

const map = new Map({
  extent,
  side: DoubleSide,
  backgroundOpacity: 0,
  hillshading: true,
  discardNoData: true,
});

instance.add(map);

map.addLayer(elevationLayer);

const box = extent.toBox3(min, min);
const boxHelper = new Box3Helper(box, new Color("yellow"));
instance.add(boxHelper);
boxHelper.updateMatrixWorld();

Inspector.attach("inspector", instance);

bindToggle("enableFillNoData", (state) => {
  map.discardNoData = state;
  instance.notifyChange(map);
});
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>No-data elimination after reprojection</title>
    <meta charset="UTF-8" />
    <meta name="name" content="cog_nodata_reprojection" />
    <meta
      name="description"
      content="Illustrates no-data elimination at the edges of a dataset after reprojection."
    />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />

    <link rel="icon" href="https://giro3d.org/images/favicon.svg" />
    <link
      href="https://giro3d.org/assets/bootstrap-custom.css"
      rel="stylesheet"
    />
    <script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
    <link
      rel="stylesheet"
      type="text/css"
      href="https://giro3d.org/next/examples/css/example.css"
    />
  </head>

  <body>
    <div id="view" class="m-0 p-0 w-100 h-100"></div>
    <div
      id="inspector"
      class="position-absolute top-0 start-0 mh-100 overflow-auto"
    ></div>

    <div class="side-pane-with-status-bar">
      <div class="card">
        <div class="card-header">Options</div>

        <div class="card-body" id="options">
          <div class="form-check form-switch">
            <input
              class="form-check-input"
              type="checkbox"
              checked="true"
              role="switch"
              id="enableFillNoData"
              autocomplete="off"
            />
            <label class="form-check-label" for="enableFillNoData"
              >Hide no-data pixels</label
            >
          </div>
        </div>
      </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_reprojection",
    "dependencies": {
        "colormap": "^2.3.2",
        "@giro3d/giro3d": "git+https://gitlab.com/giro3d/giro3d.git"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}