Skip to content

Commit 28ff2a3

Browse files
committed
resolve issue with rgeos in SetPolygons
1 parent 8df6b9c commit 28ff2a3

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: inlmisc
22
Title: Miscellaneous Functions for the USGS INL Project Office
3-
Version: 0.4.6.9000
3+
Version: 0.4.7
44
Authors@R: person(given=c("Jason", "C."), family="Fisher", role=c("aut", "cre"), email="jfisher@usgs.gov", comment=c(ORCID="0000-0001-9032-8912"))
55
Description: A collection of functions for creating high-level graphics,
66
performing raster-based analysis, processing MODFLOW-based models,

NEWS.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# inlmisc 0.4.6.9000
1+
# inlmisc 0.4.7
22

3-
- ...
3+
- In `SetPolygons`, set `checkValidity` argument to 2 and suppress warnings.
44

55
# inlmisc 0.4.6
66

R/SetPolygons.R

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -74,44 +74,41 @@ SetPolygons <- function(x, y, cmd=c("gIntersection", "gDifference"), buffer.widt
7474
y <- methods::as(y, "SpatialPolygons")
7575
y <- y[which(apply(rgeos::gIntersects(y, x, byid=TRUE), 2, any)), ]
7676

77-
are.intersecting <- rgeos::gIntersects(x, y, byid=TRUE)
77+
is_intersecting <- rgeos::gIntersects(x, y, byid=TRUE)
7878

79-
z <- lapply(seq_along(x), function (i) {
80-
if (any(are.intersecting[, i])) {
81-
y.intersect <- y[are.intersecting[, i]]
79+
z <- suppressMessages(suppressWarnings(lapply(seq_along(x), function (i) {
80+
81+
if (any(is_intersecting[, i])) {
82+
y_intersect <- y[is_intersecting[, i]]
8283
if (is.numeric(buffer.width))
83-
y.intersect <- rgeos::gBuffer(y.intersect, width=buffer.width)
84+
y_intersect <- rgeos::gBuffer(y_intersect, width=buffer.width)
85+
86+
spgeom2 <- rgeos::gUnaryUnion(y_intersect, checkValidity=2L)
8487

85-
spgeom2 <- rgeos::gUnaryUnion(y.intersect)
8688
if (cmd == "gIntersection")
87-
x.geo <- rgeos::gIntersection(x[i], spgeom2, byid=TRUE)
89+
x_geo <- rgeos::gIntersection(x[i], spgeom2, byid=TRUE, checkValidity=2L)
8890
else
89-
x.geo <- rgeos::gDifference(x[i], spgeom2, byid=TRUE)
90-
91-
if (inherits(x.geo, "SpatialCollections"))
92-
x.geo <- rgeos::gUnaryUnion(x.geo@polyobj)
91+
x_geo <- rgeos::gDifference(x[i], spgeom2, byid=TRUE, checkValidity=2L)
9392

94-
is.valid <- suppressWarnings(rgeos::gIsValid(x.geo, byid=TRUE))
95-
if (length(is.valid) == 0) return(NULL)
96-
if (!is.valid) {
97-
x.geo <- rgeos::gBuffer(x.geo, width=0)
98-
ans <- rgeos::gIsValid(x.geo, byid=TRUE, reason=TRUE)
99-
if (ans != "Valid Geometry") stop(paste("non-valid polygons:", ans))
100-
}
93+
if (inherits(x_geo, "SpatialCollections"))
94+
x_geo <- rgeos::gUnaryUnion(x_geo@polyobj, checkValidity=2L)
10195

102-
p <- x.geo@polygons[[1]]
96+
p <- x_geo@polygons[[1]]
10397
methods::slot(p, "ID") <- methods::slot(x[i]@polygons[[1]], "ID")
98+
10499
} else {
105100
p <- if (cmd == "gIntersection") NULL else x[i]@polygons[[1]]
106101
}
102+
107103
p
108-
})
104+
})))
109105

110-
is.retained <- !vapply(z, is.null, TRUE)
111-
z <- sp::SpatialPolygons(z[is.retained], proj4string=raster::crs(x))
106+
is_retained <- !vapply(z, is.null, TRUE)
107+
z <- sp::SpatialPolygons(z[is_retained], proj4string=raster::crs(x))
112108
if (inherits(d, "data.frame")) {
113-
d <- d[is.retained, , drop=FALSE]
109+
d <- d[is_retained, , drop=FALSE]
114110
z <- sp::SpatialPolygonsDataFrame(z, d, match.ID=TRUE)
115111
}
112+
116113
z
117114
}

0 commit comments

Comments
 (0)