File: image.R

package info (click to toggle)
r-cran-sp 1%3A0.9-66-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,696 kB
  • ctags: 123
  • sloc: ansic: 1,475; sh: 6; makefile: 5
file content (133 lines) | stat: -rw-r--r-- 4,956 bytes parent folder | download
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
# first argument of image generic _needs_ to be x!
image.SpatialPixelsDataFrame = function(x, ...)
	image(as(x, "SpatialGridDataFrame"), ...)

image.SpatialGridDataFrame = function(x, attr = 1, xcol = 1, ycol = 2,
                col = heat.colors(12), 
		red=NULL, green=NULL, blue=NULL, axes = FALSE, xlim = NULL, 
		ylim = NULL, add = FALSE, ..., asp = NA, 
		setParUsrBB=FALSE, interpolate = FALSE, angle = 0,
                useRasterImage=TRUE) {

	if (!add)
		plot(as(x, "Spatial"),
			xlim = xlim, ylim = ylim, axes = axes, asp = asp, ..., 
			setParUsrBB=setParUsrBB)
        if (exists("rasterImage") && useRasterImage) {
            bb <- bbox(x)
            scl <- function(x) (x - min(x, na.rm  = TRUE)) /
                diff(range(x, na.rm = TRUE))
        }
	if (is.null(red)) {
            if (exists("rasterImage") && useRasterImage) {
                x <- x[attr]
                NAs <- is.na(x[[1]])
                m <-  scl(t(matrix(x[[1]], x@grid@cells.dim[1],
                    x@grid@cells.dim[2])))
                m <- matrix(col[as.vector(m) * (length(col)-1) + 1], 
                    nrow(m), ncol(m))
                ## if missing, set to white
                m[is.na(m)] <- rgb(1, 1, 1)
                rasterImage(m, bb[1,1], bb[2,1], bb[1,2], bb[2,2],
                    interpolate = interpolate, angle = angle)
            } else {
		image(as.image.SpatialGridDataFrame(x[attr], xcol, ycol), 
                  add = TRUE, col = col, ...)
            }
	} else {
	    if (is.null(green) || is.null(blue)) 
		stop("all colour bands must be given")
# modified to handle NAs in input (typical for coercion of Spatial Pixels
# to Spatial Grid)
            if (exists("rasterImage") && useRasterImage) {
                xd <- x@data[, c(red, green, blue)]
                NAs <- is.na(xd[, 1]) | is.na(xd[, 2]) | is.na(xd[, 3])
                if (any(NAs))
                    xd <- xd[!NAs, ]
                ## create RGBs (using alpha=1 by default)
                RGBs <- rgb(xd, max = 255)
                if (any(NAs)) {
                    z <- rep(NA, length(NAs))
                    z[!NAs] <- RGBs
                    RGBs <- z
                }
                cv <- coordinatevalues(getGridTopology(x))
                m <- t(matrix(RGBs, x@grid@cells.dim[1], 
                    x@grid@cells.dim[2], byrow = FALSE))
                rasterImage(m, bb[1,1], bb[2,1], bb[1,2], bb[2,2],
                    interpolate = interpolate, angle = angle)
            } else {
		xd <- x@data[,c(red, green, blue)]
		NAs <- is.na(xd[,1]) | is.na(xd[,2]) | is.na(xd[,3])
		if (any(NAs)) xd <- xd[!NAs,]
		RGBs <- rgb(xd, max=255)
		if (any(NAs)) {
		    z <- rep(NA, length(NAs))
		    z[!NAs] <- RGBs
		    RGBs <- z
		}
		fcols <- factor(RGBs)
		cv <- coordinatevalues(getGridTopology(x))
		m <- matrix(as.integer(fcols), x@grid@cells.dim[1], 
			x@grid@cells.dim[2], byrow=FALSE)
		res <- list(x=cv[[xcol]], y=sort(cv[[ycol]]), z=m[,ncol(m):1])
		image(res, col=levels(fcols), add = TRUE, ...)
            }
	}
}

contour.SpatialGridDataFrame = function(x, attr = 1, xcol = 1, ycol = 2,
                col = 1, add = FALSE, xlim = NULL, ylim = NULL,
                axes = FALSE, ..., setParUsrBB = FALSE)  {
	if (!add)
		plot(as(x, "Spatial"),
			xlim = xlim, ylim = ylim, axes = axes, ..., 
			setParUsrBB=setParUsrBB)
	contour(as.image.SpatialGridDataFrame(x[attr], xcol, ycol), col = col,
          add = TRUE, ...)
}

contour.SpatialPixelsDataFrame = function(x, ...)
	contour(as(x, "SpatialGridDataFrame"), ...)

as.image.SpatialGridDataFrame = function(x, xcol = 1, ycol = 2, attr = 1) {
	cv = coordinatevalues(getGridTopology(x))
	m = as(x[attr], "matrix")
	list(x = cv[[xcol]], y = sort(cv[[ycol]]), z = m[,ncol(m):1])
}

# contributed by Michael Sumner 24 Oct 2007

image2Grid <- function (im, p4 = as.character(NA), digits=10) 
{
    if (!all(c("x", "y", "z") %in% names(im))) 
        stop("image must have components x, y, and z")
# RSB reversed test order
    lux <- length(unique(signif(diff(im$x), digits=digits)))
    luy <- length(unique(signif(diff(im$y), digits=digits)))
    if (lux > 1 || luy > 1) stop("x or y not equally spaced")
# RSB check for equal spacing
    cells.dim <- dim(im$z)
    xx <- im$x
    yy <- im$y
    lx <- length(xx)
    ly <- length(yy)
    if (all(c(lx, ly) == (cells.dim + 1))) {
        ##print("corners")
        if (!(lx == nrow(im$z) + 1 && ly == ncol(im$z) + 1 ) )
            stop("dimensions of z are not length(x)(-1) times length(y)(-1)")

        xx <- xx[-1] - diff(xx[1:2])/2
        yy <- yy[-1] - diff(yy[1:2])/2
    } else {

        if (!(lx == nrow(im$z) && ly == ncol(im$z)))
            stop("dimensions of z are not length(x) times length(y)")
    }

    SpatialGridDataFrame(GridTopology(c(xx[1], yy[1]), c(diff(xx[1:2]), 
        diff(yy[1:2])), cells.dim), data.frame(z = as.vector(im$z[, 
        ncol(im$z):1])), proj4string = CRS(p4))
}