Vector data · Angular, React and OpenLayers

Flight tracks from IGC

Replay multi-pilot IGC flight logs in OpenLayers using M-coordinate timestamps, getCoordinateAtM time scrubbing and nearest-track snapping.

Three flight tracks in shades of blue over OpenStreetMap north of Kraków's Old Town, a red marker on each and a red snap line to the nearest track, with the pilot and the time below the map.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

North of Kraków’s Old Town over OpenStreetMap, three recorded glider/paraglider IGC flight tracks appear in distinct shades of blue. Moving the pointer snaps a red indicator line to the closest point on the nearest flight path—reporting the pilot’s name and the interpolated time at the closest point—while dragging the time slider moves red aircraft markers synchronously along all three trajectories.

When you need it

Soaring competitions, drone-flight debriefings, maritime race replays and multi-vehicle telemetry tools analyse International Gliding Commission (.igc) or time-stamped GPS logs to see where multiple craft were at the exact same second.

Encoding fix timestamps directly inside OpenLayers’ fourth geometry ordinate (M) enables instant linear time interpolation without external array binary searches.

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.

igc.ts
import { ChangeDetectionStrategy, Component, signal, viewChild } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import Feature, { type FeatureLike } from 'ol/Feature';
import type MapBrowserEvent from 'ol/MapBrowserEvent';
import IGC from 'ol/format/IGC';
import LineString from 'ol/geom/LineString';
import Point from 'ol/geom/Point';
import type { FeatureLoader } from 'ol/featureloader';
import { getVectorContext } from 'ol/render';
import type RenderEvent from 'ol/render/Event';
import CircleStyle from 'ol/style/Circle';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import Style from 'ol/style/Style';

const FLIGHTS = [
  'assets/samples/sample-flight-a.igc',
  'assets/samples/sample-flight-b.igc',
  'assets/samples/sample-flight-c.igc',
];

const COLORS: Record<string, string> = {
  'Sample pilot A': 'rgba(0, 0, 255, 0.7)',
  'Sample pilot B': 'rgba(0, 165, 255, 0.7)',
  'Sample pilot C': 'rgba(0, 255, 255, 0.7)',
};

const styleCache: Record<string, Style> = {};

const snapStroke = new Stroke({ color: 'rgba(255,0,0,0.9)', width: 1 });
const snapStyle = new Style({
  stroke: snapStroke,
  image: new CircleStyle({ radius: 5, fill: undefined, stroke: snapStroke }),
});

/** The closest point of a flight and the line to it from the pointer, drawn over the tracks. */
interface Snap {
  point: Point;
  line: LineString;
}

/**
 * Three sample flights over the north of Kraków read from IGC files: the pointer snaps to the closest track and a
 * slider moves a marker along each flight.
 */
@Component({
  selector: 'app-igc',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px" (pointerMove)="snap($event)" (olClick)="snap($event)">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="12"><aol-coordinate [x]="19.9182" [y]="50.1011" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-vector [style]="trackStyle" [postrender]="drawSnap">
        <aol-source-vector #tracks [loader]="load" />
      </aol-layer-vector>
      <aol-layer-vector [style]="markerStyle"><aol-source-vector #markers /></aol-layer-vector>
    </aol-map>
    <div class="example-controls">
      <label> Time <input type="range" min="0" max="100" value="0" (input)="move(number($event))" /> </label>
      <span>
        Closest flight <output>{{ info() }}</output>
      </span>
    </div>
  `,
})
export class Igc {
  readonly info = signal('–');
  protected readonly snapped = signal<Snap | null>(null);
  protected readonly trackStyle = (feature: FeatureLike): Style => {
    const color = COLORS[feature.get('PLT')];
    return (styleCache[color] ??= new Style({ stroke: new Stroke({ color, width: 3 }) }));
  };
  protected readonly markerStyle = new Style({
    image: new CircleStyle({ radius: 5, fill: new Fill({ color: 'rgba(255,0,0,0.9)' }) }),
  });
  /** Reads the three flight files; the source adds the features the promise resolves to. */
  protected readonly load: FeatureLoader<Feature> = async (_extent, _resolution, projection) => {
    const format = new IGC();
    const texts = await Promise.all(FLIGHTS.map(async (url) => (await fetch(url)).text()));
    return texts.flatMap((text) => format.readFeatures(text, { featureProjection: projection }));
  };
  protected readonly drawSnap = (event: RenderEvent): void => {
    const snap = this.snapped();
    if (!snap) return;
    const context = getVectorContext(event);
    context.setStyle(snapStyle);
    context.drawGeometry(snap.point);
    context.drawGeometry(snap.line);
  };
  private readonly map = viewChild.required(MapComponent);
  private readonly tracks = viewChild.required<SourceVectorComponent>('tracks');
  private readonly markers = viewChild.required<SourceVectorComponent>('markers');

  protected number(event: Event): number {
    return (event.target as HTMLInputElement).valueAsNumber;
  }

  protected snap(event: MapBrowserEvent): void {
    if (event.dragging) return;
    const source = this.tracks().instance;
    const closest = source.getClosestFeatureToCoordinate(event.coordinate);
    if (!closest) {
      this.snapped.set(null);
      this.info.set('–');
    } else {
      const closestPoint = (closest.getGeometry() as LineString).getClosestPoint(event.coordinate);
      this.snapped.set({
        point: new Point(closestPoint),
        line: new LineString([event.coordinate, [closestPoint[0], closestPoint[1]]]),
      });
      this.info.set(`${closest.get('PLT')} (${new Date(closestPoint[2] * 1000).toUTCString()})`);
    }
    this.map().instance.render();
  }

  /** Puts a marker on every flight at `percent` of the time from the first take-off to the last landing. */
  protected move(percent: number): void {
    const flights = this.tracks().instance.getFeatures();
    const lines = flights.map((flight) => flight.getGeometry() as LineString);
    const start = Math.min(...lines.map((line) => line.getFirstCoordinate()[2]));
    const stop = Math.max(...lines.map((line) => line.getLastCoordinate()[2]));
    const moment = start + ((stop - start) * percent) / 100;
    const overlay = this.markers().instance;
    flights.forEach((flight, index) => {
      const coordinate = lines[index].getCoordinateAtM(moment, true);
      const marker = flight.get('highlight') as Feature<Point> | undefined;
      if (marker) {
        marker.getGeometry()?.setCoordinates(coordinate);
      } else {
        const created = new Feature(new Point(coordinate));
        flight.set('highlight', created);
        overlay.addFeature(created);
      }
    });
    this.map().instance.render();
  }
}

Architecture and OpenLayers mechanics

An asynchronous source loader fetches all three .igc files in parallel. OpenLayers’ IGC parser stores each fix’s UTC epoch seconds inside the line’s M coordinate and extracts the header’s pilot name into the PLT property used for colour coding.

On pointer motion, getClosestFeatureToCoordinate and getClosestPoint locate the closest point on the nearest track, interpolating its time—drawing a transient snap vector in postrender via getVectorContext and render()—while scrubbing the time slider calls getCoordinateAtM(moment, true) to interpolate each flight’s position clamped cleanly between take-off and landing.

How it works

  • A custom promise-based loader fetches all three IGC logs and adds the decoded flight features to the vector source.
  • IGC stores fix timestamps (seconds) in the line’s M ordinate and exposes the header pilot name under PLT for per-pilot styling.
  • getClosestFeatureToCoordinate and getClosestPoint resolve the nearest trajectory point and read back its M timestamp under the cursor.
  • getCoordinateAtM(moment, true) interpolates each aircraft marker along its track for the slider’s timestamp, clamping before take-off and after landing.
  • Transient snap lines paint cleanly inside postrender using getVectorContext triggered by render().

Pitfalls

  • By default new IGC() omits Z altitude coordinates (XYM layout); pass an explicit altitudeMode (barometric or gps) if your analysis also needs elevation profiles in XYZ / XYZM.
  • getClosestFeatureToCoordinate searches across the entire source without a maximum radius—add a pixel distance check if you only want snapping when the cursor is close to a track.

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.