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().

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.tsimport { 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;
}
} 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.
edit-geographic.react.tsximport { useEffect, useState } from 'react';
import type OLSelect from 'ol/interaction/Select.js';
import { clearUserProjection, useGeographic as setGeographicUserProjection } from 'ol/proj.js';
import type OLVectorSource from 'ol/source/Vector.js';
import {
DefaultControls,
DefaultInteractions,
DrawInteraction,
GeoJSONSource,
Map,
ModifyInteraction,
SelectInteraction,
SnapInteraction,
VectorLayer,
View,
} from 'cc-openlayers/react';
type Mode = 'modify' | 'draw';
/** Kraków in longitude and latitude: the view's centre is a user coordinate. */
const CENTER = [19.9368, 50.0614];
/**
* 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.
*/
export function EditGeographic() {
const [mode, setMode] = useState<Mode>('modify');
// The source and the selection are made after the first commit, so state carries them to the interactions.
const [source, setSource] = useState<OLVectorSource | null>(null);
const [select, setSelect] = useState<OLSelect | null>(null);
// The view and the source read the user projection as they mount, before any passive effect runs; StrictMode clears
// it with the first effect's cleanup, so the effect sets it again.
useState(setGeographicUserProjection);
useEffect(() => {
setGeographicUserProjection();
return clearUserProjection;
}, []);
return (
<>
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={7} center={CENTER} />
<VectorLayer background="white">
<GeoJSONSource ref={setSource} url="assets/poland/voivodeships.geojson" />
</VectorLayer>
{mode === 'modify' && (
<>
<SelectInteraction ref={setSelect} />
{select && <ModifyInteraction features={select.getFeatures()} />}
{source && <SnapInteraction source={source} />}
</>
)}
{mode === 'draw' && source && (
<>
<DrawInteraction type="Polygon" source={source} />
<SnapInteraction source={source} />
</>
)}
</Map>
<div className="example-controls">
<label>
Mode
<select value={mode} onChange={(event) => setMode(event.target.value as Mode)}>
<option value="modify">select a feature to modify</option>
<option value="draw">draw new features</option>
</select>
</label>
</div>
</>
);
}How the React version works
Poland's voivodeships modified or new polygons drawn with snapping, in longitude and latitude through useGeographic, while the map stays in Web Mercator; a list switches between modifying and drawing.
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 example has no shared file.
From Angular to React
| Angular | React |
|---|---|
useGeographic() in the constructor and clearUserProjection on destroy | useState(setGeographicUserProjection) and a useEffect that sets it again and returns clearUserProjection |
signal of the mode and @switch (mode()) | useState and mode === … conditions |
template references #source and #select read as .instance | ref={setSource} and ref={setSelect} into useState, so the interactions render once the objects exist |
<aol-interaction-modify [features]="select.instance.getFeatures()"> | <ModifyInteraction features={select.getFeatures()} /> |
<select (change)="mode.set(…)"> | <select value={mode} onChange={…}> |
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. useGeographic becomes setGeographicUserProjection, called in a useState initialiser, because the view and the geometries read the user projection while they mount, before any effect runs. StrictMode runs the effect's cleanup (clearUserProjection) once at the start, so the effect sets the projection again; the cleanup also restores it on unmount, as DestroyRef.onDestroy does in Angular. The source and the selection exist only after the first commit, so state, not a ref, carries them to the interactions.
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()keepsassets/poland/voivodeships.geojsonand all newly drawn or edited vertices in decimal degrees while the View stays in Web Mercator. @switchonmodetoggles cleanly between'modify'(<aol-interaction-modify [features]="select.instance.getFeatures()" />) and'draw'(<aol-interaction-draw type="Polygon" [source]="source.instance" />).- Mounting
aol-interaction-snapafter 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 touseGeographic()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.
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.