1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
## 11/21/07 dhm
## version where shift works for lines and polygons
## adding option to rotate
if (!isGeneric("elide")) {
setGeneric("elide", function(obj, ...) {
standardGeneric("elide")
})
}
elide.points <- function(obj, bb=NULL, shift=c(0, 0), reflect=c(FALSE, FALSE),
scale=NULL, flip=FALSE, rotate=0, center=NULL, unitsq=FALSE) {
if (length(shift) != 2)
stop("Two coordinate shift in input units required")
if (!is.numeric(shift)) stop("shift not numeric")
if (!is.logical(reflect)) stop("reflect must be logical")
if (length(reflect) != 2) stop("Two coordinate reflect required")
if (!is.logical(flip)) stop("flip must be logical")
if (!is.numeric(rotate)) stop("rotate not numeric")
if (!is.null(center) && length(center) != 2)
stop("center must be numeric of length two")
if (!is.logical(unitsq)) stop("unitsq must be logical")
crds <- coordinates(obj)
if (is.null(bb)) bb <- bbox(obj)
if (rotate != 0 && is.null(center)) center <- bb[,1]
if (rotate != 0) crds <- rotateCoords(crds, rotate, center)
if (flip) {
y <- crds[,1] + shift[1]
x <- crds[,2] + shift[2]
yr <- bb[1,] + shift[1]
xr <- bb[2,] + shift[2]
} else {
x <- crds[,1] + shift[1]
y <- crds[,2] + shift[2]
xr <- bb[1,] + shift[1]
yr <- bb[2,] + shift[2]
}
bb <- NULL
if (!is.null(scale) && is.logical(scale) && scale && unitsq) {
bb <- rbind(c(0,1), c(0,1))
colnames(bb) <- c("min", "max")
}
scale <- scaleCoords(scale=scale, xr=xr, yr=yr)
crds <- elideCoords(x=x, y=y, xr=xr, yr=yr, reflect=reflect, scale=scale)
res <- SpatialPoints(crds, bbox=bb)
res
}
elide.pointsdf <- function(obj, bb=NULL, shift=c(0, 0),
reflect=c(FALSE, FALSE), scale=NULL, flip=FALSE, rotate=0, center=NULL) {
res <- elide(as(obj, "SpatialPoints"), bb=bb, shift=shift,
reflect=reflect, scale=scale, flip=flip, rotate=rotate, center=center)
# df <- as(obj, "data.frame")[,-c(1,2)]
# df <- as(obj, "data.frame")
df <- slot(obj, "data")
res <- SpatialPointsDataFrame(res, data=df)
res
}
setMethod("elide", signature(obj="SpatialPoints"), elide.points)
setMethod("elide", signature(obj="SpatialPointsDataFrame"), elide.pointsdf)
## rotate angle degrees clockwise around center
rotateCoords <- function(crds, angle=0, center= c(min(crds[,1]),min(crds[,2]))) {
co <- cos(-angle*pi/180)
si <- sin(-angle*pi/180)
adj <- matrix(rep(center,nrow(crds)),ncol=2,byrow=TRUE)
crds <- crds-adj
cbind(co * crds[,1] - si * crds[,2],
si * crds[,1] + co * crds[,2]) + adj
}
scaleCoords <- function(scale, xr, yr) {
if (!is.null(scale)) {
if (is.logical(scale) && scale) scale <- 1
else if (!is.numeric(scale)) stop("scale neither TRUE nor numeric")
dx <- abs(diff(xr))
dy <- abs(diff(yr))
md <- max(dx, dy)
scale <- scale * (1/md)
} else scale <- 1
scale
}
elideCoords <- function(x, y, xr, yr, reflect, scale, rotate, center) {
if (reflect[1]) {
x <- xr[2] - x + xr[1]
}
if (reflect[2]) {
y <- yr[2] - y + yr[1]
}
if (!isTRUE(all.equal(scale, 1))) {
x <- (x - xr[1]) * scale
y <- (y - yr[1]) * scale
}
crds <- cbind(x, y)
crds
}
elide.lines <- function(obj, bb=NULL, shift=c(0, 0), reflect=c(FALSE, FALSE),
scale=NULL, inverse=FALSE, flip=FALSE, rotate=0, center=NULL) {
if (length(shift) != 2)
stop("Two coordinate shift in input units required")
if (!is.numeric(shift)) stop("shift not numeric")
if (!is.logical(reflect)) stop("reflect must be logical")
if (length(reflect) != 2) stop("Two coordinate reflect required")
if (!is.logical(flip)) stop("flip must be logical")
if (!is.numeric(rotate)) stop("rotate not numeric")
if (!is.null(center) && length(center) != 2)
stop("center must be numeric of length two")
if (is.null(bb)) bb <- bbox(obj)
if (rotate != 0 && is.null(center)) center <- bb[,1]
if (flip) {
yr <- bb[1,] + shift[1]
xr <- bb[2,] + shift[2]
} else {
xr <- bb[1,] + shift[1]
yr <- bb[2,] + shift[2]
}
scale <- scaleCoords(scale=scale, xr=xr, yr=yr)
lns <- slot(obj, "lines")
new_lns <- lapply(lns, function(x) {
Lns <- slot(x, "Lines")
new_Lns <- lapply(Lns, function(y) {
crds <- slot(y, "coords")
## rotate first, then elide (shift) [side effects if bb, scale or others supplied]
if (rotate != 0) crds <- rotateCoords(crds, rotate, center)
if (flip) {
yc <- crds[,1] + shift[1]
xc <- crds[,2] + shift[2]
} else {
xc <- crds[,1] + shift[1]
yc <- crds[,2] + shift[2]
}
new_crds <- elideCoords(x=xc, y=yc, xr=xr, yr=yr,
reflect=reflect, scale=scale)
## if want to elide first, then rotate:
## new_crds <- rotcrds(new_crds,rotate,center)
Line(new_crds)})
Lines(new_Lns, ID=slot(x, "ID"))})
res <- SpatialLines(new_lns)
res
}
elide.linesdf <- function(obj, bb=NULL, shift=c(0, 0), reflect=c(FALSE, FALSE),
scale=NULL, inverse=FALSE, flip=FALSE, rotate=0, center=NULL) {
res <- elide(as(obj, "SpatialLines"), bb=bb, shift=shift,
reflect=reflect, scale=scale, flip=flip, rotate=rotate, center=center)
df <- as(obj, "data.frame")
res <- SpatialLinesDataFrame(res, data=df)
res
}
setMethod("elide", signature(obj="SpatialLines"), elide.lines)
setMethod("elide", signature(obj="SpatialLinesDataFrame"), elide.linesdf)
elide.polygons <- function(obj, bb=NULL, shift=c(0, 0), reflect=c(FALSE, FALSE),
scale=NULL, inverse=FALSE, flip=FALSE, rotate=0, center=NULL) {
if (length(shift) != 2)
stop("Two coordinate shift in input units required")
if (!is.numeric(shift)) stop("shift not numeric")
if (!is.logical(reflect)) stop("reflect must be logical")
if (length(reflect) != 2) stop("Two coordinate reflect required")
if (!is.logical(flip)) stop("flip must be logical")
if (!is.numeric(rotate)) stop("rotate not numeric")
if (!is.null(center) && length(center) != 2)
stop("center must be numeric of length two")
if (is.null(bb)) bb <- bbox(obj)
if (rotate != 0 && is.null(center)) center <- bb[,1]
if (flip) {
yr <- bb[1,] + shift[1]
xr <- bb[2,] + shift[2]
} else {
xr <- bb[1,] + shift[1]
yr <- bb[2,] + shift[2]
}
scale <- scaleCoords(scale=scale, xr=xr, yr=yr)
pls <- slot(obj, "polygons")
new_pls <- lapply(pls, function(x) {
Pls <- slot(x, "Polygons")
new_Pls <- lapply(Pls, function(y) {
crds <- slot(y, "coords")
if (rotate != 0) crds <- rotateCoords(crds,rotate,center)
if (flip) {
yc <- crds[,1] + shift[1]
xc <- crds[,2] + shift[2]
} else {
xc <- crds[,1] + shift[1]
yc <- crds[,2] + shift[2]
}
new_crds <- elideCoords(x=xc, y=yc, xr=xr, yr=yr,
reflect=reflect, scale=scale)
Polygon(new_crds)})
Polygons(new_Pls, ID=slot(x, "ID"))})
res <- SpatialPolygons(new_pls)
res
}
elide.polygonsdf <- function(obj, bb=NULL, shift=c(0, 0),
reflect=c(FALSE, FALSE), scale=NULL, inverse=FALSE, flip=FALSE,
rotate=0, center=NULL) {
res <- elide(as(obj, "SpatialPolygons"), bb=bb, shift=shift,
reflect=reflect, scale=scale, flip=flip, rotate=rotate, center=center)
df <- as(obj, "data.frame")
res <- SpatialPolygonsDataFrame(res, data=df)
res
}
setMethod("elide", signature(obj="SpatialPolygons"), elide.polygons)
setMethod("elide", signature(obj="SpatialPolygonsDataFrame"), elide.polygonsdf)
|