Skip to main content

distance

Computes the Cartesian distance between geometry a and geometry b.

Distance is in input geometry units.

Parameters

NameTypeDescription
aGeometry | Prepared<Geometry>First geometry
bGeometrySecond geometry

Returns

number

The distance between geometries

Throws

  • GEOSError on unsupported geometry types (curved)
  • GEOSError when either geometry is empty

See also

  • distanceWithin returns true when two geometries are within a given distance
  • nearestPoints finds the nearest points of two geometries
  • prepare improves performance of repeated calls against a single geometry

Examples

distance between point and line
distance between line and polygon
distance between two polygons
to improve performance of repeated calls against a single geometry
const a = buffer(point([ 0, 0 ]), 10, { quadrantSegments: 1000 });
// `a` is a polygon with many vertices (4000 in this example)
prepare(a);
// preparation of geometry `a` will improve the performance of repeated
// `distance` calls, but only those where `a` is the first geometry
const r1 = distance(a, point([ 12, 0 ]));
const r2 = distance(a, point([ 12, 1 ]));
const r3 = distance(point([ 12, 2 ]), a); // no benefit from prepared geometry