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.

A map of Kraków with the selected Old Town polygon whose north-eastern corner has been dragged, and its GeoJSON below the map.
A screenshot of the example. Map data © OpenStreetMap contributors.

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.ts
import { 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>);
  }
}

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-select manages feature selection and feeds its selected collection directly into aol-interaction-modify via [features].
  • olSelect keeps the UI’s selected-feature count synchronized as polygons are clicked or deselected.
  • On olModifyEnd, OpenLayers’ GeoJSON writer exports the edited feature in EPSG:4326 degrees into the polygon signal.
  • Type guards ignore non-polygon geometries so serialized state remains valid.

Pitfalls

  • An aol-interaction-modify instance 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 Polygon geometry (outer shell plus any interior hole rings) rather than reading getCoordinates()[0] alone.

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.