Vector data · Angular, React and OpenLayers

Points, lines, polygons and circles

Declare Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and Circle geometries declaratively in an OpenLayers template.

A map of Kraków and its surroundings with a green point, three black points, blue lines, dark red polygons and a yellow circle.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

Across a street map of Kraków and its surroundings, one feature of every core OpenLayers geometry class renders simultaneously: a green single point, three black multi-points, blue single and multi-part lines, dark-red single and multi-part polygons, and a translucent yellow service-radius circle.

When you need it

Small sets of domain shapes—project boundaries, delivery depots, route corridors and coverage rings—often live directly in component state alongside forms rather than in external GIS files.

Declaring those features directly in markup binds their coordinates and styles to Angular or React component fields without manual imperative Feature construction or cleanup loops.

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.

display-geometry.ts
import { ChangeDetectionStrategy, Component } from '@angular/core';
import {
  CollectionCoordinatesComponent,
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  FeatureComponent,
  GeometryCircleComponent,
  GeometryLinestringComponent,
  GeometryMultiLinestringComponent,
  GeometryMultiPointComponent,
  GeometryMultiPolygonComponent,
  GeometryPointComponent,
  GeometryPolygonComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  StyleCircleComponent,
  StyleComponent,
  StyleFillComponent,
  StyleStrokeComponent,
  ViewComponent,
} from 'cc-openlayers';

/** One feature of every geometry type the library draws, each with its own style or the default one. */
@Component({
  selector: 'app-display-geometry',
  imports: [
    CollectionCoordinatesComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    FeatureComponent,
    GeometryCircleComponent,
    GeometryLinestringComponent,
    GeometryMultiLinestringComponent,
    GeometryMultiPointComponent,
    GeometryMultiPolygonComponent,
    GeometryPointComponent,
    GeometryPolygonComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    StyleCircleComponent,
    StyleComponent,
    StyleFillComponent,
    StyleStrokeComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="11"><aol-coordinate [x]="20.0" [y]="50.055" srid="EPSG:4326" /></aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-vector>
        <aol-source-vector>
          <aol-feature>
            <aol-geometry-point><aol-coordinate [x]="19.9373" [y]="50.0617" srid="EPSG:4326" /></aol-geometry-point>
            <aol-style>
              <aol-style-circle [radius]="10">
                <aol-style-stroke color="black" [width]="5" />
                <aol-style-fill color="green" />
              </aol-style-circle>
            </aol-style>
          </aol-feature>
          <aol-feature>
            <aol-geometry-multipoint>
              <aol-collection-coordinates [coordinates]="multiPoint" srid="EPSG:4326" />
            </aol-geometry-multipoint>
            <aol-style>
              <aol-style-circle [radius]="5">
                <aol-style-stroke color="black" [width]="5" />
                <aol-style-fill color="green" />
              </aol-style-circle>
            </aol-style>
          </aol-feature>
          <aol-feature>
            <aol-geometry-linestring>
              <aol-collection-coordinates [coordinates]="lineString" srid="EPSG:4326" />
            </aol-geometry-linestring>
          </aol-feature>
          <aol-feature>
            <aol-geometry-multilinestring>
              <aol-collection-coordinates [coordinates]="multiLineString" srid="EPSG:4326" />
            </aol-geometry-multilinestring>
          </aol-feature>
          <aol-feature>
            <aol-geometry-polygon>
              <aol-collection-coordinates [coordinates]="polygon" srid="EPSG:4326" />
            </aol-geometry-polygon>
            <aol-style>
              <aol-style-stroke color="rgba(90, 17, 26)" [width]="3" />
              <aol-style-fill color="rgba(90, 17, 26, 0.5)" />
            </aol-style>
          </aol-feature>
          <aol-feature>
            <aol-geometry-multipolygon>
              <aol-collection-coordinates [coordinates]="multiPolygon" srid="EPSG:4326" />
            </aol-geometry-multipolygon>
            <aol-style>
              <aol-style-stroke color="rgba(81, 15, 23)" [width]="2" />
              <aol-style-fill color="rgba(81, 15, 23, 0.4)" />
            </aol-style>
          </aol-feature>
          <aol-feature>
            <aol-geometry-circle [radius]="2000">
              <aol-coordinate [x]="20.0373" [y]="50.0718" srid="EPSG:4326" />
            </aol-geometry-circle>
            <aol-style>
              <aol-style-stroke color="blue" [width]="2" />
              <aol-style-fill color="rgba(255, 255, 0, 0.5)" />
            </aol-style>
          </aol-feature>
        </aol-source-vector>
      </aol-layer-vector>
    </aol-map>
  `,
})
export class DisplayGeometry {
  readonly multiPoint = [
    [20.065, 49.987],
    [20.217, 50.035],
    [19.828, 49.975],
  ];
  readonly lineString = [
    [19.85, 50.04],
    [19.9, 50.05],
    [19.935, 50.05],
    [19.96, 50.045],
    [20.0, 50.04],
    [20.05, 50.03],
  ];
  readonly multiLineString = [
    [
      [19.88, 50.1],
      [19.94, 50.11],
    ],
    [
      [19.88, 50.115],
      [19.94, 50.125],
    ],
    [
      [19.88, 50.13],
      [19.94, 50.14],
    ],
  ];
  readonly polygon = [
    [
      [19.96, 50.075],
      [19.99, 50.075],
      [19.99, 50.09],
      [19.96, 50.09],
      [19.96, 50.075],
    ],
  ];
  readonly multiPolygon = [
    [
      [
        [20.1, 50.06],
        [20.13, 50.06],
        [20.13, 50.08],
        [20.1, 50.08],
        [20.1, 50.06],
      ],
    ],
    [
      [
        [20.15, 50.06],
        [20.18, 50.06],
        [20.18, 50.08],
        [20.15, 50.08],
        [20.15, 50.06],
      ],
    ],
  ];
}

Architecture and OpenLayers mechanics

Each aol-feature wraps a dedicated geometry element—aol-geometry-point, -multipoint, -linestring, -multilinestring, -polygon, -multipolygon or -circle—populated from component arrays via aol-collection-coordinates with srid="EPSG:4326" so degree tuples project automatically into Web Mercator.

Attaching a child aol-style overrides the layer style for that specific feature, while omitting it (as on the two line features) falls back cleanly to OpenLayers’ default blue stroke, and aol-geometry-circle expresses its radius in map projection units.

How it works

  • Each aol-feature mounts one geometry component (aol-geometry-point, -multipoint, -linestring, -multilinestring, -polygon, -multipolygon or -circle).
  • aol-collection-coordinates with srid="EPSG:4326" transforms WGS84 degree arrays from component properties into the view projection.
  • Feature-level aol-style blocks override layer defaults, whereas unstyled line features inherit OpenLayers’ default stroke.
  • aol-geometry-circle defines its radius in the map projection’s metre units.

Pitfalls

  • Every linear ring of a polygon must close by repeating its first coordinate pair at the end—leaving a ring open produces malformed geometry.
  • Analytical circles are not a native GeoJSON geometry type: serialise them either as a centre point plus numeric radius attribute or approximate them as a multi-vertex polygon before exporting to GeoJSON.

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.