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.

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.tsimport 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.tsimport { 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 }));
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
cog-stretch.react.tsximport { Fragment, useMemo, useState, type ChangeEvent } from 'react';
import type { ViewOptions } from 'ol/View.js';
import { DefaultControls, DefaultInteractions, Map, View } from 'cc-openlayers/react';
import { GeoTIFFSource, WebGLTileLayer } from 'cc-openlayers/react/webgl';
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.
*/
export function CogStretch() {
const [view, setView] = useState<ViewOptions | undefined>(undefined);
/** The band each channel shows and the raw value it reaches full brightness at. */
const [shown, setShown] = useState<Record<Channel, number>>({ red: 1, green: 2, blue: 3 });
const [max, setMax] = useState<Record<Channel, number>>({ red: 3000, green: 3000, blue: 3000 });
const variables = useMemo(
() => ({ ...shown, redMax: max.red, greenMax: max.green, blueMax: max.blue }),
[shown, max]
);
const choose = (channel: Channel, event: ChangeEvent<HTMLSelectElement>) => {
const band = Number(event.target.value);
setShown((current) => ({ ...current, [channel]: band }));
};
const stretch = (channel: Channel, event: ChangeEvent<HTMLInputElement>) => {
const value = event.target.valueAsNumber;
setMax((current) => ({ ...current, [channel]: value }));
};
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
{view && (
<View
projection={view.projection}
resolutions={view.resolutions}
extent={view.extent}
center={KRAKOW}
resolution={40}
/>
)}
<WebGLTileLayer style={STYLE} variables={variables}>
<GeoTIFFSource
sources={SOURCES}
normalize={false}
attributions="Contains modified Copernicus Sentinel data 2024"
onViewReady={setView}
/>
</WebGLTileLayer>
</Map>
<div className="example-controls">
{CHANNELS.map((channel) => (
<Fragment key={channel.name}>
<label>
{channel.label} channel
<select name={channel.name} value={shown[channel.name]} onChange={(event) => choose(channel.name, event)}>
{BANDS.map((band) => (
<option key={band.value} value={band.value}>
{band.label}
</option>
))}
</select>
</label>
<label>
{channel.label} max
<input
name={`${channel.name}Max`}
type="range"
min="2000"
max="5000"
value={max[channel.name]}
onChange={(event) => stretch(channel.name, event)}
/>
<output>{max[channel.name]}</output>
</label>
</Fragment>
))}
</div>
</>
);
}cog-stretch.shared.tsimport 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' },
];How the React version works
The red, green, blue and near-infrared files of a scene over Kraków read as raw values; each colour channel shows a band chosen from a list, stretched to a maximum set by a slider, without restyling the layer. The source reads the file in the browser, so nothing is drawn before its metadata arrive.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. OpenLayers draws the tiles with WebGL; the style expressions run on the GPU in both versions. Both versions import the centre over Kraków, the channels, the band list, the sources and the style from the example's .shared.ts file, so the logic exists once. The style reads the bands and maxima through variables, so a change updates only the variables.
From Angular to React
| Angular | React |
|---|---|
<aol-layer-webgltile> with <aol-source-geotiff [sources]> inside | <WebGLTileLayer> with <GeoTIFFSource sources> inside, both from cc-openlayers/react/webgl |
(viewReady) and a signal of the view options | onViewReady and useState; the View is rendered once the options are there |
the lists, sliders and variables in a computed | useState of the chosen bands and maxima and variables made in useMemo |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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 span2000to5000from an initial value of3000.
Pitfalls
- Without
[normalize]="false"(or explicitmaxbounds), 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
sourcesare still requested for visible tiles even while near-infrared band 4 is unselected.
Related examples
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.