Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

A GeoTIFF scaled band by band

Balance Sentinel-2 true-colour GeoTIFF contrast in OpenLayers without shaders by configuring independent per-band min, max and nodata arrays.

The Sentinel-2 scene east of Kraków, brighter and warmer than the plain file: the city in pinkish grey, the Niepołomice Forest dark, fields in beige and brown and the slanted edge on the right.
A screenshot of the example. Contains modified Copernicus Sentinel data 2024.

What this example shows

A Copernicus Sentinel-2 true-colour scene (TCI.tif) covering Kraków, the Niepołomice Forest and surrounding farmland renders in its native UTM projection with noticeably brighter, colour-balanced contrast and transparent outer swath borders—achieved purely through source band configuration with zero custom WebGL style expressions.

When you need it

Raw earth-observation imagery frequently clusters pixel values inside a narrow sub-range of 0..255 or 0..65535 and suffers from atmospheric blue haze in the blue channel, looking dull or colour-cast out of the box.

Configuring per-band min and max arrays directly on SourceInfo stretches contrast and neutralises colour casts during tile decoding without writing custom shader math.

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.

geotiff-per-band-stats.ts
import { ChangeDetectionStrategy, Component, 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 { SourceInfo } from 'ol/source/GeoTIFF';

/** A true-colour Sentinel-2 scene over Kraków stretched with its own minimum and maximum for each of the red, green and blue bands. */
@Component({
  selector: 'app-geotiff-per-band-stats',
  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]="options.center"
          [zoom]="options.zoom"
        />
      }
      <aol-layer-webgltile>
        <aol-source-geotiff
          [sources]="sources"
          attributions="Contains modified Copernicus Sentinel data 2024"
          (viewReady)="view.set($event)"
        />
      </aol-layer-webgltile>
    </aol-map>
  `,
})
export class GeotiffPerBandStats {
  readonly view = signal<ViewOptions | undefined>(undefined);

  protected readonly sources: SourceInfo[] = [
    {
      url: 'https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/34/U/DA/2024/8/S2B_34UDA_20240814_0_L2A/TCI.tif',
      nodata: 0,
      min: [10, 20, 10],
      max: [160, 170, 150],
    },
  ];
}

Architecture and OpenLayers mechanics

The single entry in sources points at TCI.tif with nodata: 0, min: [10, 20, 10] and max: [160, 170, 150] corresponding to the red, green and blue bands respectively. With default normalization active, aol-source-geotiff linearly rescales each band i from [min[i], max[i]] onto 0..255 (255 * (v - min) / (max - min)) and clamps out-of-range values.

(viewReady)="view.set($event)" passes the file’s native UTM projection, resolutions, extent, center and zoom straight into aol-view inside @if (view(); as options).

How it works

  • Configuring min: [10, 20, 10] and max: [160, 170, 150] stretches the red, green and blue bands of TCI.tif independently.
  • OpenLayers maps each band linearly onto bytes (0..255) by 255 / (max - min) and clamps values below min or above max.
  • nodata: 0 appends an alpha channel that turns outer collar pixels transparent where all three RGB bands equal 0.

Pitfalls

  • Multi-band nodata transparency triggers strictly when every band of a pixel matches its no-data value—a dark shadow pixel that hits 0 in red alone while green is 12 remains completely opaque.
  • Setting min too high crushes dark forest shadows to solid black (0), while setting max too low clips bright industrial roofs and clouds to pure white (255).

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.