Interactions · Angular, React and OpenLayers
Edit a polygon and read its GeoJSON
Select an OpenLayers polygon on click, drag its vertices or edges to reshape it with Modify, and serialize updated WGS84 GeoJSON into state.

What this example shows
Over central Kraków, clicking the polygon framing the Old Town highlights it for editing; dragging any corner vertex or midpoint segment stretches the boundary in real time and immediately updates the exported EPSG:4326 GeoJSON readout below the map when released.
When you need it
Cadastral boundary adjustments, zoning amendments, farm-field editors and geofence management screens load an existing polygon from an API, let the user tweak individual corners, and save updated GeoJSON back to the server.
Scoping vertex editing strictly to currently selected features prevents accidental drags on neighbouring parcels while panning.
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.
modify-polygon.tsimport { JsonPipe } from '@angular/common';
import { ChangeDetectionStrategy, Component, signal } from '@angular/core';
import type { Feature as GeoJsonFeature, Polygon as GeoJsonPolygon } from 'geojson';
import type Feature from 'ol/Feature';
import GeoJSON from 'ol/format/GeoJSON';
import Polygon from 'ol/geom/Polygon';
import {
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SelectInteractionComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import { ModifyInteractionComponent } from 'cc-openlayers/editing';
/** A polygon selected and edited with the select and modify interactions, shown as geographic GeoJSON. */
@Component({
selector: 'app-modify-polygon',
imports: [
CollectionCoordinatesComponent,
CoordinateComponent,
DefaultInteractionComponent,
FeatureComponent,
GeometryPolygonComponent,
JsonPipe,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
ModifyInteractionComponent,
SelectInteractionComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-interaction-select #select (olSelect)="selected.set(select.instance.getFeatures().getLength())" />
<aol-interaction-modify
[features]="select.instance.getFeatures()"
(olModifyEnd)="modifyEnd($event.features.item(0))"
/>
<aol-view [zoom]="14">
<aol-coordinate [x]="19.9368" [y]="50.0614" srid="EPSG:4326" />
</aol-view>
<aol-layer-tile><aol-source-osm /></aol-layer-tile>
<aol-layer-vector>
<aol-source-vector>
<aol-feature>
<aol-geometry-polygon>
<aol-collection-coordinates [coordinates]="polygon().geometry.coordinates" srid="EPSG:4326" />
</aol-geometry-polygon>
</aol-feature>
</aol-source-vector>
</aol-layer-vector>
</aol-map>
<div class="example-controls">
<p class="example-hint">Click the polygon to select it, then drag a vertex to edit its geometry.</p>
<span>
Selected features <output>{{ selected() }}</output>
</span>
</div>
<pre>{{ polygon() | json }}</pre>
`,
})
export class ModifyPolygon {
readonly selected = signal(0);
readonly polygon = signal<GeoJsonFeature<GeoJsonPolygon>>({
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[19.928, 50.057],
[19.945, 50.057],
[19.945, 50.066],
[19.928, 50.066],
[19.928, 50.057],
],
],
},
});
private readonly format = new GeoJSON({ dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });
protected modifyEnd(feature: Feature | undefined): void {
if (!(feature?.getGeometry() instanceof Polygon)) return;
this.polygon.set(this.format.writeFeatureObject(feature) as GeoJsonFeature<GeoJsonPolygon>);
}
} 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.
modify-polygon.react.tsximport { useState } from 'react';
import type { Feature as GeoJsonFeature, Polygon as GeoJsonPolygon } from 'geojson';
import Collection from 'ol/Collection.js';
import type OLFeature from 'ol/Feature.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import Polygon from 'ol/geom/Polygon.js';
import {
CollectionCoordinates,
Coordinate,
DefaultInteractions,
Feature,
Map,
ModifyInteraction,
OSMSource,
PolygonGeometry,
SelectInteraction,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const format = new GeoJSON({ dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' });
const initialPolygon: GeoJsonFeature<GeoJsonPolygon> = {
type: 'Feature',
properties: {},
geometry: {
type: 'Polygon',
coordinates: [
[
[19.928, 50.057],
[19.945, 50.057],
[19.945, 50.066],
[19.928, 50.066],
[19.928, 50.057],
],
],
},
};
/** A polygon selected and edited with the select and modify interactions, shown as geographic GeoJSON. */
export function ModifyPolygon() {
const [selected, setSelected] = useState(0);
const [polygon, setPolygon] = useState(initialPolygon);
// The selection the select interaction fills and the modify interaction edits.
const [features] = useState(() => new Collection<OLFeature>());
const modifyEnd = (feature: OLFeature | undefined) => {
if (!(feature?.getGeometry() instanceof Polygon)) return;
setPolygon(format.writeFeatureObject(feature) as GeoJsonFeature<GeoJsonPolygon>);
};
return (
<>
<Map height="480px">
<DefaultInteractions />
<SelectInteraction features={features} onSelect={() => setSelected(features.getLength())} />
<ModifyInteraction features={features} onModifyEnd={(event) => modifyEnd(event.features.item(0))} />
<View zoom={14}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource>
<Feature>
<PolygonGeometry>
<CollectionCoordinates coordinates={polygon.geometry.coordinates} srid="EPSG:4326" />
</PolygonGeometry>
</Feature>
</VectorSource>
</VectorLayer>
</Map>
<div className="example-controls">
<p className="example-hint">Click the polygon to select it, then drag a vertex to edit its geometry.</p>
<span>
Selected features <output>{selected}</output>
</span>
</div>
<pre>{JSON.stringify(polygon, null, 2)}</pre>
</>
);
}How the React version works
A polygon with holes selected and edited with the select and modify interactions; the edited polygon is shown as geographic GeoJSON with its properties, and the number of selected features is counted.
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.
From Angular to React
| Angular | React |
|---|---|
<aol-interaction-select (olSelect)> and <aol-interaction-modify [features]> from the select's features | <SelectInteraction features onSelect> and <ModifyInteraction features onModifyEnd> sharing one Collection created once in useState |
the GeoJSON and the count in signals | useState |
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
aol-interaction-select tracks clicked polygons and passes its live feature collection into aol-interaction-modify via [features], updating the selected-feature counter on each olSelect event.
Whenever a vertex drag finishes, olModifyEnd serialises the modified feature through OpenLayers’ GeoJSON format—reprojecting coordinates from Web Mercator back into EPSG:4326 degrees—and stores the JSON inside the polygon signal.
How it works
aol-interaction-selectmanages feature selection and feeds its selected collection directly intoaol-interaction-modifyvia[features].olSelectkeeps the UI’s selected-feature count synchronized as polygons are clicked or deselected.- On
olModifyEnd, OpenLayers’ GeoJSON writer exports the edited feature inEPSG:4326degrees into thepolygonsignal. - Type guards ignore non-polygon geometries so serialized state remains valid.
Pitfalls
- An
aol-interaction-modifyinstance without either [source] or[features]bound has nothing to edit—pass the Select interaction’s feature collection when only selected shapes should be editable. - When exporting edited polygons, serialise the entire
Polygongeometry (outer shell plus any interior hole rings) rather than reading getCoordinates()[0] alone.
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.