@@ -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