Stack multiple WMTS layers into a single Giro3D layer.

Sources
100% © IGN

The AggregateImageSource can combine multiple ImageSources into one. One the many benefits of this approach is that is reduces the number of layers in a map, which helps improve performance. In this example, we display seveveral WMTS layers in a single Map layer.

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

import Coordinates from "@giro3d/giro3d/core/geographic/Coordinates.js";
import CoordinateSystem from "@giro3d/giro3d/core/geographic/CoordinateSystem.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 Map from "@giro3d/giro3d/entities/Map.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import AggregateImageSource from "@giro3d/giro3d/sources/AggregateImageSource.js";
import WmtsSource from "@giro3d/giro3d/sources/WmtsSource.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];
}

const crs = CoordinateSystem.epsg3857;
const lowerLeft = new Coordinates(crs, -571790, 5144751);
const upperRight = new Coordinates(crs, 961225, 6577787);
const extent = new Extent(
  crs,
  lowerLeft.x,
  upperRight.x,
  lowerLeft.y,
  upperRight.y,
);
const center = extent.centerAsVector3();

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

instance.view.camera.position.set(center.x, center.y - 1, 5_000_000);

const controls = new MapControls(instance.view.camera, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.target.copy(extent.centerAsVector3());
instance.view.setControls(controls);

const map = new Map({ extent, backgroundColor: "gray" });

instance.add(map);

// Define WMTS layers
const wmtsLayers = [
  { layer: "TRANSPORTNETWORKS.ROADS", imageFormat: "image/png", zIndex: 2 },
  { layer: "ADMINEXPRESS-COG.LATEST", imageFormat: "image/png", zIndex: 1 },
  {
    layer: "ORTHOIMAGERY.ORTHOPHOTOS.BDORTHO",
    imageFormat: "image/jpeg",
    zIndex: 0,
  },
];

let sources = [];

let aggregateSource;

async function loadLayers() {
  const capabilities =
    "https://data.geopf.fr/wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetCapabilities";

  const promises = wmtsLayers
    // Sort by z-index so that they appear in the correct order in the stack
    .sort((a, b) => a.zIndex - b.zIndex)
    .map(({ layer, imageFormat }) =>
      // Create a WMTS source from the layer name
      WmtsSource.fromCapabilities(capabilities, { layer, imageFormat }),
    );

  sources = await Promise.all(promises);

  // Let's build the aggregate source that combines all WMTS sources
  aggregateSource = new AggregateImageSource({ sources });

  await map.addLayer(new ColorLayer({ source: aggregateSource }));

  for (let i = 0; i < wmtsLayers.length; i++) {
    const name = wmtsLayers[i].layer;
    // Bind the example GUI to each source
    bindToggle(name, (show) =>
      aggregateSource.setSourceVisibility(sources[i], show),
    );
  }
}

loadLayers();

// Attach the inspector
Inspector.attach("inspector", instance);
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Stacked WMTS layers</title>
    <meta charset="UTF-8" />
    <meta name="name" content="multiple-wmts-layers" />
    <meta
      name="description"
      content="Stack multiple WMTS layers into a single Giro3D layer."
    />
    <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/latest/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">Sources</div>
        <div class="card-body">
          <fieldset id="options">
            <!-- TRANSPORTNETWORKS.ROADS -->
            <div class="form-check form-switch">
              <input
                class="form-check-input"
                type="checkbox"
                checked="true"
                role="switch"
                id="TRANSPORTNETWORKS.ROADS"
                autocomplete="off"
              />
              <label class="form-check-label" for="show-top-source"
                >TRANSPORTNETWORKS.ROADS</label
              >
            </div>

            <!-- ADMINEXPRESS-COG.LATEST -->
            <div class="form-check form-switch">
              <input
                class="form-check-input"
                type="checkbox"
                checked="true"
                role="switch"
                id="ADMINEXPRESS-COG.LATEST"
                autocomplete="off"
              />
              <label class="form-check-label" for="show-top-source"
                >ADMINEXPRESS-COG.LATEST</label
              >
            </div>

            <!-- ORTHOIMAGERY.ORTHOPHOTOS.BDORTHO -->
            <div class="form-check form-switch">
              <input
                class="form-check-input"
                type="checkbox"
                checked="true"
                role="switch"
                id="ORTHOIMAGERY.ORTHOPHOTOS.BDORTHO"
                autocomplete="off"
              />
              <label class="form-check-label" for="show-top-source"
                >ORTHOIMAGERY.ORTHOPHOTOS.BDORTHO</label
              >
            </div>
          </fieldset>
        </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": "multiple-wmts-layers",
    "dependencies": {
        "@giro3d/giro3d": "1.0.0"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}
vite.config.js
import { defineConfig } from "vite";

export default defineConfig({
  build: {
    target: 'esnext',
  },
  optimizeDeps: {
    esbuildOptions: {
      target: 'esnext',
    },
  },
})