Raster, GeoTIFF and WebGL · Angular, React and OpenLayers
Flooding the terrain on the GPU
Simulate real-time terrain inundation on the GPU in OpenLayers by decoding Terrarium RGB elevation tiles inside a WebGLTileLayer style rule.

What this example shows
Over OpenStreetMap of Kraków and the Vistula valley ([opacity]="0.6"), scrubbing the Water level slider floods low-lying terrain in crisp light blue ([139, 212, 255, 1]) smoothly on the GPU—paired with preset jump buttons for four regional towns.
When you need it
Compared with CPU worker RasterSource flooding (sea-level), evaluating elevation thresholds inside a WebGL fragment shader eliminates inter-thread pixel copying and responds instantaneously during continuous slider scrubbing and pinch-zooming.
GPU-driven elevation styling scales effortlessly to full-screen 4K and mobile Retina viewports.
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.
webgl-sea-level.shared.tsimport type { ExpressionValue } from 'ol/expr/expression.js';
import type { Style } from 'ol/layer/WebGLTile.js';
export interface Place {
name: string;
lon: number;
lat: number;
}
/** The Vistula valley and the neighbouring towns, which the buttons move the map to. */
export const PLACES: Place[] = [
{ name: 'Kraków', lon: 19.9368, lat: 50.0614 },
{ name: 'Tarnów', lon: 20.9877, lat: 50.0121 },
{ name: 'Oświęcim', lon: 19.2238, lat: 50.0343 },
{ name: 'Nowy Sącz', lon: 20.6877, lat: 49.6249 },
];
/**
* The elevation in metres of a Terrarium tile's pixel, `R * 256 + G + B / 256 - 32768`: the band math reads the
* bands from 0 to 1, so each is scaled by 255.
*/
export const ELEVATION: ExpressionValue = [
'+',
-32768,
['*', 255 * 256, ['band', 1]],
['*', 255, ['band', 2]],
['*', 255 / 256, ['band', 3]],
];
/** Light blue where the terrain is at or below the `level` variable, transparent elsewhere. */
export const FLOOD: Style = {
color: ['case', ['<=', ELEVATION, ['var', 'level']], [139, 212, 255, 1], [139, 212, 255, 0]],
};webgl-sea-level.tsimport { ChangeDetectionStrategy, Component, computed, signal, viewChild } from '@angular/core';
import { fromLonLat } from 'ol/proj';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
MapComponent,
SourceImageTileComponent,
SourceOsmComponent,
ViewComponent,
} from 'cc-openlayers';
import { LayerWebGLTileComponent } from 'cc-openlayers/webgl';
import { FLOOD, PLACES, type Place } from './webgl-sea-level.shared';
/**
* The Vistula valley flooded up to a chosen level by a WebGL tile layer: its style compares the elevation of
* Terrarium tiles with a style variable, which the slider updates in place.
*/
@Component({
selector: 'app-webgl-sea-level',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
LayerTileComponent,
LayerWebGLTileComponent,
MapComponent,
SourceImageTileComponent,
SourceOsmComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="11">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-webgltile [opacity]="0.6" [style]="style" [variables]="variables()">
<aol-source-imagetile
url="https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
[maxZoom]="15"
attributions='Elevation: <a href="https://github.com/tilezen/joerd/blob/master/docs/attribution.md" target="_blank">Terrain Tiles</a>'
/>
</aol-layer-webgltile>
</aol-map>
<div class="example-controls">
<label>
Water level
<input type="range" name="level" min="180" max="260" [value]="level()" (input)="setLevel($event)" />
<output>{{ level() }}</output>
m above sea level
</label>
@for (place of places; track place.name) {
<button type="button" (click)="goTo(place)">{{ place.name }}</button>
}
</div>
`,
})
export class WebglSeaLevel {
readonly level = signal(200);
protected readonly variables = computed(() => ({ level: this.level() }));
protected readonly style = FLOOD;
protected readonly places = PLACES;
private readonly map = viewChild.required(MapComponent);
protected setLevel(event: Event): void {
this.level.set((event.target as HTMLInputElement).valueAsNumber);
}
protected goTo(place: Place): void {
const view = this.map().instance.getView();
view.setCenter(fromLonLat([place.lon, place.lat]));
view.setZoom(11);
}
} 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.
webgl-sea-level.react.tsximport { useMemo, useRef, useState, type ChangeEvent } from 'react';
import type OLMap from 'ol/Map.js';
import { fromLonLat } from 'ol/proj.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
ImageTileSource,
Map,
OSMSource,
TileLayer,
View,
} from 'cc-openlayers/react';
import { WebGLTileLayer } from 'cc-openlayers/react/webgl';
import { FLOOD, PLACES, type Place } from './webgl-sea-level.shared';
/**
* The Vistula valley flooded up to a chosen level by a WebGL tile layer: its style compares the elevation of
* Terrarium tiles with a style variable, which the slider updates in place.
*/
export function WebglSeaLevel() {
const [level, setLevel] = useState(200);
const variables = useMemo(() => ({ level }), [level]);
const map = useRef<OLMap>(null);
const goTo = (place: Place) => {
const view = map.current?.getView();
view?.setCenter(fromLonLat([place.lon, place.lat]));
view?.setZoom(11);
};
return (
<>
<Map ref={map} height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={11}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<WebGLTileLayer opacity={0.6} style={FLOOD} variables={variables}>
<ImageTileSource
url="https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"
maxZoom={15}
attributions='Elevation: <a href="https://github.com/tilezen/joerd/blob/master/docs/attribution.md" target="_blank">Terrain Tiles</a>'
/>
</WebGLTileLayer>
</Map>
<div className="example-controls">
<label>
Water level
<input
type="range"
name="level"
min="180"
max="260"
value={level}
onChange={(event: ChangeEvent<HTMLInputElement>) => setLevel(event.target.valueAsNumber)}
/>
<output>{level}</output>m above sea level
</label>
{PLACES.map((place) => (
<button key={place.name} type="button" onClick={() => goTo(place)}>
{place.name}
</button>
))}
</div>
</>
);
}webgl-sea-level.shared.tsimport type { ExpressionValue } from 'ol/expr/expression.js';
import type { Style } from 'ol/layer/WebGLTile.js';
export interface Place {
name: string;
lon: number;
lat: number;
}
/** The Vistula valley and the neighbouring towns, which the buttons move the map to. */
export const PLACES: Place[] = [
{ name: 'Kraków', lon: 19.9368, lat: 50.0614 },
{ name: 'Tarnów', lon: 20.9877, lat: 50.0121 },
{ name: 'Oświęcim', lon: 19.2238, lat: 50.0343 },
{ name: 'Nowy Sącz', lon: 20.6877, lat: 49.6249 },
];
/**
* The elevation in metres of a Terrarium tile's pixel, `R * 256 + G + B / 256 - 32768`: the band math reads the
* bands from 0 to 1, so each is scaled by 255.
*/
export const ELEVATION: ExpressionValue = [
'+',
-32768,
['*', 255 * 256, ['band', 1]],
['*', 255, ['band', 2]],
['*', 255 / 256, ['band', 3]],
];
/** Light blue where the terrain is at or below the `level` variable, transparent elsewhere. */
export const FLOOD: Style = {
color: ['case', ['<=', ELEVATION, ['var', 'level']], [139, 212, 255, 1], [139, 212, 255, 0]],
};How the React version works
The Vistula valley flooded up to a chosen level by a WebGL tile layer whose style compares the elevation from Terrarium tiles with a level variable; buttons move the map to towns.
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 towns, the elevation expression and the flood style from the example's .shared.ts file, so the logic exists once.
From Angular to React
| Angular | React |
|---|---|
<aol-layer-webgltile [style] [variables]> over <aol-layer-tile> with OpenStreetMap | <WebGLTileLayer style variables> over <TileLayer> with <OSMSource>, the variables made in useMemo |
the slider and a signal of the level | useState |
the town buttons and viewChild of the map | the buttons with useRef<OLMap> |
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
Style expression ELEVATION decodes Terrarium heights from ['band', 1], ['band', 2] and ['band', 3]—multiplying each normalized 0..1 WebGL band value by 255 first before applying (R * 256 + G + B / 256) - 32768.
Conditional colour expression FLOOD outputs [139, 212, 255, 1] wherever the decoded elevation stays at or below ['var', 'level'] and transparent [139, 212, 255, 0] above it, driven reactively via [variables]="variables()".
How it works
FLOODcolours pixels[139, 212, 255, 1]at or below['var', 'level']and sets alpha0above that threshold.ELEVATIONmultiplies normalized 0..1 values of['band', 1],['band', 2]and['band', 3]by255to apply the Terrarium height formula.[variables]="variables()"updateslevelas a GPU uniform without shader recompilation, rendered at[opacity]="0.6"over OpenStreetMap.
Pitfalls
- WebGL tile shaders expose standard 8-bit PNG bands normalized from
0to1, whereas the Terrarium formula expects0to255byte values—forgetting * 255 collapses decoded elevations right down near-32768metres!
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.