CHAPTER 21
Buffering and spatial operations
Expanding geometries by a distance, computing unions and intersections, and building spatial pipelines with Turf.js.
A buffer is the zone of all points within distance d of a geometry. It's one of the most common spatial operations: "find everything within 500 m of this road", "draw the flood risk zone 100 m from this river". Buffers are also the foundation for proximity analysis: rather than querying with radius, you materialise the zone as a polygon and use polygon containment checks.
Line buffer
100 m buffer
Point buffer
A point buffer is a circle. Given centre and radius metres, generate points around it:
function circleBuffer(lat, lon, radiusM, steps = 64) {
const R = 6_371_008.8;
const Δlat = (radiusM / R) * (180 / Math.PI);
const Δlon = Δlat / Math.cos((lat * Math.PI) / 180);
return Array.from({ length: steps + 1 }, (_, i) => {
const θ = (i / steps) * 2 * Math.PI;
return [lon + Δlon * Math.cos(θ), lat + Δlat * Math.sin(θ)];
});
}
This approximates the circle in planar space, which is accurate for radii under ~50 km. For larger radii, use the destination-point formula (Chapter 9) to generate each vertex from the center with the exact bearing and distance.
Line buffer
A line buffer creates a corridor around a LineString. The approach:
- For each segment, compute the perpendicular offset lines at distance d
- Add semicircular end caps at each terminus
- Union all the pieces into a single polygon
This is significantly more complex to implement from scratch. In practice, use Turf.js:
import buffer from "@turf/buffer";
const road = {
type: "Feature",
geometry: { type: "LineString", coordinates: [[74.0, 31.5], [75.0, 31.8]] },
};
const zone = buffer(road, 0.5, { units: "kilometers" });
// → GeoJSON Polygon representing a 500-m corridor
Set operations
Once you have buffered geometries, set operations let you combine them:
| Operation | What it produces |
|---|---|
| Union | All area covered by either A or B |
| Intersection | Only the area covered by both A and B |
| Difference | Area of A that doesn't overlap B |
| Symmetric difference | Area covered by either but not both |
These are standard set operations applied to geographic area. Union, intersection, and difference work on polygons the same way the set operations work on sets of numbers. The underlying implementation uses polygon clipping algorithms (Sutherland-Hodgman or Greiner-Hormann).
Continue reading "Buffering and spatial operations"
You've reached the end of the free preview. Unlock all 22 paid chapters, including distance math, bearings, polygons, spatial indexing, and 3D map rendering, plus a downloadable PDF and the companion code repo.
- All 22 paid chapters with worked examples
- Downloadable PDF for offline reading
- Companion GitHub repo (JavaScript + Python)
- Free updates for life
Multiple payment options: card, Wise, and bank transfer.
Related chapters
- Polygons: area, centroid, and containment, buffers are polygons
- Destination points: moving on a sphere, buffers are rings of destination points
- The antimeridian: the bug that breaks every map, buffers crossing ±180° need special handling
