Raster, GeoTIFF and WebGL · Angular, React and OpenLayers

A heatmap of tremors from KML

Render 1,130 KML seismic events across Upper Silesia and Kraków as a magnitude-weighted OpenLayers Heatmap layer with radius and blur sliders.

Esri's light grey map of southern Poland with a heatmap of tremors, red over Upper Silesia from Tarnowskie Góry to Katowice and over Karviná, and the radius and blur sliders below.
A screenshot of the example. Seismic events: U.S. Geological Survey. Base map: Esri World Light Gray (Esri, HERE, Garmin, OpenStreetMap contributors and the GIS user community).

What this example shows

Sandwiched cleanly between Esri’s World Light Gray base map and an upper reference layer of town labels, 1,130 seismic tremors from assets/krakow/tremors.kml glow as a continuous density heatmap peaking over Upper Silesia and Karviná, with live range sliders adjusting kernel radius (starting at 5) and blur (starting at 15).

When you need it

When hundreds of tremors, emergency calls, wildlife sightings or retail deliveries stack directly on top of the same coordinates, discrete circle markers saturate into opaque blobs whereas a weighted kernel-density heatmap reveals true hotspots.

Sandwiching the WebGL heatmap below a transparent reference label layer keeps city names crisp and readable right over intense red hotspots.

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.

heatmap-earthquakes.shared.ts
import type { FeatureLike } from 'ol/Feature.js';

/** The heat weight of a tremor: its magnitude above 2, read from the name ("M 3.4 - 1995 Mar 7") in tremors.kml. */
export const weight = (feature: FeatureLike): number => {
  // every event has a magnitude of 2 or more.
  const name: string = feature.get('name');
  const magnitude = parseFloat(name.slice(2));
  return magnitude - 2;
};
heatmap-earthquakes.ts
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import KML from 'ol/format/KML';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  MapComponent,
  SourceImageTileComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import { LayerHeatmapComponent } from 'cc-openlayers/webgl';
import { weight } from './heatmap-earthquakes.shared';

/**
 * The seismic events around Kraków since 1978, most of them mining tremors of the Upper Silesian coal basin west of
 * the city, read from a KML file and drawn as a heatmap weighted by magnitude; the sliders set its radius and blur.
 */
@Component({
  selector: 'app-heatmap-earthquakes',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerHeatmapComponent,
    LayerTileComponent,
    MapComponent,
    SourceImageTileComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="9">
        <aol-coordinate [x]="19.25" [y]="50.15" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile>
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="16"
          attributions="Tiles © Esri — Esri, HERE, Garmin, © OpenStreetMap contributors, and the GIS user community"
        />
      </aol-layer-tile>
      <aol-layer-heatmap [blur]="blur()" [radius]="radius()" [weight]="weight">
        <aol-source-vector
          url="assets/krakow/tremors.kml"
          [format]="format"
          attributions="Seismic events: U.S. Geological Survey"
        />
      </aol-layer-heatmap>
      <aol-layer-tile>
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Reference/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="16"
        />
      </aol-layer-tile>
    </aol-map>
    <div class="example-controls">
      <label>
        radius size
        <input
          name="radius"
          type="range"
          min="1"
          max="50"
          step="1"
          [value]="radius()"
          (input)="radius.set(read($event))"
        />
      </label>
      <label>
        blur size
        <input name="blur" type="range" min="1" max="50" step="1" [value]="blur()" (input)="blur.set(read($event))" />
      </label>
    </div>
  `,
})
export class HeatmapEarthquakes {
  protected readonly radius = signal(5);
  protected readonly blur = signal(15);
  protected readonly format = new KML({
    extractStyles: false,
  });

  protected readonly weight = weight;

  protected read(event: Event): number {
    return parseInt((event.target as HTMLInputElement).value, 10);
  }
}

Architecture and OpenLayers mechanics

The vector source loads assets/krakow/tremors.kml via new KML({ with extractStyles: false. On the Heatmap layer, weight parses magnitude from each placemark’s name (parseFloat(name.slice(2))) and subtracts 2, while [radius]="radius()" and [blur]="blur()" invoke setRadius and setBlur directly for instant GPU re-rendering.

How it works

  • assets/krakow/tremors.kml loads with new KML({ and extractStyles: false so KML pushpins are ignored.
  • weight extracts event magnitude via parseFloat(name.slice(2)) and returns magnitude - 2.
  • [radius]="radius()" and [blur]="blur()" start at 5 and 15, updating the WebGL heatmap without shader recompilation.
  • Placing Esri’s reference label tiles above the heatmap keeps town names legible over dense clusters.

Pitfalls

  • OpenLayers clamps Heatmap weight return values strictly to [0, 1]: returning magnitude - 2 unscaled means every tremor of M 3.0 or stronger clamps to 1.0—divide by (maxMagnitude - 2) when you want M 5.0 events weighted more heavily than M 3.0.
  • Plain numeric [radius] and [blur] inputs update uniforms cheaply via setRadius / setBlur; if you pass WebGL expressions instead, reference a style variable inside them to avoid recompiling shaders.

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.