Display an elevation GeoTIFF with a color map.

Giro3D version
THREE.js version
OpenLayers version
CRS
Memory usage (CPU)
Memory usage (GPU)
Frames
Clear color
Clear alpha
Status
Local clipping enabled
Capabilities
WebGL 2
Max texture units
Max texture size
Precision
Max fragment shader uniforms
Logarithmic depth buffer
Max shader attributes
Check shader errors
EXT_clip_control
EXT_color_buffer_float
EXT_color_buffer_half_float
EXT_conservative_depth
EXT_depth_clamp
EXT_float_blend
EXT_polygon_offset_clamp
EXT_texture_compression_bptc
EXT_texture_compression_rgtc
EXT_texture_filter_anisotropic
EXT_texture_mirror_clamp_to_edge
EXT_texture_norm16
NV_shader_noperspective_interpolation
OES_draw_buffers_indexed
OES_sample_variables
OES_shader_multisample_interpolation
OES_texture_float_linear
OVR_multiview2
WEBGL_clip_cull_distance
WEBGL_compressed_texture_astc
WEBGL_compressed_texture_etc
WEBGL_compressed_texture_etc1
WEBGL_compressed_texture_s3tc
WEBGL_compressed_texture_s3tc_srgb
WEBGL_debug_renderer_info
WEBGL_debug_shaders
WEBGL_lose_context
WEBGL_multi_draw
WEBGL_polygon_mode
WEBGL_stencil_texturing
MSAA
EDL
EDL Radius
EDL Strength
Inpainting
Inpainting steps
Inpainting depth contrib.
Point cloud occlusion
Type
FOV
Automatic plane computation
Far plane
Near plane
Max far plane
Min near plane
Width (pixels)
Height (pixels)
x
y
z
x
y
z
color
Enable cache
Default TTL (seconds)
Capacity (MB)
Capacity (entries)
Entries
Memory usage (approx)
Pending requests
Memory tracker
Identifier
Memory usage (CPU)
Memory usage (GPU)
Status
Render order
Enable
Plane normal X
Plane normal Y
Plane normal Z
Distance
Helper size
Negate plane
Visible
Freeze updates
Opacity
Show volumes
Volume color
Discard no-data values
Sidedness
Front
Depth test
Visible tiles
Reachable tiles
Loaded tiles
Cast shadow
Receive shadow
Tile width (pixels)
Tile height (pixels)
Show grid
Background
Background opacity
Show tiles outlines
Tile outline color
Show tile info
Show extent
Extent color
Subdivision threshold
Deformation
Wireframe
Tile subdivisions
Show collider meshes
CPU terrain
Stitching
Geometry pool
Enabled
Mode
Hillshade
Hillshade intensity
Z factor
Hillshade zenith
Hillshade azimuth
Elevation layers only
Enable
Color
Opacity
X step
Y step
X Offset
Y Offset
Thickness
Enable
Color
Thickness
Opacity
Primary interval (m)
Secondary interval (m)
Brightness
Contrast
Saturation
Layer count
Render state
Normal
Layers
Identifier
Memory usage (CPU)
Memory usage (GPU)
Name
Source CRS
Status
Resolution factor
Visible
Frozen
Interpretation
Loaded images
Minimum elevation
Maximum elevation
Show extent
Extent color
Enabled
Mode
Elevation
Lower bound
Upper bound
Type
Color space
Data type
Flip Y
Synchronous
CRS
Memory usage (CPU)
Memory usage (GPU)
URL
Channel mapping
Show helpers
Show hidden objects
Name filter
Hierarchy
Properties
isObject3D
uuid
name
type
matrixAutoUpdate
matrixWorldAutoUpdate
matrixWorldNeedsUpdate
visible
castShadow
receiveShadow
frustumCulled
renderOrder
x
y
z
x
y
z
Read COG as

A GeoTIFF that contains elevation data in 32-bit floating point values. You can visualize the dataset in the following ways: as an elevation layer with or without a color map, as a color layer compressed to 8-bit using Interpretation.CompressTo8Bit, and as a color layer with a ColorMap.

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 GeoTIFFSource from "@giro3d/giro3d/sources/GeoTIFFSource.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import ElevationLayer from "@giro3d/giro3d/core/layer/ElevationLayer.js";
import Interpretation from "@giro3d/giro3d/core/layer/Interpretation.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import ColorMap, { ColorMapMode } from "@giro3d/giro3d/core/ColorMap.js";

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;
}

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

  element.onchange = () => {
    onChange(element.value);
  };

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

  const setOptions = (options) => {
    const items = options.map(
      (opt) =>
        `<option value=${opt.id} ${opt.selected ? "selected" : ""}>${opt.name}</option>`,
    );
    element.innerHTML = items.join("\n");
  };

  return [callback, element.value, element, setOptions];
}

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

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

instance.view.camera.position.set(-13656319, 5735451, 88934);

const controls = new MapControls(instance.view.camera, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.2;
controls.target.set(-13545408, 5837154, 0);
instance.view.setControls(controls);

const map = new Map({
  extent,
  backgroundColor: "gray",
  lighting: true,
});
instance.add(map);

// Use an elevation COG with nodata values
const source = new GeoTIFFSource({
  // https://www.sciencebase.gov/catalog/item/632a9a9ad34e71c6d67b95a3
  url: "https://3d.oslandia.com/cog_data/COG_EPSG3857_USGS_13_n47w122_20220919.tif",
  crs: extent.crs,
});

const min = 263;
const max = 4347;

// Display it as elevation and color
const viridis = new ColorMap({
  colors: makeColorRamp("viridis"),
  min,
  max,
  mode: ColorMapMode.Elevation,
});
const magma = new ColorMap({
  colors: makeColorRamp("magma"),
  min,
  max,
  mode: ColorMapMode.Elevation,
});

// Attach the inspector
Inspector.attach("inspector", instance);

function updateMode(value) {
  map.removeLayer(map.getLayers()[0]);

  switch (value) {
    case "elevation-colormap":
      map.addLayer(
        new ElevationLayer({
          name: value,
          extent,
          source,
          colorMap: viridis,
          minmax: { min, max },
        }),
      );
      break;
    case "elevation":
      map.addLayer(
        new ElevationLayer({
          name: value,
          extent,
          source,
          minmax: { min, max },
        }),
      );
      break;
    case "8bit":
      map.addLayer(
        new ColorLayer({
          name: value,
          extent,
          source,
          interpretation: Interpretation.CompressTo8Bit(min, max),
        }),
      );
      break;
    case "colormap":
      map.addLayer(
        new ColorLayer({
          name: value,
          extent,
          source,
          colorMap: magma,
        }),
      );
      break;
    default:
      break;
  }

  instance.notifyChange(map);
}

bindDropDown("mode", updateMode);

updateMode("elevation");
index.html
<!doctype html>
<html lang="en">
  <head>
    <title>Elevation GeoTIFF</title>
    <meta charset="UTF-8" />
    <meta name="name" content="cog_elevation" />
    <meta
      name="description"
      content="Display an elevation GeoTIFF with a color map."
    />
    <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">
      <!-- Top color layer -->
      <div class="card">
        <div class="card-body">
          <div class="input-group">
            <span class="input-group-text flex-grow-1">Read COG as</span>
            <select
              class="btn btn-outline-primary btn-sm"
              id="mode"
              autocomplete="off"
            >
              <option selected value="elevation">Elevation layer</option>
              <option value="elevation-colormap">
                Elevation layer (with colormap)
              </option>
              <option value="8bit">Color layer (compressed to 8-bit)</option>
              <option value="colormap">Color layer (with colormap)</option>
            </select>
          </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_elevation",
    "dependencies": {
        "colormap": "^2.3.2",
        "@giro3d/giro3d": "0.42.3"
    },
    "devDependencies": {
        "vite": "^3.2.3"
    },
    "scripts": {
        "start": "vite",
        "build": "vite build"
    }
}