Vector data · Angular, React and OpenLayers

Buffer streets with JSTS

Compute true 40-metre ground buffers around 456 Kraków Old Town streets in the browser using JSTS and OpenLayers latitude-adjusted projection.

The streets of Kraków's Old Town as 40-metre buffers in the default blue around the Main Square, over OpenStreetMap at zoom 15.
A screenshot of the example. Map data © OpenStreetMap contributors.

What this example shows

At zoom 15 over Kraków’s Old Town and the Main Square, 456 named street centrelines loaded from GeoJSON are replaced in the browser by accurate 40-metre ground-distance buffer polygons calculated with the JavaScript Topology Suite (JSTS).

When you need it

Proximity and setback zones—such as noise corridors within 40 m of a road, riparian buffers along streams, or excavation safety zones around gas mains—are core spatial analysis tasks.

Computing geometric buffers client-side with JSTS gives instant interactive feedback on loaded or user-drawn features without round-tripping to PostGIS ST_Buffer.

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.

jsts.ts
import { afterNextRender, ChangeDetectionStrategy, Component, viewChild } from '@angular/core';
import GeoJSON from 'ol/format/GeoJSON';
import type Geometry from 'ol/geom/Geometry';
import MultiPolygon from 'ol/geom/MultiPolygon';
import {
  CoordinateComponent,
  DefaultControlComponent,
  DefaultInteractionComponent,
  LayerTileComponent,
  LayerVectorComponent,
  MapComponent,
  SourceOsmComponent,
  SourceVectorComponent,
  ViewComponent,
} from 'cc-openlayers';
import { bufferGeometry } from 'cc-openlayers/geometry';

/**
 * The named streets of Kraków's Old Town and around it, each replaced by a buffer of 40 metres on the ground computed
 * with JSTS (through `bufferGeometry` of cc-openlayers/geometry, which reads OpenLayers geometries into JSTS and back).
 */
@Component({
  selector: 'app-jsts',
  imports: [
    CoordinateComponent,
    DefaultControlComponent,
    DefaultInteractionComponent,
    LayerTileComponent,
    LayerVectorComponent,
    MapComponent,
    SourceOsmComponent,
    SourceVectorComponent,
    ViewComponent,
  ],
  changeDetection: ChangeDetectionStrategy.OnPush,
  template: `
    <aol-map height="480px">
      <aol-interaction-default />
      <aol-control-defaults />
      <aol-view [zoom]="15">
        <aol-coordinate [x]="19.9373" [y]="50.0617" srid="EPSG:4326" />
      </aol-view>
      <aol-layer-tile><aol-source-osm /></aol-layer-tile>
      <aol-layer-vector>
        <aol-source-vector />
      </aol-layer-vector>
    </aol-map>
  `,
})
export class Jsts {
  private readonly source = viewChild.required(SourceVectorComponent);

  constructor() {
    afterNextRender(() => {
      fetch('assets/krakow/old-town-streets.geojson')
        .then(function (response) {
          return response.json();
        })
        .then((json) => {
          const format = new GeoJSON();
          const features = format.readFeatures(json, {
            featureProjection: 'EPSG:3857',
          });

          for (let i = 0; i < features.length; i++) {
            const feature = features[i];
            // create a buffer of 40 metres around each line, through JSTS
            const buffered = bufferGeometry(feature.getGeometry() as Geometry, 40, 'EPSG:3857');

            // replace the geometry on the feature
            feature.setGeometry(buffered.length === 1 ? buffered[0] : new MultiPolygon(buffered));
          }

          this.source().instance.addFeatures(features);
        });
    });
  }
}

Architecture and OpenLayers mechanics

Street centrelines parse from GeoJSON into Web Mercator via featureProjection. For each feature, bufferGeometry(feature.getGeometry() as Geometry, 40, 'EPSG:3857') from cc-openlayers/geometry converts the requested 40 true ground metres into latitude-compensated Web Mercator units, runs JSTS’s topological buffer operation, and writes the resulting Polygon or MultiPolygon back onto the feature.

How it works

  • 456 street lines (340 named streets) load from GeoJSON into Web Mercator using featureProjection.
  • bufferGeometry(feature.getGeometry() as Geometry, 40, 'EPSG:3857') computes a true 40-metre ground buffer per street.
  • The helper compensates automatically for Web Mercator latitude scale distortion before invoking JSTS and converting back.
  • Single or multi-part results (including MultiPolygon outputs) replace each street’s line geometry in place.

Pitfalls

  • Passing 40 raw Web Mercator units directly to an uncompensated planar buffer at Kraków’s 50° N latitude would produce only a ~25.7-metre ground buffer—always scale planar buffer distances by latitude (bufferGeometry handles this automatically).
  • Buffering each street feature individually leaves overlapping translucent polygons at intersections; run a JSTS unary union if your analysis requires one dissolved corridor polygon.

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.