Vector data · Angular, React and OpenLayers

A rectangle from a bounding box

Draw a closed geographic bounding-box polygon over Kraków in an OpenLayers template using aol-geometry-polygon and EPSG:4326 corner coordinates.

A rectangle in OpenLayers' default blue outline and light fill over most of Kraków on OpenStreetMap at zoom 11.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

At zoom 11 over OpenStreetMap, a crisp rectangular bounding box spanning western and central Kraków (19.8°–20.07° E, 49.99°–50.13° N) renders with OpenLayers’ default translucent white fill and blue outline directly from four corner values in component state.

When you need it

Map sheet indexes, spatial export tools, imagery footprints and search-area previews constantly visualise [minLon, minLat, maxLon, maxLat] bounding boxes on the map.

Constructing a five-point closed polygon ring in the template keeps simple bounding-box overlays declarative and synchronized with form bounds.

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.

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

/** A rectangular polygon, the corners of a bounding box, over an OpenStreetMap base map. */
@Component({
  selector: 'app-rectangle',
  imports: [
    CollectionCoordinatesComponent,
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    FeatureComponent,
    GeometryPolygonComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="11"><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>
            <aol-geometry-polygon>
              <aol-collection-coordinates [coordinates]="box" srid="EPSG:4326" />
            </aol-geometry-polygon>
          </aol-feature>
        </aol-source-vector>
      </aol-layer-vector>
    </aol-map>
  `,
})
export class Rectangle {
  /** The box from the minimum to the maximum longitude and latitude, as the ring of a polygon. */
  protected readonly box = [
    [
      [19.8, 49.99],
      [20.07, 49.99],
      [20.07, 50.13],
      [19.8, 50.13],
      [19.8, 49.99],
    ],
  ];
}

Architecture and OpenLayers mechanics

Component property box arranges the minimum and maximum longitude/latitude into a five-vertex linear ring—repeating the starting corner at index 4 to close the polygon—and passes that ring into aol-geometry-polygon via aol-collection-coordinates with srid="EPSG:4326" so OpenLayers transforms the corners into Web Mercator.

How it works

  • box builds the closed five-coordinate ring from minimum and maximum longitude/latitude values, repeating the first corner last.
  • aol-geometry-polygon and aol-collection-coordinates with srid="EPSG:4326" project degree corners into the view CRS.
  • The rectangle covers 19.8°–20.07° E and 49.99°–50.13° N across Kraków using OpenLayers’ default vector style.

Pitfalls

  • Transforming only four corners keeps edges straight along meridians and parallels in cylindrical Web Mercator, whereas conic or polar projections curve parallels—densify long east-west edges with intermediate vertices before reprojecting onto conic grids.
  • GeoJSON and OGC WKT specifications strictly require linear rings to repeat their initial vertex at the end; a four-point unclosed array will be rejected by spatial backends.

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.