Projections · Angular, React and OpenLayers

Editing in geographic coordinates

Modify Polish voivodeship polygons and draw snapped shapes directly in WGS84 degrees on a Web Mercator map using OpenLayers useGeographic().

The voivodeship borders of southern Poland as blue outlines on white, from Lower Silesia in the west to Lublin and Subcarpathia in the east, with the Mode select set to modify below the map.
A screenshot of the example. Voivodeships: Państwowy Rejestr Granic © GUGiK.

What this example shows

Against a crisp white background="white" canvas with no base layer, southern and central Poland’s voivodeship borders load from GeoJSON for interactive editing: a Mode selector switches between selecting and dragging existing provincial vertices (modify) and sketching new polygons (draw), with edge-and-corner snapping active in both modes—all stored natively in [lon, lat] degrees.

When you need it

Applications backed by WGS 84 REST APIs or GeoJSON databases can eliminate error-prone projection round-trips on load and save by keeping vector features in decimal degrees throughout Select, Modify, Draw and Snap workflows.

Meanwhile, keeping the underlying OpenLayers View in Spherical Mercator preserves familiar north-up conformal geometry on screen.

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.

edit-geographic.ts
import { ChangeDetectionStrategy, Component, DestroyRef, inject, signal } from '@angular/core';
import {
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerVectorComponent,
  MapComponent,
  SelectInteractionComponent,
  SourceGeoJSONComponent,
  ViewComponent,
} from 'cc-openlayers';
import { DrawInteractionComponent, ModifyInteractionComponent, SnapInteractionComponent } from 'cc-openlayers/editing';
import { clearUserProjection, useGeographic } from 'ol/proj';

type Mode = 'modify' | 'draw';

/**
 * Voivodeships edited in geographic coordinates: `useGeographic()` makes the view, the events and the
 * interactions speak longitude and latitude while the map stays in Web Mercator. Modify or draw polygons.
 */
@Component({
  selector: 'app-edit-geographic',
  imports: [
    DefaultControlComponent,
    DefaultInteractionComponent,
    DrawInteractionComponent,
    LayerVectorComponent,
    MapComponent,
    ModifyInteractionComponent,
    SelectInteractionComponent,
    SnapInteractionComponent,
    SourceGeoJSONComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="7" [center]="center" />
      <aol-layer-vector background="white">
        <aol-source-geojson #source url="assets/poland/voivodeships.geojson" />
      </aol-layer-vector>
      @switch (mode()) {
        @case ('modify') {
          <aol-interaction-select #select />
          <aol-interaction-modify [features]="select.instance.getFeatures()" />
          <aol-interaction-snap [source]="source.instance" />
        }
        @case ('draw') {
          <aol-interaction-draw type="Polygon" [source]="source.instance" />
          <aol-interaction-snap [source]="source.instance" />
        }
      }
    </aol-map>
    <div class="example-controls">
      <label>
        Mode
        <select (change)="mode.set(value($event))">
          <option value="modify" [selected]="mode() === 'modify'">select a feature to modify</option>
          <option value="draw" [selected]="mode() === 'draw'">draw new features</option>
        </select>
      </label>
    </div>
  `,
})
export class EditGeographic {
  readonly mode = signal<Mode>('modify');
  /** Kraków in longitude and latitude: the view's centre is a user coordinate. */
  protected readonly center = [19.9368, 50.0614];

  constructor() {
    useGeographic();
    inject(DestroyRef).onDestroy(clearUserProjection);
  }

  protected value(event: Event): Mode {
    return (event.target as HTMLSelectElement).value as Mode;
  }
}

Architecture and OpenLayers mechanics

Calling useGeographic() inside the constructor before loading assets/poland/voivodeships.geojson ensures the vector source stores parsed rings directly in degrees while the Web Mercator view projects them solely during rendering.

An @switch on the mode signal mounts either Select + <aol-interaction-modify [features]="select.instance.getFeatures()" /> ('modify') or <aol-interaction-draw type="Polygon" [source]="source.instance" /> ('draw'), followed in both branches by aol-interaction-snap.

How it works

  • Constructor useGeographic() keeps assets/poland/voivodeships.geojson and all newly drawn or edited vertices in decimal degrees while the View stays in Web Mercator.
  • @switch on mode toggles cleanly between 'modify' (<aol-interaction-modify [features]="select.instance.getFeatures()" />) and 'draw' (<aol-interaction-draw type="Polygon" [source]="source.instance" />).
  • Mounting aol-interaction-snap after Modify and Draw enables vertex and edge snapping in degree space.
  • background="white" provides a clean backdrop without needing a raster basemap.

Pitfalls

  • Always invoke useGeographic() before reading features into a source: features already parsed into Web Mercator metres prior to useGeographic() will be misinterpreted as huge degree numbers on the next render.
  • When computing spherical lengths or areas on degree-stored geometries via ol/sphere, pass an explicit EPSG:4326 projection option because getArea and getLength default to assuming EPSG:3857 input.

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.