Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

A GeoTIFF fetched through your own loader

Fetch Cloud Optimized GeoTIFF byte ranges through a custom OpenLayers HTTP loader hook for auth headers, retries or caching, keeping Range requests intact.

A true-colour Sentinel-2 scene east of Kraków: the city's grey in the west, the dark Niepołomice Forest in the middle, fields all around and the scene's slanted eastern edge on the right.
A screenshot of the example. Contains modified Copernicus Sentinel data 2024.

What this example shows

A true-colour Sentinel-2 scene spanning eastern Kraków and the dark Niepołomice Forest streams byte-range chunks through an application-controlled fetch loader function, automatically centring the viewport on the scene’s footprint (withExtentCenter()).

When you need it

Commercial satellite tasking APIs, enterprise STAC catalogues and private S3 buckets protect Cloud Optimized GeoTIFFs behind Bearer tokens, signed-header proxies, custom retry policies or request telemetry.

Providing a custom loader lets your code attach Authorization headers or retry transient 503s while keeping geotiff.js partial byte-range streaming intact.

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-custom-client.shared.ts
import type { SourceInfo } from 'ol/source/GeoTIFF.js';

export const 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',
    // The loader controls how the data is fetched: it receives the URL, the request headers and an abort signal.
    loader: (url, headers, abortSignal) =>
      // Custom headers (an authorization token, say), logging, retries or caching would go here.
      fetch(url, { headers: { ...headers }, signal: abortSignal }),
  },
];
cog-custom-client.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import { DefaultControlComponent, DefaultInteractionComponent, MapComponent, ViewComponent } from 'cc-openlayers';
import { LayerWebGLTileComponent, SourceGeoTIFFComponent } from 'cc-openlayers/webgl';
import { getView, withExtentCenter, withHigherResolutions, type ViewOptions } from 'ol/View';
import { SOURCES } from './cog-custom-client.shared';

/** A true-colour Sentinel-2 scene over Kraków whose requests go through a custom loader function. */
@Component({
  selector: 'app-cog-custom-client',
  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"
          [center]="options.center"
          [zoom]="options.zoom"
        />
      }
      <aol-layer-webgltile>
        <aol-source-geotiff
          [sources]="sources"
          attributions="Contains modified Copernicus Sentinel data 2024"
          (viewReady)="showScene()"
        />
      </aol-layer-webgltile>
    </aol-map>
  `,
})
export class CogCustomClient {
  readonly view = signal<ViewOptions | undefined>(undefined);
  private readonly source = viewChild.required(SourceGeoTIFFComponent);

  protected readonly sources = SOURCES;

  /** Shows the whole scene with one more level of resolution, centred on its extent. */
  protected showScene(): void {
    void getView(this.source().instance, withHigherResolutions(1), withExtentCenter()).then((options) =>
      this.view.set(options)
    );
  }
}

Architecture and OpenLayers mechanics

Each source entry supplies a custom loader callback invoked by OpenLayers / geotiff.js with (url, headers, abortSignal) and returning fetch(url, { headers: { ...headers }, signal: abortSignal }).

When (viewReady)="showScene()" fires, getView(this.source().instance, withHigherResolutions(1), withExtentCenter()) reads metadata from viewChild.required(SourceGeoTIFFComponent), adds one overzoom step, and centres the view on the scene.

How it works

  • Custom loader intercepts every tile/header request with URL, request headers and cancellation abortSignal, returning fetch(url, { headers: { ...headers }, signal: abortSignal }).
  • (viewReady)="showScene()" constructs view options via getView(this.source().instance, withHigherResolutions(1), withExtentCenter()) using viewChild.required(SourceGeoTIFFComponent).
  • withHigherResolutions(1) enables 2× overzoom while withExtentCenter() centres the camera without locking pan bounds.

Pitfalls

  • Always forward incoming headers ({ ...headers, Authorization: ... }) inside your custom fetch call: geotiff.js relies on the Range: bytes=... header in headers, and dropping it causes servers to return HTTP 200 with the entire file (Server responded with full file error).
  • Note that withExtentCenter() intentionally omits the constraining extent from view options so users can pan freely past the scene’s slanted swath border.

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.