Skip to content

Commit

Permalink
x
Browse files Browse the repository at this point in the history
  • Loading branch information
rhijmans committed Aug 24, 2023
1 parent 8095c6f commit 86e57f5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 21 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
## new
`compareGeom` for list and SpatRasterCollection [#1207](https://github.com/rspatial/terra/issues/1207) by Sarah Endicott
`is.rotated<SpatRaster>` method [#1229](https://github.com/rspatial/terra/issues/1229) by Andy Lyons

`forceCCW<SpatVector>` method to force counter-clockwise orientation [#1249](https://github.com/rspatial/terra/issues/1249) by srfall.

# version 1.7-39

Expand Down
2 changes: 1 addition & 1 deletion R/geom.R
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,6 @@ setMethod("forceCCW", signature(x="SpatVector"),
function(x) {
x <- deepcopy(x)
x@pnt$make_CCW()
x
messages(x)
}
)
33 changes: 33 additions & 0 deletions man/forceCCW.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
\name{forceCCW}

\alias{forceCCW}
\alias{forceCCW,SpatVector-method}


\title{force counter-clockwise polygons}


\description{
Assure that the nodes of outer rings of polygons are in counter-clockwise order.
}

\usage{
\S4method{forceCCW}{SpatVector}(x)
}

\arguments{
\item{x}{SpatVector of polygons}
}

\value{
SpatVector
}

\examples{
p <- vect("POLYGON ((2 45, 2 55, 18 55, 18 45, 2 45))")
pcc <- forceCCW(p)
geom(pcc, wkt=TRUE)
}

\keyword{spatial}

27 changes: 27 additions & 0 deletions src/geos_methods.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3218,3 +3218,30 @@ bool SpatPart::is_CCW() {
}
#endif
}



void SpatVector::make_CCW() {
#ifndef GEOS370
setError("GEOS >= 3.7 needed for CCW");
return;
#else
size_t n = size();
if (n == 0) return;
if (geoms[0].gtype != polygons) return;
for (size_t i=0; i<n; i++) {
for (size_t j=0; j<geoms[i].parts.size(); j++) {
if (!geoms[i].parts[j].is_CCW()) {
std::reverse(geoms[i].parts[j].x.begin(), geoms[i].parts[j].x.end());
std::reverse(geoms[i].parts[j].y.begin(), geoms[i].parts[j].y.end());
for (size_t k=0; k<geoms[i].parts[j].nHoles(); k++) {
std::reverse(geoms[i].parts[j].holes[k].x.begin(), geoms[i].parts[j].holes[k].x.end());
std::reverse(geoms[i].parts[j].holes[k].y.begin(), geoms[i].parts[j].holes[k].y.end());
}
}
}
}
#endif
}


19 changes: 0 additions & 19 deletions src/spatVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1433,22 +1433,3 @@ std::vector<std::vector<std::vector<std::vector<double>>>> SpatVector::polygonsL



void SpatVector::make_CCW() {
size_t n = size();
if (n == 0) return;
if (geoms[0].gtype != polygons) return;
for (size_t i=0; i<n; i++) {
for (size_t j=0; j<geoms[i].parts.size(); j++) {
if (!geoms[i].parts[j].is_CCW()) {
std::reverse(geoms[i].parts[j].x.begin(), geoms[i].parts[j].x.end());
std::reverse(geoms[i].parts[j].y.begin(), geoms[i].parts[j].y.end());
for (size_t k=0; k<geoms[i].parts[j].nHoles(); k++) {
std::reverse(geoms[i].parts[j].holes[k].x.begin(), geoms[i].parts[j].holes[k].x.end());
std::reverse(geoms[i].parts[j].holes[k].y.begin(), geoms[i].parts[j].holes[k].y.end());
}
}
}
}
}


0 comments on commit 86e57f5

Please sign in to comment.