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.

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.tsimport { 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);
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
tracing.react.tsximport { useState } from 'react';
import GeoJSON from 'ol/format/GeoJSON.js';
import type OLVectorSource from 'ol/source/Vector.js';
import type { FlatStyle } from 'ol/style/flat.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
DrawInteraction,
ImageTileSource,
Map,
SnapInteraction,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
import { WebGLTileLayer } from 'cc-openlayers/react/webgl';
type GeometryType = 'Polygon' | 'LineString' | 'None';
const types: GeometryType[] = ['Polygon', 'LineString', 'None'];
const format = new GeoJSON();
const baseStyle: FlatStyle = {
'fill-color': 'rgba(255, 0, 0, 0.3)',
'stroke-color': 'rgba(255, 0, 0, 0.9)',
'stroke-width': 0.5,
};
const drawStyle: FlatStyle = {
'stroke-color': 'rgba(100, 255, 0, 1)',
'stroke-width': 2,
'fill-color': 'rgba(100, 255, 0, 0.3)',
};
const 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)',
};
/**
* 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.
*/
export function Tracing() {
const [geometryType, setGeometryType] = useState<GeometryType>('Polygon');
const [base, setBase] = useState<OLVectorSource | null>(null);
const [drawn, setDrawn] = useState<OLVectorSource | null>(null);
/** The geometry type drawn; none when drawing is off. */
const drawType = geometryType === 'None' ? undefined : geometryType;
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={14}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<WebGLTileLayer>
<ImageTileSource
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"
/>
</WebGLTileLayer>
{/* features in this layer will be snapped to */}
<VectorLayer style={baseStyle}>
<VectorSource
ref={setBase}
url="assets/krakow/parks.geojson"
format={format}
attributions='Parks © <a href="https://www.openstreetmap.org/copyright" target="_blank">OpenStreetMap</a> contributors'
/>
</VectorLayer>
{/* this is where the drawn features go */}
<VectorLayer style={drawStyle}>
<VectorSource ref={setDrawn} />
</VectorLayer>
{drawType && base && drawn ? (
<>
<DrawInteraction type={drawType} source={drawn} trace={true} traceSource={base} style={sketchStyle} />
<SnapInteraction source={base} />
</>
) : null}
</Map>
<div className="example-controls">
<label>
Geometry type
<select
name="type"
value={geometryType}
onChange={(event) => setGeometryType(event.target.value as GeometryType)}
>
{types.map((type) => (
<option key={type} value={type}>
{type}
</option>
))}
</select>
</label>
</div>
</>
);
}How the React version works
Shapes drawn along the edges of the parks around the Old Town (the Planty among them) over Esri's World Imagery: a click on an edge starts tracing, the pointer snaps to the edges, and a second click on the same edge stops.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection. The snap interaction is rendered after the draw and modify interactions in both versions, because OpenLayers lets the last added interaction handle a pointer event first.
From Angular to React
| Angular | React |
|---|---|
the type <select>'s (change) and a computed | onChange and useState |
viewChild of the parks and the drawn sources | ref={setBase} and ref={setDrawn} into useState |
@if with <aol-interaction-draw [trace] [traceSource]> and the snap | <DrawInteraction key={type} trace traceSource> and <SnapInteraction>, nothing for None |
<aol-layer-webgltile> | <WebGLTileLayer> from cc-openlayers/react/webgl |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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-webgltileandaol-source-imagetilewithmaxZoom19 overzooming.
Pitfalls
- If you set
[trace]="true"but forget[traceSource], OpenLayers defaults to tracing features inside Draw’s own outputsource—ignoring your reference park or parcel layer completely. - Capping imagery source
maxZoomat 19 lets street-level tracing at zoom 20+ smoothly upscale level-19 tiles rather than requesting missing imagery.
Related examples
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.