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.

A polygon in the default blue outline over OpenStreetMap at zoom 9, from west of Kraków almost to Tarnów.
A screenshot of the example. Map data © OpenStreetMap contributors.

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

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

  • readGeometry parses the WKT string into an OpenLayers Polygon bound directly to aol-feature via [geometry].
  • dataProjection: 'EPSG:4326' declares that input vertices are in WGS84 degrees while featureProjection: '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 dataProjection causes 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.

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.