Vector data · Angular, React and OpenLayers
A polygon from WKT
Parse a geographic Well-Known Text (WKT) polygon in OpenLayers with readGeometry and reproject its degrees from EPSG:4326 into Web Mercator.

What this example shows
Spanning the corridor from west of Kraków almost to Tarnów at zoom 9, a regional polygon parsed directly from an OGC Well-Known Text string (POLYGON((...))) renders in OpenLayers’ crisp blue outline over OpenStreetMap.
When you need it
PostGIS (ST_AsText), Oracle Spatial, DuckDB Spatial, JSTS/GEOS geometry engines and REST search filters routinely exchange geometries as compact WKT strings.
Parsing WKT directly in the browser lets admin forms, SQL query consoles and debugging tools preview database geometries immediately without adding a GeoJSON serialisation step on the backend.
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.
wkt.tsimport { ChangeDetectionStrategy, Component } from '@angular/core';
import {
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
} from 'cc-openlayers';
import WKT from 'ol/format/WKT';
/** A polygon read from WKT text in geographic coordinates and shown in the map's Web Mercator. */
@Component({
selector: 'app-wkt',
imports: [
CoordinateComponent,
DefaultControlComponent,
DefaultInteractionComponent,
FeatureComponent,
LayerTileComponent,
LayerVectorComponent,
MapComponent,
SourceOsmComponent,
SourceVectorComponent,
ViewComponent,
],
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<aol-map height="480px">
<aol-interaction-default />
<aol-control-defaults />
<aol-view [zoom]="9"><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 [geometry]="geometry" />
</aol-source-vector>
</aol-layer-vector>
</aol-map>
`,
})
export class Wkt {
protected readonly geometry = new WKT().readGeometry(
'POLYGON((19.2 49.95, 20.6 50.2, 20.9 49.75, 19.5 49.65, 19.2 49.95))',
{ dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }
);
} 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.
wkt.react.tsximport WKT from 'ol/format/WKT.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Feature,
Map,
OSMSource,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
const geometry = new WKT().readGeometry('POLYGON((19.2 49.95, 20.6 50.2, 20.9 49.75, 19.5 49.65, 19.2 49.95))', {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857',
});
/** A polygon read from WKT text in geographic coordinates and shown in the map's Web Mercator. */
export function Wkt() {
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={9}>
<Coordinate x={19.9368} y={50.0614} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource>
<Feature geometry={geometry} />
</VectorSource>
</VectorLayer>
</Map>
);
}How the React version works
A polygon read from WKT text in degrees and shown in Web Mercator.
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-feature [geometry]> | <Feature geometry> with a geometry read once in the module |
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
OpenLayers’ WKT format readGeometry method parses the coordinate text and applies an immediate coordinate transformation when given { dataProjection: 'EPSG:4326', featureProjection: 'EPSG:3857' }—handing the resulting projected Polygon directly to aol-feature via [geometry].
How it works
readGeometryparses the WKT string into an OpenLayers Polygon bound directly toaol-featurevia[geometry].dataProjection: 'EPSG:4326'declares that input vertices are in WGS84 degrees whilefeatureProjection: 'EPSG:3857'projects them into Web Mercator.- The sample polygon covers 19.2°–20.9° E and 49.65°–50.2° N across Lesser Poland.
Pitfalls
- Standard WKT carries no embedded CRS identifier: omitting
dataProjectioncauses OpenLayers to treat 19.2, 49.65 as raw Web Mercator metres—shrinking a regional polygon to a 1.7-metre speck at 0°, 0° off West Africa. - WKT always orders coordinates X Y (longitude latitude): pasting latitude longitude pairs copied from consumer GPS tools flips the polygon across the globe.
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.