Vector data · Angular, React and OpenLayers

A polygon from WKB

Decode hexadecimal Well-Known Binary (WKB) geometries from spatial databases in OpenLayers using readGeometry and explicit projection options.

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

Over OpenStreetMap at zoom 9 between Kraków and Tarnów, the exact same Lesser Poland polygon renders directly from a compact hexadecimal Well-Known Binary (WKB_POLYGON) payload—matching the raw binary representation stored inside PostGIS tables.

When you need it

High-throughput spatial backends and GraphQL/JSON APIs frequently return raw hexadecimal WKB (or binary ArrayBuffer streams) straight from database cursors to avoid CPU-heavy text formatting on the database server.

Decoding WKB natively on the client cuts server serialisation overhead and avoids floating-point rounding drift.

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.

wkb.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  FeatureComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import WKB from 'ol/format/WKB';

const WKB_POLYGON =
  '0103000000010000000500000033333333333333409A99999999F948409A999999999934409A999999991949406666666666E634400000000000E0484000000000008033403333333333D3484033333333333333409A99999999F94840';

/** A polygon read from hexadecimal WKB in geographic coordinates and shown in the map's Web Mercator. */
@Component({
  selector: 'app-wkb',
  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 Wkb {
  protected readonly geometry = new WKB().readGeometry(WKB_POLYGON, {
    dataProjection: 'EPSG:4326',
    featureProjection: 'EPSG:3857',
  });
}

Architecture and OpenLayers mechanics

WKB_POLYGON encodes byte-order (01 for little-endian), geometry type (3 for Polygon) and IEEE-754 double-precision vertex rings in hex. OpenLayers’ WKB readGeometry accepts hex strings, ArrayBuffers or Uint8Array views alike, reprojecting vertices from dataProjection into featureProjection in a single pass.

How it works

  • WKB_POLYGON represents a little-endian (01) Polygon (type 3) with one closed five-vertex ring in hexadecimal.
  • readGeometry decodes hexadecimal strings as well as binary array buffers and typed arrays directly.
  • dataProjection and featureProjection transform decoded degree coordinates into the map’s Web Mercator projection.

Pitfalls

  • Standard ISO WKB contains no SRID header, making explicit dataProjection mandatory; PostGIS Extended WKB (EWKB) embeds an SRID flag that OpenLayers can auto-detect only when dataProjection is omitted and that EPSG code is registered.
  • Hex-encoded strings double the byte length of raw binary WKB—prefer binary ArrayBuffer transport or simplified geometries when transferring thousands of complex polygons.

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.