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.

The Sentinel-2 scene of Kraków in true colour, the city, the Vistula and Balice airport among the fields, with the Set the layer style select on True Color below.
A screenshot of the example. Contains modified Copernicus Sentinel data 2024.

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.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];

/** 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.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 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);
  }
}

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

  • STYLES defines trueColor, falseColor, ndvi, ndviPalettePlasma and ndviPaletteViridis, bound reactively via [style]="style()".
  • Expression helper normalize scales raw band values by / 3000, while falseColor maps near-infrared band 4 into the red channel (gamma: 1.1).
  • ndvi linearly interpolates (nir - red) / (nir + red) across 20 RGB stops (-0.2 to 0.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" on aol-source-geotiff, which disables automatic 0..1 byte scaling in the source.

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.