Display a map with TMS TIFF tiles in Float32 format.

100% © U.S. Geological Survey

You can implement your own ImageFormat to load images in a non-standard MIME type (by default, only PNG/JPG/WEBP tiles are loaded by Giro3D).

index.js
import * as turf from "@turf/turf";

import { MapControls } from "three/examples/jsm/controls/MapControls.js";

import XYZ from "ol/source/XYZ.js";

import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import ElevationLayer from "@giro3d/giro3d/core/layer/ElevationLayer.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import GeoTIFFFormat from "@giro3d/giro3d/formats/GeoTIFFFormat.js";
import TiledImageSource from "@giro3d/giro3d/sources/TiledImageSource.js";
import Fetcher from "@giro3d/giro3d/utils/Fetcher.js";

import { DoubleSide } from "three";

const x = -13602618.385789588;
const y = 5811042.273912458;

const extent = new Extent(
  "EPSG:3857",
  x - 12000,
  x + 13000,
  y - 4000,
  y + 26000,
);

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

const map = new Map({
  extent,
  hillshading: true,
  side: DoubleSide,
  discardNoData: true,
  backgroundColor: "white",
});

instance.add(map);

let footprint;

function customIntersectionTest(tileExtent) {
  if (!footprint) {
    return true;
  }

  const corners = [
    [tileExtent.topLeft().x, tileExtent.topLeft().y],
    [tileExtent.topRight().x, tileExtent.topRight().y],
    [tileExtent.bottomRight().x, tileExtent.bottomRight().y],
    [tileExtent.bottomLeft().x, tileExtent.bottomLeft().y],
  ];

  const extentAsPolygon = turf.helpers.polygon([
    [corners[0], corners[1], corners[2], corners[3], corners[0]],
  ]);

  const intersects = turf.booleanIntersects(
    turf.toWgs84(extentAsPolygon),
    footprint,
  );

  return intersects;
}

Fetcher.json("data/MtStHelens-footprint.geojson")
  .then((geojson) => {
    footprint = turf.toWgs84(geojson);

    const source = new TiledImageSource({
      containsFn: customIntersectionTest, // Here we specify our custom intersection test
      source: new XYZ({
        minZoom: 10,
        maxZoom: 16,
        url: "https://3d.oslandia.com/dem/MtStHelens-tiles/{z}/{x}/{y}.tif",
      }),
      format: new GeoTIFFFormat(),
    });

    map
      .addLayer(
        new ElevationLayer({
          name: "osm",
          extent,
          source,
          noDataOptions: {
            replaceNoData: true,
          },
        }),
      )
      .catch((e) => console.error(e));
  })
  .catch((e) => console.error(e));

const center = extent.centerAsVector3();
instance.view.camera.position.set(center.x, center.y - 1, 50000);

const controls = new MapControls(instance.view.camera, instance.domElement);

controls.target.copy(center);

instance.view.setControls(controls);

Inspector.attach("inspector", instance);
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>32-bit TIFF elevation tiles</title>
    <meta charset="UTF-8" />
    <meta name="name" content="tifftiles" />
    <meta
      name="description"
      content="Display a map with TMS TIFF tiles in Float32 format."
    />
    <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>

    <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": "tifftiles",
    "dependencies": {
        "@turf/turf": "^7.1.0",
        "@giro3d/giro3d": "git+https://gitlab.com/giro3d/giro3d.git"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}