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.

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.tsimport { 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);
});
});
}
} The React version uses the library's React adapter, cc-openlayers/react: each component creates the OpenLayers object it names when it mounts, updates it from its props and disposes of it when it unmounts.
jsts.react.tsximport { useEffect, useRef } from 'react';
import GeoJSON from 'ol/format/GeoJSON.js';
import type Geometry from 'ol/geom/Geometry.js';
import MultiPolygon from 'ol/geom/MultiPolygon.js';
import type OLVectorSource from 'ol/source/Vector.js';
import {
Coordinate,
DefaultControls,
DefaultInteractions,
Map,
OSMSource,
TileLayer,
VectorLayer,
VectorSource,
View,
} from 'cc-openlayers/react';
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).
*/
export function Jsts() {
const source = useRef<OLVectorSource>(null);
useEffect(() => {
// StrictMode runs the effect twice: only the answer to the request of the last run is used.
let current = true;
fetch('assets/krakow/old-town-streets.geojson')
.then(function (response) {
return response.json();
})
.then((json) => {
if (!current) return;
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));
}
source.current?.addFeatures(features);
});
return () => {
current = false;
};
}, []);
return (
<Map height="480px">
<DefaultInteractions />
<DefaultControls />
<View zoom={15}>
<Coordinate x={19.9373} y={50.0617} srid="EPSG:4326" />
</View>
<TileLayer>
<OSMSource />
</TileLayer>
<VectorLayer>
<VectorSource ref={source} />
</VectorLayer>
</Map>
);
}How the React version works
The Old Town's streets replaced by 40 m buffers computed by bufferGeometry from cc-openlayers/geometry.
What both versions share
OpenLayers renders the map and handles the interactions; the cc-openlayers/core layer gives both adapters the same view binding (MapViewBinding) and the same conversion of coordinates from EPSG:4326 to the map's projection.
From Angular to React
| Angular | React |
|---|---|
afterNextRender fetching the file | useEffect with a cancel flag |
viewChild of the source | useRef<OLVectorSource> |
Who creates, updates and disposes
Map creates the ol/Map in useLayoutEffect and disposes of it on unmount; each layer, source and interaction component adds its object to the parent it gets from context and removes it in its effect's cleanup, as ngOnDestroy does in Angular.
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
MultiPolygonoutputs) replace each street’s line geometry in place.
Pitfalls
- Passing
40raw 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 (bufferGeometryhandles 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.
Related examples
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.