Skip to content

Commit

Permalink
m
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Jan 5, 2025
1 parent ecf095c commit a2ec2d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
17 changes: 13 additions & 4 deletions man/convhull.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Convex, concave, rectangular and circular hulls
\description{
Compute hulls around SpatVector geometries. This can be the convex hull, the minimal bounding rotated rectangle, the minimal bounding circle, or a concave hull. The concaveness of the concave hull can be specified in different ways.

The old methods \code{convHull}, \code{minRect} and \code{minCircle} are depracated and will be removed in a future version.
The old methods \code{convHull}, \code{minRect} and \code{minCircle} are deprecated and will be removed in a future version.
}

\usage{
Expand All @@ -27,17 +27,26 @@ The old methods \code{convHull}, \code{minRect} and \code{minCircle} are depraca

\arguments{
\item{x}{SpatVector}
\item{type}{character. One of "convex", "rectangle", "circle", "concave_ratio", "concave_length", "concave_polygons"}
\item{type}{character. One of "convex", "rectangle", "circle", "concave_ratio", "concave_length"}
\item{by}{character (variable name), to get a new geometry for groups of input geometries}
\item{param}{numeric between 0 and 1. For the "concave_*" types only. For \code{type="concave_ratio"} this is The edge length ratio value, between 0 and 1. For \code{type="concave_length"} this the maximum edge length (a value > 0). For \code{type="concave_polygons"} thism specifies the maximum Edge Length as a fraction of the difference between the longest and shortest edge lengths between the polygons. This normalizes the maximum edge length to be scale-free. A value of 1 produces the convex hull; a value of 0 produces the original polygons}
\item{allowHoles}{logical. May the polygonal output contain holes? For the "concave_* types only}
\item{tight}{logical. Should the hull follow the outer boundaries of the input polygons? For the "concave_* types only}
\item{allowHoles}{logical. May the output polygons contain holes? For "concave_*" methods only}
\item{tight}{logical. Should the hull follow the outer boundaries of the input polygons? For "concave_length" with polygon geometry only}
}

\value{
SpatVector
}


\details{
A concave hull is a polygon which contains all the points of the input. It can be a better representation of the input data (typically points) than the convex hull. There are many possible convex hulls with different degrees of concaveness. These can be created with argument \code{param}.

The hull is constructed by removing the longest outer edges of the Delaunay Triangulation of the space between the polygons, until the target criterion \code{param} is reached. If \code{type="concave_ratio"}, \code{param} expresses is the ratio between the lengths of the longest and shortest edges. 1 produces the convex hull; 0 produces a hull with maximum concaveness. If \code{type="concave_length"}, \code{param} specifies the maximm edge length. A large value produces the convex hull, 0 produces the hull of maximum concaveness.
}



\examples{
p <- vect(system.file("ex/lux.shp", package="terra"))
h <- hull(p)
Expand Down
35 changes: 19 additions & 16 deletions src/geos_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -992,6 +992,12 @@ SpatVector SpatVector::hull(std::string htype, std::string by, double param, boo
return out;
}

std::vector<std::string> methods = {"convex", "rectangle", "circle", "concave_ratio", "concave_length"};
if (std::find(methods.begin(), methods.end(), htype) == methods.end()) {
out.setError("unknown hull type");
return out;
}

if (!by.empty()) {
SpatVector tmp = aggregate(by, false);
if (tmp.hasError()) {
Expand All @@ -1017,21 +1023,16 @@ SpatVector SpatVector::hull(std::string htype, std::string by, double param, boo

out.reserve(size());


if (htype != "convex") {
#ifndef GEOS361
out.setError("GEOS 3.6.1 required for rotated rectangle");
return out;
#endif
if (is_lonlat()) {
if ((extent.ymin > -85) && (extent.ymax < 85)) {
SpatVector tmp = project("+proj=merc", false);
tmp = tmp.hull(htype, "");
tmp = tmp.project(srs.wkt, false);
return tmp;
}
/*
if (is_lonlat()) {
if ((extent.ymin > -85) && (extent.ymax < 85)) {
SpatVector tmp = project("+proj=merc", false);
tmp = tmp.hull(htype, "");
tmp = tmp.project(srs.wkt, false);
return tmp;
}
}
*/

SpatVector a = aggregate(false);

Expand Down Expand Up @@ -1066,9 +1067,11 @@ SpatVector SpatVector::hull(std::string htype, std::string by, double param, boo
if (htype == "concave_ratio") {
h = GEOSConcaveHull_r(hGEOSCtxt, g[0].get(), param, allowHoles);
} else if (htype == "concave_length") {
h = GEOSConcaveHullByLength_r(hGEOSCtxt, g[0].get(), param, allowHoles);
} else if (htype == "concave_polygons") {
h = GEOSConcaveHullOfPolygons_r(hGEOSCtxt, g[0].get(), param, tight, allowHoles);
if (type() == "polygons") {
h = GEOSConcaveHullOfPolygons_r(hGEOSCtxt, g[0].get(), param, tight, allowHoles);
} else {
h = GEOSConcaveHullByLength_r(hGEOSCtxt, g[0].get(), param, allowHoles);
}
} else {
geos_finish(hGEOSCtxt);
out.setError("unknown hull type");
Expand Down

0 comments on commit a2ec2d7

Please sign in to comment.