nearestPointTo method
Returns the Point with minimum distance between the FeatureCollection and the point
.
If the FeatureCollection is empty, returns an empty FeatureCollection.
Example:
FeatureCollection([
Point([Coordinate(1, 2)]),
Point([Coordinate(5, 6)])
]).nearest(Point([Coordinate(0, 0)])); // Point(1, 2)
FeatureCollection([]).nearest(Point([Coordinate(0, 0)])); // null
FeatureCollection([
LineString([Coordinate(1, 2), Coordinate(3, 4)]),
Implementation
Point? nearestPointTo(Point point) {
if (isEmpty) {
return null;
} else if (!isCollectionOf(Point.type)) {
return null;
}
double minDistance = double.infinity;
Point nearestPoint = features[0] as Point;
for (final feature in (features as List<Point>)) {
final distance = feature.coordinate.distanceTo(point.coordinate);
if (distance < minDistance) {
minDistance = distance;
nearestPoint = feature;
}
}
return nearestPoint;
}