Interactions · Angular, React and OpenLayers

Trace the edges of Kraków's parks

Trace exact park and Planty polygon boundaries around Kraków’s Old Town in OpenLayers with two clicks using Draw trace and traceSource options.

An aerial view of central Kraków with its parks in red and a green polygon whose western side traces the outer edge of the Planty from its north to its south-east.
A screenshot of the example. Map data © OpenStreetMap contributors. Imagery: Esri World Imagery (Esri, Maxar, Earthstar Geographics and the GIS User Community).

What this example shows

Over GPU-rendered Esri World Imagery of central Kraków, city parks including the sinuous Planty ring appear outlined in red. When drawing a new green polygon or line, clicking once on any park boundary starts live edge-following—highlighting and copying every intricate curve of the park border as you move the cursor—until a second click along that boundary locks the traced stretch in place.

When you need it

Digitising a planning zone, mowing contract area or conservation buffer that borders a winding river, forest edge or road reserve by hand takes dozens of tedious clicks and still leaves topological slivers.

Edge tracing copies exact source coordinates vertex-for-vertex between any two clicks along an existing line or polygon ring.

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.

tracing.ts
import { ChangeDetectionStrategy, Component, computed, signal } from '@angular/core';
import GeoJSON from 'ol/format/GeoJSON';
import type { FlatStyle } from 'ol/style/flat';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerVectorComponent,
  MapComponent,
  SourceImageTileComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent, SnapInteractionComponent } from 'cc-openlayers/editing';
import { LayerWebGLTileComponent } from 'cc-openlayers/webgl';

type GeometryType = 'Polygon' | 'LineString' | 'None';

/**
 * Shapes drawn along the edges of Kraków's parks around the Old Town, the Planty ring among them, over Esri's World
 * Imagery: a click on an edge starts tracing it, the pointer snaps to the edges, and a second click on the same edge
 * stops tracing.
 */
@Component({
  selector: 'app-tracing',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    DrawInteractionComponent,
    LayerVectorComponent,
    LayerWebGLTileComponent,
    MapComponent,
    SnapInteractionComponent,
    SourceImageTileComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="14">
        <aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-webgltile>
        <aol-source-imagetile
          url="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"
          [maxZoom]="19"
          attributions="Tiles © Esri — Source: Esri, Maxar, Earthstar Geographics, and the GIS User Community"
        />
      </aol-layer-webgltile>
      <!-- features in this layer will be snapped to -->
      <aol-layer-vector [style]="baseStyle">
        <aol-source-vector
          #base
          url="assets/krakow/parks.geojson"
          [format]="format"
          attributions='Parks © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
        />
      </aol-layer-vector>
      <!-- this is where the drawn features go -->
      <aol-layer-vector [style]="drawStyle"><aol-source-vector #drawn /></aol-layer-vector>
      @if (drawType(); as type) {
        <aol-interaction-draw
          [type]="type"
          [source]="drawn.instance"
          [trace]="true"
          [traceSource]="base.instance"
          [style]="sketchStyle"
        />
        <aol-interaction-snap [source]="base.instance" />
      }
    </aol-map>
    <div class="example-controls">
      <label>
        Geometry type
        <select name="type" (change)="choose($event)">
          @for (type of types; track type) {
            <option [value]="type" [selected]="type === geometryType()">{{ type }}</option>
          }
        </select>
      </label>
    </div>
  `,
})
export class Tracing {
  readonly geometryType = signal<GeometryType>('Polygon');
  /** The geometry type drawn; none when drawing is off. */
  protected readonly drawType = computed(() => {
    const type = this.geometryType();
    return type === 'None' ? undefined : type;
  });
  protected readonly types: GeometryType[] = ['Polygon', 'LineString', 'None'];
  protected readonly format = new GeoJSON();
  protected readonly baseStyle: FlatStyle = {
    'fill-color': 'rgba(255, 0, 0, 0.3)',
    'stroke-color': 'rgba(255, 0, 0, 0.9)',
    'stroke-width': 0.5,
  };
  protected readonly drawStyle: FlatStyle = {
    'stroke-color': 'rgba(100, 255, 0, 1)',
    'stroke-width': 2,
    'fill-color': 'rgba(100, 255, 0, 0.3)',
  };
  protected readonly sketchStyle: FlatStyle = {
    'stroke-color': 'rgba(255, 255, 100, 0.5)',
    'stroke-width': 1.5,
    'fill-color': 'rgba(255, 255, 100, 0.25)',
    'circle-radius': 6,
    'circle-fill-color': 'rgba(255, 255, 100, 0.5)',
  };

  protected choose(event: Event): void {
    this.geometryType.set((event.target as HTMLSelectElement).value as GeometryType);
  }
}

Architecture and OpenLayers mechanics

Reference park polygons load from GeoJSON into a dedicated aol-source-vector (format) while user-sketched shapes write into a separate second vector source. Setting [trace]="true" and pointing [traceSource] at the parks source tells aol-interaction-draw to enter boundary-following mode on the first snapped edge click and splice the shortest perimeter path on the second click.

A companion Snap interaction pulls the cursor onto park edges cleanly over an aol-layer-webgltile + aol-source-imagetile aerial backdrop.

How it works

  • Enabling [trace]="true" with [traceSource] lets two clicks along any park boundary copy every intermediate vertex automatically.
  • Reference park GeoJSON lives in its own aol-source-vector (format) while newly drawn shapes stay cleanly separated in a second source.
  • Pairing Snap with Draw ensures clicks land accurately on park perimeter rings.
  • Esri World Imagery renders underneath via aol-layer-webgltile and aol-source-imagetile with maxZoom 19 overzooming.

Pitfalls

  • If you set [trace]="true" but forget [traceSource], OpenLayers defaults to tracing features inside Draw’s own output source—ignoring your reference park or parcel layer completely.
  • Capping imagery source maxZoom at 19 lets street-level tracing at zoom 20+ smoothly upscale level-19 tiles rather than requesting missing imagery.

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.