| 1 |
# first argument of image generic _needs_ to be x! |
| 2 |
image.SpatialPixelsDataFrame = function(x, ...) |
| 3 |
image(as(x, "SpatialGridDataFrame"), ...) |
| 4 |
|
| 5 |
image.SpatialPixels = function(x, ...) { |
| 6 |
x <- SpatialPixelsDataFrame(x, data=data.frame(rep(1, |
| 7 |
dim(coordinates(x))[1]))) |
| 8 |
image(x, ...) |
| 9 |
} |
| 10 |
|
| 11 |
image.SpatialGridDataFrame = function(x, attr = 1, xcol = 1, ycol = 2, |
| 12 |
col = heat.colors(12), |
| 13 |
red=NULL, green=NULL, blue=NULL, axes = FALSE, xlim = NULL, |
| 14 |
ylim = NULL, add = FALSE, ..., asp = NA, |
| 15 |
setParUsrBB=FALSE, interpolate = FALSE, angle = 0, |
| 16 |
useRasterImage = !(.Platform$GUI[1] == "Rgui" && getIdentification() == "R Console") && |
| 17 |
missing(breaks), |
| 18 |
breaks, zlim = range(as.numeric(x[[attr]])[is.finite(x[[attr]])])) { |
| 19 |
|
| 20 |
if (!add) |
| 21 |
suppressWarnings(plot(as(x, "Spatial"), |
| 22 |
xlim = xlim, ylim = ylim, axes = axes, asp = asp, ..., |
| 23 |
setParUsrBB=setParUsrBB)) |
| 24 |
|
| 25 |
if (exists("rasterImage") && useRasterImage) { |
| 26 |
bb <- bbox(x) |
| 27 |
scl <- function(xx, zlim) { |
| 28 |
xx = matrix(as.numeric(xx), nrow(xx), ncol(xx)) |
| 29 |
#dr <- diff(range(x, na.rm = TRUE)) |
| 30 |
dr = diff(zlim) |
| 31 |
#mx <- min(x, na.rm = TRUE) |
| 32 |
mx = zlim[1] |
| 33 |
xx[xx < zlim[1] | xx > zlim[2]] = NA |
| 34 |
if (abs(dr) < .Machine$double.eps) |
| 35 |
res <- ifelse(is.na(xx), xx, 0.5) |
| 36 |
else res <- (xx - mx) / dr |
| 37 |
res |
| 38 |
} |
| 39 |
} |
| 40 |
if (is.null(red)) { |
| 41 |
if (exists("rasterImage") && useRasterImage) { |
| 42 |
if (!missing(breaks)) |
| 43 |
warning("set useRasterImage to FALSE when using breaks") |
| 44 |
#x <- x[attr] |
| 45 |
# NAs <- is.na(x[[1]]) |
| 46 |
m <- scl(t(matrix(x[attr][[1]], x@[email protected][1], |
| 47 |
x@[email protected][2])), zlim) |
| 48 |
m <- matrix(col[as.vector(m) * (length(col)-1) + 1], nrow(m), ncol(m)) |
| 49 |
## if missing, set to transparent |
| 50 |
# m[is.na(m)] <- rgb(1, 1, 1, 0) |
| 51 |
rasterImage(m, bb[1,1], bb[2,1], bb[1,2], bb[2,2], |
| 52 |
interpolate = interpolate, angle = angle) |
| 53 |
} else { |
| 54 |
if (is.factor(x[[attr]])) |
| 55 |
x[[attr]] = as.numeric(x[[attr]]) |
| 56 |
image(as.image.SpatialGridDataFrame(x[attr], xcol, ycol), |
| 57 |
add = TRUE, col = col, zlim = zlim, breaks = breaks, ...) |
| 58 |
} |
| 59 |
} else { |
| 60 |
if (is.null(green) || is.null(blue)) |
| 61 |
stop("all colour bands must be given") |
| 62 |
# modified to handle NAs in input (typical for coercion of Spatial Pixels |
| 63 |
# to Spatial Grid) |
| 64 |
if (exists("rasterImage") && useRasterImage) { |
| 65 |
if (!missing(breaks)) |
| 66 |
warning("set useRasterImage to FALSE when using breaks") |
| 67 |
xd <- x@data[, c(red, green, blue)] |
| 68 |
NAs <- is.na(xd[, 1]) | is.na(xd[, 2]) | is.na(xd[, 3]) |
| 69 |
if (any(NAs)) |
| 70 |
xd <- xd[!NAs, ] |
| 71 |
## create RGBs (using alpha=1 by default) |
| 72 |
RGBs <- rgb(xd, maxColorValue = 255) |
| 73 |
if (any(NAs)) { |
| 74 |
z <- rep(NA, length(NAs)) |
| 75 |
z[!NAs] <- RGBs |
| 76 |
RGBs <- z |
| 77 |
} |
| 78 |
cv <- coordinatevalues(getGridTopology(x)) |
| 79 |
m <- t(matrix(RGBs, x@[email protected][1], |
| 80 |
x@[email protected][2], byrow = FALSE)) |
| 81 |
rasterImage(m, bb[1,1], bb[2,1], bb[1,2], bb[2,2], |
| 82 |
interpolate = interpolate, angle = angle) |
| 83 |
} else { |
| 84 |
xd <- x@data[,c(red, green, blue)] |
| 85 |
NAs <- is.na(xd[,1]) | is.na(xd[,2]) | is.na(xd[,3]) |
| 86 |
if (any(NAs)) xd <- xd[!NAs,] |
| 87 |
RGBs <- rgb(xd, maxColorValue = 255) |
| 88 |
if (any(NAs)) { |
| 89 |
z <- rep(NA, length(NAs)) |
| 90 |
z[!NAs] <- RGBs |
| 91 |
RGBs <- z |
| 92 |
} |
| 93 |
fcols <- factor(RGBs) |
| 94 |
cv <- coordinatevalues(getGridTopology(x)) |
| 95 |
m <- matrix(as.integer(fcols), x@[email protected][1], |
| 96 |
x@[email protected][2], byrow=FALSE) |
| 97 |
res <- list(x=cv[[xcol]], y=sort(cv[[ycol]]), |
| 98 |
z=m[,ncol(m):1,drop=FALSE]) |
| 99 |
image(res, col=levels(fcols), add = TRUE, breaks = breaks, ...) |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
contour.SpatialGridDataFrame = function(x, attr = 1, xcol = 1, ycol = 2, |
| 105 |
col = 1, add = FALSE, xlim = NULL, ylim = NULL, |
| 106 |
axes = FALSE, ..., setParUsrBB = FALSE) { |
| 107 |
if (!add) |
| 108 |
plot(as(x, "Spatial"), |
| 109 |
xlim = xlim, ylim = ylim, axes = axes, ..., |
| 110 |
setParUsrBB=setParUsrBB) |
| 111 |
contour(as.image.SpatialGridDataFrame(x[attr], xcol, ycol), col = col, |
| 112 |
add = TRUE, ...) |
| 113 |
} |
| 114 |
|
| 115 |
contour.SpatialPixelsDataFrame = function(x, ...) |
| 116 |
contour(as(x, "SpatialGridDataFrame"), ...) |
| 117 |
|
| 118 |
as.image.SpatialGridDataFrame = function(x, xcol = 1, ycol = 2, attr = 1) { |
| 119 |
cv = coordinatevalues(getGridTopology(x)) |
| 120 |
m = as(x[attr], "matrix") |
| 121 |
list(x = cv[[xcol]], y = sort(cv[[ycol]]), z = m[,ncol(m):1,drop=FALSE]) |
| 122 |
} |
| 123 |
|
| 124 |
# contributed by Michael Sumner 24 Oct 2007 |
| 125 |
|
| 126 |
image2Grid <- function (im, p4 = as.character(NA), digits=10) |
| 127 |
{ |
| 128 |
if (!all(c("x", "y", "z") %in% names(im))) |
| 129 |
stop("image must have components x, y, and z") |
| 130 |
# RSB reversed test order |
| 131 |
lux <- length(unique(signif(diff(im$x), digits=digits))) |
| 132 |
luy <- length(unique(signif(diff(im$y), digits=digits))) |
| 133 |
if (lux > 1 || luy > 1) stop("x or y not equally spaced") |
| 134 |
# RSB check for equal spacing |
| 135 |
cells.dim <- dim(im$z) |
| 136 |
xx <- im$x |
| 137 |
yy <- im$y |
| 138 |
lx <- length(xx) |
| 139 |
ly <- length(yy) |
| 140 |
if (all(c(lx, ly) == (cells.dim + 1))) { |
| 141 |
##print("corners") |
| 142 |
if (!(lx == nrow(im$z) + 1 && ly == ncol(im$z) + 1 ) ) |
| 143 |
stop("dimensions of z are not length(x)(-1) times length(y)(-1)") |
| 144 |
|
| 145 |
xx <- xx[-1] - diff(xx[1:2])/2 |
| 146 |
yy <- yy[-1] - diff(yy[1:2])/2 |
| 147 |
} else { |
| 148 |
|
| 149 |
if (!(lx == nrow(im$z) && ly == ncol(im$z))) |
| 150 |
stop("dimensions of z are not length(x) times length(y)") |
| 151 |
} |
| 152 |
|
| 153 |
SpatialGridDataFrame(GridTopology(c(xx[1], yy[1]), c(diff(xx[1:2]), |
| 154 |
diff(yy[1:2])), cells.dim), data.frame(z = as.vector(im$z[, |
| 155 |
ncol(im$z):1])), proj4string = CRS(p4)) |
| 156 |
} |
| 157 |
|
| 158 |
# menugget, |
| 159 |
# from https://gist.github.com/menugget/7689145/raw/dac746aa322ca4160a5fe66c70fec16ebe26faf9/image.scale.2.r |
| 160 |
|
| 161 |
#This function creates a color scale for use with the image() |
| 162 |
#function. Input parameters should be consistent with those |
| 163 |
#used in the corresponding image plot. The "axis.pos" argument |
| 164 |
#defines the side of the axis. The "add.axis" argument defines |
| 165 |
#whether the axis is added (default: TRUE)or not (FALSE). |
| 166 |
imageScale <- function(z, zlim, col = heat.colors(12), |
| 167 |
breaks, axis.pos = 1, add.axis = TRUE, ...) { |
| 168 |
if (!missing(breaks)) |
| 169 |
if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")} |
| 170 |
if (missing(breaks) & !missing(zlim)) |
| 171 |
breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) |
| 172 |
if (missing(breaks) & missing(zlim)) { |
| 173 |
zlim <- range(z, na.rm=TRUE) |
| 174 |
zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions |
| 175 |
zlim[1] <- zlim[1]-c(zlim[2]-zlim[1])*(1E-3) |
| 176 |
breaks <- seq(zlim[1], zlim[2], length.out=(length(col)+1)) |
| 177 |
} |
| 178 |
poly <- vector(mode="list", length(col)) |
| 179 |
for (i in seq(poly)) |
| 180 |
poly[[i]] <- c(breaks[i], breaks[i+1], breaks[i+1], breaks[i]) |
| 181 |
if (axis.pos %in% c(1,3)){ |
| 182 |
ylim<-c(0,1) |
| 183 |
xlim<-range(breaks) |
| 184 |
} |
| 185 |
if (axis.pos %in% c(2,4)) { |
| 186 |
ylim<-range(breaks) |
| 187 |
xlim<-c(0,1) |
| 188 |
} |
| 189 |
plot(1,1,t="n",ylim=ylim, xlim=xlim, axes=FALSE, xlab="", ylab="", xaxs="i", yaxs="i", ...) |
| 190 |
for (i in seq(poly)) { |
| 191 |
if(axis.pos %in% c(1,3)) |
| 192 |
polygon(poly[[i]], c(0,0,1,1), col=col[i], border=NA) |
| 193 |
if(axis.pos %in% c(2,4)) |
| 194 |
polygon(c(0,0,1,1), poly[[i]], col=col[i], border=NA) |
| 195 |
} |
| 196 |
box() |
| 197 |
if (add.axis) |
| 198 |
axis(axis.pos) |
| 199 |
} |