Skip to main content

nearestPoints

Finds the nearest points between geometry a and geometry b.

The returned points can be points along a line segment, not necessarily one of the vertices of the input geometries.

Parameters

NameTypeDescription
aGeometry | Prepared<Geometry>First geometry
bGeometrySecond geometry

Returns

[ Point, Point ]

An array with two point geometries, first is the nearest point from the geometry a second from the geometry b

Throws

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

See also

  • distance computes the distance between two geometries
  • distanceWithin returns true when two geometries are within a given distance
  • prepare improves performance of repeated calls against a single geometry

Examples

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