MapMath

CHAPTER 21

Buffering and spatial operations

Expanding geometries by a distance, computing unions and intersections, and building spatial pipelines with Turf.js.

3 min read

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

100m

Point buffer

A point buffer is a circle. Given centre (φ,λ)(φ, λ) and radius rr metres, generate nn 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:

  1. For each segment, compute the perpendicular offset lines at distance d
  2. Add semicircular end caps at each terminus
  3. 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:

OperationWhat it produces
UnionAll area covered by either A or B
IntersectionOnly the area covered by both A and B
DifferenceArea of A that doesn't overlap B
Symmetric differenceArea covered by either but not both
optional — skip if familiarrefresher

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).

Chapter 21 · Paid content

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 including Wise, PayPal, and bank transfer.