Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

Vegetation and water from three bands

Combine 10 m and 20 m Sentinel-2 red, NIR and SWIR Cloud Optimized GeoTIFFs into one multi-index NDVI and NDWI WebGL composite in OpenLayers.

Kraków and its surroundings in cyan and dark blue: vegetation bright cyan, the city dark, the Vistula a thin red line through the middle and small red and blue patches of water.
A screenshot of the example. Contains modified Copernicus Sentinel data 2024.

What this example shows

Centred on Kraków at [resolution]="40" in UTM zone 34N, three separate Sentinel-2 spectral band files of mixed native resolutions (10 m and 20 m) fuse on the GPU into one false-colour environmental composite: healthy forests and meadows glow bright cyan/green, built-up city blocks stay dark, and the Vistula river plus nearby gravel pits stand out sharply.

When you need it

Wetland delineation, flood-extent mapping and habitat classification simultaneously track vegetation vigour and surface moisture by combining visible, near-infrared (10 m) and short-wave infrared (20 m) channels.

OpenLayers’ GeoTIFF source resamples multi-resolution band files (10 m and 20 m) into aligned GPU textures automatically so WebGL shaders can cross-compute multiple spectral indices in a single pass.

The code

The example uses cc-openlayers, my Angular component library for OpenLayers: each aol-… element creates the OpenLayers object it names, updates it from its inputs and disposes of it with the component.

cog-math-multisource.shared.ts
import type { Coordinate } from 'ol/coordinate.js';
import type { ExpressionValue } from 'ol/expr/expression.js';
import type { Style } from 'ol/layer/WebGLTile.js';
import type { SourceInfo } from 'ol/source/GeoTIFF.js';

const SCENE =
  'https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/34/U/DA/2024/8/S2B_34UDA_20240814_0_L2A';

/** The Main Market Square in the scene's projection, UTM zone 34N (EPSG:32634). */
export const KRAKOW: Coordinate = [423901, 5545999];

/** Band 1 is red, band 2 near infrared (both 10 m), band 3 short-wave infrared (20 m); band 4 is the alpha band nodata adds. */
const ndvi: ExpressionValue = ['/', ['-', ['band', 2], ['band', 1]], ['+', ['band', 2], ['band', 1]]];
const ndwi: ExpressionValue = ['/', ['-', ['band', 3], ['band', 1]], ['+', ['band', 3], ['band', 1]]];

/** One band file of the scene, with 0 as nodata and the full 16-bit range. */
function band(name: string): SourceInfo {
  return { url: `${SCENE}/${name}.tif`, min: 0, max: 65535, nodata: 0 };
}

export const SOURCES: SourceInfo[] = [band('B04'), band('B08'), band('B11')];

export const STYLE: Style = {
  color: [
    'color',
    // red: | NDVI - NDWI |
    ['*', 255, ['abs', ['-', ndvi, ndwi]]],
    // green: NDVI
    ['*', 255, ndvi],
    // blue: NDWI
    ['*', 255, ndwi],
    // alpha
    ['band', 4],
  ],
};
cog-math-multisource.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import { DefaultControlComponent, DefaultInteractionComponent, MapComponent, ViewComponent } from 'cc-openlayers';
import { LayerWebGLTileComponent, SourceGeoTIFFComponent } from 'cc-openlayers/webgl';
import type { ViewOptions } from 'ol/View';
import { KRAKOW, SOURCES, STYLE } from './cog-math-multisource.shared';

/**
 * NDVI as green and NDWI as blue (their difference as red), calculated from three Sentinel-2 COGs of a scene over
 * Kraków: red and near infrared at 10 m and short-wave infrared at 20 m.
 */
@Component({
  selector: 'app-cog-math-multisource',
  imports: [
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerWebGLTileComponent,
    MapComponent,
    SourceGeoTIFFComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      @if (view(); as options) {
        <aol-view
          [projection]="options.projection"
          [resolutions]="options.resolutions"
          [extent]="options.extent"
          [center]="krakow"
          [resolution]="40"
        />
      }
      <aol-layer-webgltile [style]="style">
        <aol-source-geotiff
          [sources]="sources"
          attributions="Contains modified Copernicus Sentinel data 2024"
          (viewReady)="view.set($event)"
        />
      </aol-layer-webgltile>
    </aol-map>
  `,
})
export class CogMathMultisource {
  readonly view = signal<ViewOptions | undefined>(undefined);
  protected readonly krakow = KRAKOW;
  protected readonly sources = SOURCES;
  protected readonly style = STYLE;
}

Architecture and OpenLayers mechanics

Helper band configures SOURCES over B04 (10 m red, band 1), B08 (10 m NIR, band 2) and B11 (20 m SWIR, band 3), each with min: 0, max: 65535, nodata: 0. In STYLE, ndvi computes (band2 - band1) / (band2 + band1) and ndwi computes (band3 - band1) / (band3 + band1).

An RGBA 'color' expression maps 255 * |ndvi - ndwi| to Red, 255 * ndvi to Green, 255 * ndwi to Blue, and passes ['band', 4]—the combined nodata alpha band—as the fourth channel.

How it works

  • band configures B04 (10 m red), B08 (10 m NIR) and B11 (20 m SWIR) as bands 1–3, each with min: 0, max: 65535, nodata: 0.
  • ndvi evaluates (band2 - band1) / (band2 + band1) while ndwi evaluates (band3 - band1) / (band3 + band1).
  • STYLE outputs 255 * |ndvi - ndwi| in red, 255 * ndvi in green, 255 * ndwi in blue and ['band', 4] as alpha.

Pitfalls

  • When nodata is set across multiple files in one GeoTIFF source, OpenLayers appends one shared alpha channel after the data bands of all files—making alpha ['band', 4] after three single-band TIFFs, even if API docs can look as though alpha follows a single source.
  • Remember that WebGL's color operator expects 0..255 RGB values and a 0..1 alpha value, unlike the array operator which expects 0..1 across all four channels.

The same feature in the OpenLayers examples .

Background: state and lifecycle of maps in Angular with OpenLayers, which the components of cc-openlayers take care of.