Debian Codegraph @

R/elide.R

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