Define a polygon hull to encompass a set of data points

points2polygonHull(x, returnClass = c("matrix", "SpatialPolygons"), ...)

Arguments

x

two-column numeric matrix of data points, or an object of class sp::SpatialPoints.

returnClass

character string indicating the class to return, either "matrix" to return a two-column numeric matrix, or "SpatialPolygons" to return sp::SpatialPolygons.

verbose

logical whether to print verbose output.

Value

A two-column numeric matrix of polygon coordinates when returnClass="matrix" by default, or when returnClass="SpatialPolygons" it returns an object of class sp::SpatialPolygons.

Details

A polygon hull can be useful as a visual indicator of the location of a set of points. This function creates a polygon around all points, and not just the most dense region of points, using grDevices::chull().

The output is slightly modified to duplicate the first coordinate, in order to "close" the resulting polygon, which is consistent with the output of rgeos::gConvexHull().

Examples

set.seed(123);
xy <- cbind(x=1:10,
   y=sample(1:10, 10));
xyhull <- points2polygonHull(xy);
plot(xy, pch=20, cex=1, col="purple4",
   main="Polygon hull around points");
polygon(xyhull, border="purple2", col="transparent");