Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

A band and a stretch for each colour channel

Assign Sentinel-2 bands and live brightness-stretch sliders to RGB channels in OpenLayers WebGL using unnormalized GeoTIFFs and style variables.

A true-colour Sentinel-2 image of Kraków among its fields, with the runway of Balice airport in the west, and the band selects and 3000 sliders of the red, green and blue channels below.
A screenshot of the example. Contains modified Copernicus Sentinel data 2024.

What this example shows

Centred on Kraków and Balice airport at 40 m/px in UTM zone 34N, four 16-bit Sentinel-2 spectral bands (visible red, visible green, visible blue and near infrared) feed an interactive compositing console below the map.

Each Red, Green and Blue display channel pairs a band <select> with a live 2000–5000 range slider (starting at 3000), updating false-colour combinations and per-channel white points at 60 fps as you drag.

When you need it

Remote-sensing analysts constantly switch between natural colour (R=B04, G=B03, B=B02) and colour-infrared vegetation composites (R=B08, G=B04, B=B03) while dragging white-point stretch sliders to bring out crop vigour, burn scars or urban roofs.

Driving both band selection and stretch ceilings through WebGL style variables updates GPU uniforms instantaneously without recompiling shaders on every slider tick.

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-stretch.shared.ts
import type { Coordinate } from 'ol/coordinate.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];

export type Channel = 'red' | 'green' | 'blue';

/** Each channel shows the band its variable names, stretched so that its `…Max` variable is full brightness. */
export const STYLE: Style = {
  color: [
    'array',
    ['/', ['band', ['var', 'red']], ['var', 'redMax']],
    ['/', ['band', ['var', 'green']], ['var', 'greenMax']],
    ['/', ['band', ['var', 'blue']], ['var', 'blueMax']],
    1,
  ],
};

/** Bands 1 to 4 of the style, one file each. */
export const SOURCES: SourceInfo[] = ['B04', 'B03', 'B02', 'B08'].map((name) => ({
  url: `${SCENE}/${name}.tif`,
}));

export const BANDS = [
  { value: 1, label: 'visible red' },
  { value: 2, label: 'visible green' },
  { value: 3, label: 'visible blue' },
  { value: 4, label: 'near infrared' },
];

export const CHANNELS: { name: Channel; label: string }[] = [
  { name: 'red', label: 'Red' },
  { name: 'green', label: 'Green' },
  { name: 'blue', label: 'Blue' },
];
cog-stretch.ts
import { ChangeDetectionStrategy, Component, computed, 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 { BANDS, type Channel, CHANNELS, KRAKOW, SOURCES, STYLE } from './cog-stretch.shared';

/**
 * Bands of a Sentinel-2 scene over Kraków chosen for the red, green and blue channels, each with its own contrast
 * stretch: the choices update the layer's style variables, not its style.
 */
@Component({
  selector: 'app-cog-stretch',
  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" [variables]="variables()">
        <aol-source-geotiff
          [sources]="sources"
          [normalize]="false"
          attributions="Contains modified Copernicus Sentinel data 2024"
          (viewReady)="view.set($event)"
        />
      </aol-layer-webgltile>
    </aol-map>
    <div class="example-controls">
      @for (channel of channels; track channel.name) {
        <label>
          {{ channel.label }} channel
          <select [name]="channel.name" (change)="choose(channel.name, $event)">
            @for (band of bands; track band.value) {
              <option [value]="band.value" [selected]="band.value === shown()[channel.name]">{{ band.label }}</option>
            }
          </select>
        </label>
        <label>
          {{ channel.label }} max
          <input
            [name]="channel.name + 'Max'"
            type="range"
            min="2000"
            max="5000"
            [value]="max()[channel.name]"
            (input)="stretch(channel.name, $event)"
          />
          <output>{{ max()[channel.name] }}</output>
        </label>
      }
    </div>
  `,
})
export class CogStretch {
  readonly view = signal<ViewOptions | undefined>(undefined);
  protected readonly krakow = KRAKOW;
  protected readonly style = STYLE;

  protected readonly sources = SOURCES;
  protected readonly bands = BANDS;
  protected readonly channels = CHANNELS;

  /** The band each channel shows and the raw value it reaches full brightness at. */
  readonly shown = signal<Record<Channel, number>>({ red: 1, green: 2, blue: 3 });
  readonly max = signal<Record<Channel, number>>({ red: 3000, green: 3000, blue: 3000 });
  protected readonly variables = computed(() => {
    const max = this.max();
    return { ...this.shown(), redMax: max.red, greenMax: max.green, blueMax: max.blue };
  });

  protected choose(channel: Channel, event: Event): void {
    const band = Number((event.target as HTMLSelectElement).value);
    this.shown.update((shown) => ({ ...shown, [channel]: band }));
  }

  protected stretch(channel: Channel, event: Event): void {
    const value = (event.target as HTMLInputElement).valueAsNumber;
    this.max.update((max) => ({ ...max, [channel]: value }));
  }
}

Architecture and OpenLayers mechanics

Four single-band COG files (B04, B03, B02, B08) load into aol-source-geotiff with [normalize]="false" so raw 16-bit reflectance integers pass unscaled to the GPU, framing Kraków at [resolution]="40" once (viewReady) resolves.

On aol-layer-webgltile, [style]="style" defines each output channel inside an RGBA 'array' expression as ['/', ['band', ['var', 'red']], ['var', 'redMax']] (and likewise for green and blue, with alpha 1). Computed signal [variables]="variables()" merges the three band indices (shown) with redMax, greenMax and blueMax—calling OpenLayers’ lightweight uniform update path rather than setStyle.

How it works

  • Four single-band COGs (B04, B03, B02, B08) mount as bands 1–4 with [normalize]="false" to preserve raw 16-bit reflectance values.
  • Each RGB channel evaluates ['/', ['band', ['var', 'red']], ['var', 'redMax']] (and matching green/blue expressions) inside [style].
  • Binding [variables]="variables()" updates WebGL uniforms directly via OpenLayers’ updateStyleVariables mechanism instead of recompiling shaders.
  • Channel selects start on true colour (1, 2, 3) while (input) range sliders span 2000 to 5000 from an initial value of 3000.

Pitfalls

  • Without [normalize]="false" (or explicit max bounds), un-annotated 16-bit Sentinel-2 TIFFs normalise across the full 0..65535 integer range—rendering typical 0..3000 land reflectance almost completely black; also note that [normalize]="false" relies on WebGL float-texture support on older mobile GPUs.
  • Style variables pick which band the shader displays, not which TIFFs the source fetches—all four band files in sources are still requested for visible tiles even while near-infrared band 4 is unselected.

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.