Skip to main content

distanceWithin

Returns true when the distance between two geometries is within the given distance, false otherwise.

Returns false when negative distance is given or when either geometry is empty.

Distance is in input geometry units.

Parameters

NameTypeDescription
aGeometry | Prepared<Geometry>First geometry
bGeometrySecond geometry
maxDistancenumberThe maximum distance

Returns

boolean

true when the distance between the geometries is less than or equal to the given distance, false otherwise

Throws

  • GEOSError on unsupported geometry types (curved)

See also

  • distance computes the distance between two geometries
  • nearestPoints finds the nearest points of two geometries
  • prepare improves performance of repeated calls against a single geometry

Examples

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
// `distanceWithin` calls, but only those where `a` is the first geometry
const r1 = distanceWithin(a, point([ 12, 0 ]), 2);
const r2 = distanceWithin(a, point([ 12, 1 ]), 2);
const r3 = distanceWithin(point([ 12, 2 ]), a, 2); // no benefit from prepared geometry