Combine multiple elevation sources into a single layer.
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. It also makes it possible to have more than one elevation source on a map, which only supports a single elevation layer.
import { MapControls } from "three/examples/jsm/controls/MapControls.js";
import ColorMap from "@giro3d/giro3d/core/ColorMap.js";
import Coordinates from "@giro3d/giro3d/core/geographic/Coordinates.js";
import CoordinateSystem from "@giro3d/giro3d/core/geographic/CoordinateSystem.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 AggregateImageSource from "@giro3d/giro3d/sources/AggregateImageSource.js";
import GeoTIFFSource from "@giro3d/giro3d/sources/GeoTIFFSource.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];
}
function makeColorRamp(
preset,
discrete = false,
invert = false,
mirror = false,
) {
let nshades = discrete ? 10 : 256;
const values = colormap({ colormap: preset, nshades });
const colors = values.map((v) => new Color(v));
if (invert) {
colors.reverse();
}
if (mirror) {
const mirrored = [...colors, ...colors.reverse()];
return mirrored;
}
return colors;
}
const CRS = CoordinateSystem.epsg3857;
const instance = new Instance({
target: "view",
crs: CRS,
});
async function loadData() {
// Let's load a single low-resolution SRTM tile
const srtm = new GeoTIFFSource({
url: "https://3d.oslandia.com/giro3d/rasters/N46W123.cog.tif",
crs: CRS,
});
// An a high-resolution DEM of Mount St Helens
const highRes = new GeoTIFFSource({
url: "https://3d.oslandia.com/giro3d/rasters/msh2009dem-3857.tif",
enableWorkers: false,
crs: CRS,
});
// Let's initialize the SRTM dataset so that we can access its extent
await srtm.initialize();
await highRes.initialize();
const map = new Map({
extent: srtm.getExtent(),
backgroundColor: "gray",
lighting: true,
});
instance.add(map);
// Let's combine those two DEMs into a single source.
// Note that the order in which the sub-sources appear in the array
// dictates their z-index in the stack: make sure that the higher-resolution
// sources appear after the lower resolution sources.
// Important: All sources must share the same CRS.
const aggregateSource = new AggregateImageSource({
sources: [srtm, highRes],
});
const min = 0;
const max = 2500;
const layer = new ElevationLayer({
minmax: { min, max },
colorMap: new ColorMap({ colors: makeColorRamp("viridis"), min, max }),
source: aggregateSource,
});
await map.addLayer(layer);
const center = new Coordinates(instance.coordinateSystem, -13601907, 5812324);
instance.view.camera.position.set(center.x, center.y, 30_000);
const controls = new MapControls(instance.view.camera, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.target.set(center.x, center.y + 1, 0);
instance.view.setControls(controls);
// Attach the inspector
Inspector.attach("inspector", instance);
bindToggle("show-top-source", (show) => {
aggregateSource.setSourceVisibility(highRes, show);
});
bindToggle("show-bottom-source", (show) => {
aggregateSource.setSourceVisibility(srtm, show);
});
}
loadData().catch(console.error);
<!doctype html>
<html lang="en">
<head>
<title>Multi-resolution elevation data</title>
<meta charset="UTF-8" />
<meta name="name" content="multi-resolution-elevation" />
<meta
name="description"
content="Combine multiple elevation sources into a single 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">Parameters</div>
<div class="card-body">
<fieldset id="options">
<!-- Show top source -->
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
checked="true"
role="switch"
id="show-top-source"
autocomplete="off"
/>
<label class="form-check-label" for="show-top-source"
>Show high-resolution source</label
>
</div>
<!-- Show bottom source -->
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
checked="true"
role="switch"
id="show-bottom-source"
autocomplete="off"
/>
<label class="form-check-label" for="show-bottom-source"
>Show low-resolution source</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>
{
"name": "multi-resolution-elevation",
"dependencies": {
"@giro3d/giro3d": "1.0.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}
import { defineConfig } from "vite";
export default defineConfig({
build: {
target: 'esnext',
},
optimizeDeps: {
esbuildOptions: {
target: 'esnext',
},
},
})