| 1 |
|
| 2 |
# copied from raster: |
| 3 |
if (!isGeneric("disaggregate")) { |
| 4 |
setGeneric("disaggregate", function(x, ...) |
| 5 |
standardGeneric("disaggregate")) |
| 6 |
} |
| 7 |
|
| 8 |
# Robert Hijmans: |
| 9 |
explodePolygons <- function(x, ignoreholes=FALSE, ...) { |
| 10 |
warning("No rgeos support in sp from October 2023;\nsee https://r-spatial.org/r/2023/05/15/evolution4.html") |
| 11 |
# if (!requireNamespace("rgeos", quietly = TRUE)) |
| 12 |
stop('use sf or terra functions') |
| 13 |
npols <- length(x@polygons) |
| 14 |
crs <- x@proj4string |
| 15 |
count <- 0 |
| 16 |
p <- NULL |
| 17 |
np <- vector(length=npols) |
| 18 |
for (i in 1:npols) { |
| 19 |
np[i] <- length(x@polygons[[i]]@Polygons) |
| 20 |
if (np[i] > 1) { |
| 21 |
parts <- x@polygons[[i]]@Polygons |
| 22 |
if (ignoreholes) { |
| 23 |
holes <- FALSE |
| 24 |
} else { |
| 25 |
holes <- sapply(parts, function(x)x@hole) |
| 26 |
} |
| 27 |
if (any(holes)) { |
| 28 |
if (np[i]==2) { |
| 29 |
pp <- x@polygons[[i]] |
| 30 |
pp@ID <- as.character(count + 1) |
| 31 |
} else { |
| 32 |
# cmt <- as.integer(unlist(strsplit(rgeos::createPolygonsComment(x@polygons[[i]]), ' '))) |
| 33 |
cmt <- comment(x@polygons[[i]]) |
| 34 |
if (is.null(cmt)) |
| 35 |
stop("legacy Spatial object without polygon comments: please repair first using as(st_as_sf(x), \"Spatial\")") |
| 36 |
cmt <- as.integer(unlist(strsplit(cmt, ' '))) |
| 37 |
cmt <- cbind(id=1:length(cmt), holeOf=cmt) |
| 38 |
cmt <- cmt[cmt[,2] > 0, ,drop=FALSE] |
| 39 |
pp <- NULL |
| 40 |
add <- 0 |
| 41 |
for (j in unique(cmt[,2])) { |
| 42 |
# there might be multiple holes in a single polygon |
| 43 |
k <- cmt[cmt[,2]==j, 1] |
| 44 |
add <- add + 1 |
| 45 |
pp <- c(pp, Polygons(x@polygons[[i]]@Polygons[c(j, k)], count + add)) |
| 46 |
} |
| 47 |
x@polygons[[i]]@Polygons <- x@polygons[[i]]@Polygons[-unique(as.vector(cmt))] |
| 48 |
if (length(x@polygons[[i]]@Polygons) > 0) { |
| 49 |
parts <- x@polygons[[i]]@Polygons |
| 50 |
pp <- c(pp, sapply(1:length(parts), function(g) Polygons(parts[g], count + add + g))) |
| 51 |
} |
| 52 |
} |
| 53 |
np[i] <- np[i] - sum(holes) |
| 54 |
} else { |
| 55 |
pp <- sapply(1:np[i], function(g) Polygons(parts[g], count + g)) |
| 56 |
} |
| 57 |
} else { |
| 58 |
pp <- x@polygons[[i]] |
| 59 |
pp@ID <- as.character(count + 1) |
| 60 |
} |
| 61 |
count <- count + np[i] |
| 62 |
p <- c(p, pp) |
| 63 |
} |
| 64 |
p <- SpatialPolygons(p) |
| 65 |
ps <- slot(p, "polygons") |
| 66 |
for (i in seq_along(ps)) { |
| 67 |
# comment(ps[[i]]) <- rgeos::createPolygonsComment(ps[[i]]) |
| 68 |
n = length(ps[[i]]@Polygons) |
| 69 |
comment(ps[[i]]) <- do.call(paste, as.list(c("0", rep("1", n - 1)))) # they must be all holes now! |
| 70 |
} |
| 71 |
slot(p, "polygons") <- ps |
| 72 |
proj4string(p) <- crs |
| 73 |
|
| 74 |
if (.hasSlot(x, 'data')) { |
| 75 |
np <- rep(1:npols, np) |
| 76 |
x <- x@data[np, , drop = FALSE] |
| 77 |
#rownames(x) <- 1:nrow(x) |
| 78 |
rownames(x) <- NULL |
| 79 |
SpatialPolygonsDataFrame(p, x, FALSE) |
| 80 |
} else { |
| 81 |
p |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
setMethod("disaggregate", "SpatialPolygons", |
| 88 |
function(x,...) explodePolygons(x, ...)) |
| 89 |
|
| 90 |
setMethod("disaggregate", "SpatialPolygonsDataFrame", |
| 91 |
function(x,...) explodePolygons(x, ...)) |
| 92 |
|
| 93 |
|
| 94 |
explodeLines <- function(x, ...) { |
| 95 |
nlins <- length(x@lines) |
| 96 |
crs <- x@proj4string |
| 97 |
count <- 0 |
| 98 |
p <- NULL |
| 99 |
nl <- vector(length=nlins) |
| 100 |
for (i in 1:nlins) { |
| 101 |
parts <- x@lines[[i]]@Lines |
| 102 |
nl[i] <- length(parts) |
| 103 |
p <- c(p, sapply(1:nl[i], function(x) Lines(parts[x], count + x))) |
| 104 |
count <- count + nl[i] |
| 105 |
} |
| 106 |
p <- SpatialLines(p) |
| 107 |
proj4string(p) <- crs |
| 108 |
if (.hasSlot(x, 'data')) { |
| 109 |
nl <- rep(1:nlins, nl) |
| 110 |
x <- x@data[nl, , drop = FALSE] |
| 111 |
rownames(x) <- 1:nrow(x) |
| 112 |
SpatialLinesDataFrame(p, x, FALSE) |
| 113 |
} else |
| 114 |
p |
| 115 |
} |
| 116 |
|
| 117 |
setMethod("disaggregate", "SpatialLines", |
| 118 |
function(x,...) explodeLines(x, ...)) |
| 119 |
|
| 120 |
setMethod("disaggregate", "SpatialLinesDataFrame", |
| 121 |
function(x,...) explodeLines(x, ...)) |
| 122 |
|
| 123 |
# Roger, claims Barry wrote it first: |
| 124 |
unfoldLines = function(x) { |
| 125 |
crds <- coordinates(x) |
| 126 |
nobjs <- sum(sapply(crds, length)) |
| 127 |
out <- vector(mode="list", length=nobjs) |
| 128 |
i <- 1 |
| 129 |
for (j in seq_along(crds)) { |
| 130 |
jcrds <- crds[[j]] |
| 131 |
for (k in seq_along(jcrds)) { |
| 132 |
out[[i]] <- Lines(list(Line(jcrds[k])), as.character(i)) |
| 133 |
i <- i + 1 |
| 134 |
} |
| 135 |
} |
| 136 |
SLout <- SpatialLines(out) |
| 137 |
length(SLout) |
| 138 |
} |