Raster, GeoTIFF and WebGL · Angular, React and OpenLayers
Five styles for one Sentinel-2 scene
Switch a 4-band Sentinel-2 GeoTIFF across True Color, NIR False Color, continuous NDVI ramps and discrete palette WebGL styles in OpenLayers.

What this example shows
Over Kraków, the Vistula valley and Balice airport at [resolution]="40" in UTM zone 34N, a Set the layer style selector switches one four-band Sentinel-2 scene live across five analytical visualisations: True Color, False Color (vegetation in vivid red), a 20-stop continuous NDVI green ramp, and two discrete 5-class NDVI colour palettes (Plasma and Viridis).
When you need it
Agronomy, forestry and urban heat-island viewers answer very different questions from the exact same multi-spectral satellite pass—needing natural colour for visual orientation alongside false-colour and vegetation-index modes.
Swapping declarative WebGL Style objects on a single GeoTIFF source delivers five thematic products in the browser without generating five separate server-side GeoTIFFs.
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-style.shared.tsimport 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];
/** A band's raw value as a fraction of 3000, a bright surface's value. */
function normalize(value: ExpressionValue): ExpressionValue {
return ['/', value, 3000];
}
const red = normalize(['band', 1]);
const green = normalize(['band', 2]);
const blue = normalize(['band', 3]);
const nir = normalize(['band', 4]);
const ndvi: ExpressionValue = ['/', ['-', nir, red], ['+', nir, red]];
/** NDVI from -0.2 to 0.65 scaled to an index into a palette of five colours. */
function ndviPalette(colors: string[]): Style {
return { color: ['palette', ['interpolate', ['linear'], ndvi, -0.2, 0, 0.65, 4], colors] };
}
export const STYLES = {
trueColor: { color: ['array', red, green, blue, 1], gamma: 1.1 },
falseColor: { color: ['array', nir, red, green, 1], gamma: 1.1 },
ndvi: {
color: [
'interpolate',
['linear'],
ndvi,
// colour ramp for NDVI values, ranging from -0.2 to 0.65
-0.2,
[191, 191, 191],
-0.1,
[219, 219, 219],
0,
[255, 255, 224],
0.025,
[255, 250, 204],
0.05,
[237, 232, 181],
0.075,
[222, 217, 156],
0.1,
[204, 199, 130],
0.125,
[189, 184, 107],
0.15,
[176, 194, 97],
0.175,
[163, 204, 89],
0.2,
[145, 191, 82],
0.25,
[128, 179, 71],
0.3,
[112, 163, 64],
0.35,
[97, 150, 54],
0.4,
[79, 138, 46],
0.45,
[64, 125, 36],
0.5,
[48, 110, 28],
0.55,
[33, 97, 18],
0.6,
[15, 84, 10],
0.65,
[0, 69, 0],
],
},
ndviPalettePlasma: ndviPalette(['#0d0887', '#7e03a8', '#cb4778', '#f89540', '#f0f921']),
ndviPaletteViridis: ndviPalette(['#440154', '#3b528b', '#21918c', '#5ec962', '#fde725']),
} satisfies Record<string, Style>;
export type StyleName = keyof typeof STYLES;
/** Bands 1 to 4 of the styles, one file each. */
export const SOURCES: SourceInfo[] = ['B04', 'B03', 'B02', 'B08'].map((name) => ({
url: `${SCENE}/${name}.tif`,
}));
export const OPTIONS: { value: StyleName; label: string }[] = [
{ value: 'trueColor', label: 'True Color' },
{ value: 'falseColor', label: 'False Color' },
{ value: 'ndvi', label: 'NDVI' },
{ value: 'ndviPalettePlasma', label: 'NDVI w/ palette 1' },
{ value: 'ndviPaletteViridis', label: 'NDVI w/ palette 2' },
];cog-style.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 type { Style } from 'ol/layer/WebGLTile';
import { KRAKOW, OPTIONS, SOURCES, STYLES, type StyleName } from './cog-style.shared';
/** A Sentinel-2 scene over Kraków in true colour, false colour or NDVI: a chosen style replaces the layer's style. */
@Component({
selector: 'app-cog-style',
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"
[normalize]="false"
attributions="Contains modified Copernicus Sentinel data 2024"
(viewReady)="view.set($event)"
/>
</aol-layer-webgltile>
</aol-map>
<div class="example-controls">
<label>
Set the layer style
<select name="style" (change)="choose($event)">
@for (option of options; track option.value) {
<option [value]="option.value" [selected]="option.value === chosen()">{{ option.label }}</option>
}
</select>
</label>
</div>
`,
})
export class CogStyle {
readonly view = signal<ViewOptions | undefined>(undefined);
protected readonly krakow = KRAKOW;
protected readonly sources = SOURCES;
protected readonly options = OPTIONS;
readonly chosen = signal<StyleName>('trueColor');
protected readonly style = computed<Style>(() => STYLES[this.chosen()]);
protected choose(event: Event): void {
this.chosen.set((event.target as HTMLSelectElement).value as StyleName);
}
} 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-style.react.tsximport { 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 { KRAKOW, OPTIONS, SOURCES, STYLES, type StyleName } from './cog-style.shared';
/** A Sentinel-2 scene over Kraków in true colour, false colour or NDVI: a chosen style replaces the layer's style. */
export function CogStyle() {
const [view, setView] = useState<ViewOptions | undefined>(undefined);
const [chosen, setChosen] = useState<StyleName>('trueColor');
const choose = (event: ChangeEvent<HTMLSelectElement>) => setChosen(event.target.value as StyleName);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
{view && (
<View
projection={view.projection}
resolutions={view.resolutions}
extent={view.extent}
center={KRAKOW}
resolution={40}
/>
)}
<WebGLTileLayer style={STYLES[chosen]}>
<GeoTIFFSource
sources={SOURCES}
normalize={false}
attributions="Contains modified Copernicus Sentinel data 2024"
onViewReady={setView}
/>
</WebGLTileLayer>
</Map>
<div className="example-controls">
<label>
Set the layer style
<select name="style" value={chosen} onChange={choose}>
{OPTIONS.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</select>
</label>
</div>
</>
);
}cog-style.shared.tsimport 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];
/** A band's raw value as a fraction of 3000, a bright surface's value. */
function normalize(value: ExpressionValue): ExpressionValue {
return ['/', value, 3000];
}
const red = normalize(['band', 1]);
const green = normalize(['band', 2]);
const blue = normalize(['band', 3]);
const nir = normalize(['band', 4]);
const ndvi: ExpressionValue = ['/', ['-', nir, red], ['+', nir, red]];
/** NDVI from -0.2 to 0.65 scaled to an index into a palette of five colours. */
function ndviPalette(colors: string[]): Style {
return { color: ['palette', ['interpolate', ['linear'], ndvi, -0.2, 0, 0.65, 4], colors] };
}
export const STYLES = {
trueColor: { color: ['array', red, green, blue, 1], gamma: 1.1 },
falseColor: { color: ['array', nir, red, green, 1], gamma: 1.1 },
ndvi: {
color: [
'interpolate',
['linear'],
ndvi,
// colour ramp for NDVI values, ranging from -0.2 to 0.65
-0.2,
[191, 191, 191],
-0.1,
[219, 219, 219],
0,
[255, 255, 224],
0.025,
[255, 250, 204],
0.05,
[237, 232, 181],
0.075,
[222, 217, 156],
0.1,
[204, 199, 130],
0.125,
[189, 184, 107],
0.15,
[176, 194, 97],
0.175,
[163, 204, 89],
0.2,
[145, 191, 82],
0.25,
[128, 179, 71],
0.3,
[112, 163, 64],
0.35,
[97, 150, 54],
0.4,
[79, 138, 46],
0.45,
[64, 125, 36],
0.5,
[48, 110, 28],
0.55,
[33, 97, 18],
0.6,
[15, 84, 10],
0.65,
[0, 69, 0],
],
},
ndviPalettePlasma: ndviPalette(['#0d0887', '#7e03a8', '#cb4778', '#f89540', '#f0f921']),
ndviPaletteViridis: ndviPalette(['#440154', '#3b528b', '#21918c', '#5ec962', '#fde725']),
} satisfies Record<string, Style>;
export type StyleName = keyof typeof STYLES;
/** Bands 1 to 4 of the styles, one file each. */
export const SOURCES: SourceInfo[] = ['B04', 'B03', 'B02', 'B08'].map((name) => ({
url: `${SCENE}/${name}.tif`,
}));
export const OPTIONS: { value: StyleName; label: string }[] = [
{ value: 'trueColor', label: 'True Color' },
{ value: 'falseColor', label: 'False Color' },
{ value: 'ndvi', label: 'NDVI' },
{ value: 'ndviPalettePlasma', label: 'NDVI w/ palette 1' },
{ value: 'ndviPaletteViridis', label: 'NDVI w/ palette 2' },
];How the React version works
A scene over Kraków drawn in a style chosen from a list: true colour, false colour with near infrared as red, an NDVI ramp from -0.2 to 0.65 or an NDVI palette of five colours. 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 four styles, the sources and the list of options from the example's .shared.ts file, so the logic exists once.
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 style list and a signal of the chosen name | useState and style={STYLES[chosen]}, which replaces the layer style |
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
aol-source-geotiff reads B04, B03, B02 and B08 with [normalize]="false". Inside cog-style.shared.ts, helper normalize divides raw band expressions by 3000, trueColor and falseColor build RGBA tuples with gamma: 1.1, ndvi computes (nir - red) / (nir + red) interpolated linearly across 20 stops from -0.2 to 0.65, and ndviPalettePlasma / ndviPaletteViridis map ['interpolate', ['linear'], ndvi, -0.2, 0, 0.65, 4] into 5 hex colours with the 'palette' operator.
Changing <select name="style"> updates [style]="style()" on aol-layer-webgltile.
How it works
STYLESdefinestrueColor,falseColor,ndvi,ndviPalettePlasmaandndviPaletteViridis, bound reactively via[style]="style()".- Expression helper
normalizescales raw band values by / 3000, whilefalseColormaps near-infrared band 4 into the red channel (gamma: 1.1). ndvilinearly interpolates (nir - red) / (nir + red) across 20 RGB stops (-0.2to0.65), whereas both palette styles index 5 discrete colours via'palette'.
Pitfalls
- Replacing
[style]invokes setStyle and compiles a fresh WebGL fragment shader—ideal for discrete preset dropdowns, whereas high-frequency slider scrubbing should mutate [variables] instead as in cog-stretch. - Do not confuse the local expression helper
normalize(/ 3000) in cog-style.shared.ts with the source input[normalize]="false"onaol-source-geotiff, which disables automatic 0..1 byte scaling in the source.
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.