Debian Codegraph @

R/GridTopology-methods.R

raw
1 GridTopology = function(cellcentre.offset, cellsize, cells.dim) {
2 new("GridTopology",
3 cellcentre.offset = cellcentre.offset,
4 cellsize = cellsize,
5 cells.dim = as.integer(cells.dim))
6 }
7
8 # setMethod("show", "GridTopology", function(object) summary(object))
9
10 as.data.frame.GridTopology = function(x, row.names, optional, ...) data.frame(
11 cellcentre.offset = [email protected],
12 cellsize = x@cellsize,
13 cells.dim = [email protected]
14 )
15
16 setAs("GridTopology", "data.frame", function(from) as.data.frame.GridTopology(from))
17
18 setMethod("coordinates", "GridTopology", function(obj) {
19 cc = do.call(expand.grid, coordinatevalues(obj))
20 # as.matrix(sapply(cc, as.numeric))
21 # dropping dimension for single cell grid
22 do.call(cbind, lapply(cc, as.numeric))
23 })
24
25 coordnamesGT = function(x, value) {
26 names([email protected]) = value
27 names(x@cellsize) = value
28 names([email protected]) = value
29 x
30 }
31
32 setReplaceMethod("coordnames",
33 signature(x = "GridTopology", value = "character"), coordnamesGT)
34
35 coordinatevalues = function(obj) {
36 if (!is(obj, "GridTopology"))
37 stop("function only works for objects of class or extending GridTopology")
38 ret = list()
39 for (i in seq_along([email protected])) {
40 if (i == 2) # y-axis is the exception--starting at top of map, and decreasing:
41 ret[[i]] = [email protected][i] +
42 obj@cellsize[i] * (([email protected][i] - 1):0)
43 else
44 ret[[i]] = [email protected][i] +
45 obj@cellsize[i] * (0:([email protected][i] - 1))
46 }
47 ns = names([email protected])
48 if (is.null(ns))
49 ns = paste("s", 1:length(ret), sep = "") #dimnames(obj@bbox)[[1]]
50 names(ret) = ns
51 ret
52 }
53
54 points2grid = function(points, tolerance=sqrt(.Machine$double.eps),
55 round = NULL) {
56 # work out grid topology from points
57 n = dimensions(points)
58 ret = new("GridTopology",
59 cellcentre.offset = numeric(n),
60 cellsize = numeric(n),
61 cells.dim = as.integer(rep(1,n)))
62 cc = coordinates(points)
63 nr <- nrow(cc)
64 for (i in 1:n) { # loop over x, y, and possibly z
65 x = cc[, i]
66 sux = sort(unique(x))
67 difx = diff(sux)
68 if (length(difx) == 0) {
69 warning(paste("cell size from constant coordinate", i,
70 "possibly taken from other coordinate"))
71 ret@cellsize[i] = NA
72 [email protected][i] = min(sux)
73 [email protected][i] = as.integer(1)
74 } else {
75 ru.difx = range(unique(difx)) # min to max x coord leaps
76 # next, find cases where some of the differences are
77 # VERY close to zero, and remove these:
78 if (ru.difx[1] / ru.difx[2] < tolerance) {
79 difx = difx[difx > ru.difx[2] * tolerance]
80 ru.difx = range(unique(difx)) # reset
81 }
82 err1 = diff(ru.difx) #?? /max(range(abs(sux))) # (max-min)/max(abs(x))
83 if (err1 > tolerance) {
84 xx = ru.difx / min(ru.difx)
85 err2 = max(abs(floor(xx) - xx)) # is it an integer multiple?
86 if (err2 > tolerance) {
87 cat(paste("suggested tolerance minimum:", signif(err2, 6),
88 "\n"))
89 stop(paste("dimension", i,": coordinate intervals are not constant"))
90 } else {
91 difx = difx[difx < ru.difx[1] + tolerance]
92 warning(paste("grid has empty column/rows in dimension", i))
93 }
94 }
95 ret@cellsize[i] = mean(difx)
96 [email protected][i] = min(sux)
97 [email protected][i] = as.integer(round(diff(range(sux))/ret@cellsize[i]) + 1)
98 #was: length(sux), but this will not cope with empty rows.
99 if ([email protected][i] > nr)
100 warning(paste("grid topology may be corrupt in dimension", i))
101 }
102 }
103 cs = ret@cellsize
104 if (any(is.na(cs))) {
105 if (all(is.na(cs)))
106 stop("cannot derive grid parameters from a single point!")
107 ret@cellsize[is.na(cs)] = cs[!is.na(cs)][1]
108 }
109 nm = dimnames(cc)[[2]]
110 names(ret@cellsize) = nm
111 names([email protected]) = nm
112 names([email protected]) = nm
113 ret
114 }
115
116 .NumberOfCells = function(x) {
117 if (!is(x, "GridTopology"))
118 stop(".NumberOfCells only works on objects of class GridTopology")
119 prod([email protected])
120 }
121
122 print.GridTopology = function(x, ...) {
123 res = data.frame(rbind([email protected], x@cellsize, as.numeric([email protected])))
124 rownames(res) = c("cellcentre.offset", "cellsize", "cells.dim")
125 if (!is.null(names([email protected])))
126 names(res) = names([email protected])
127 print(res)
128 invisible(res)
129 }
130 setMethod("show", "GridTopology", function(object) print.GridTopology(object))
131
132 summ.GridTopology = function(object, ...) {
133 ret = list()
134 ret[["values"]] = gridparameters(object)
135 class(ret) = "summary.GridTopology"
136 ret
137 }
138 setMethod("summary", "GridTopology", summ.GridTopology)
139
140 print.summary.GridTopology = function(x, ...) {
141 cat("Grid topology:\n")
142 print(x$values)
143 invisible(x)
144 }
145
146 # make a SpatialPolygons from a GridTopology - NERSC query
147
148 as.SpatialPolygons.GridTopology <- function(grd, proj4string=CRS(as.character(NA)))
149 {
150 coordnam = names([email protected])
151 grd_crds <- coordinates(grd)
152 IDs <- IDvaluesGridTopology(grd)
153 nPolygons <- nrow(grd_crds)
154 cS <- grd@cellsize
155 cS2 <- cS/2
156 cS2x <- cS2[1]
157 cS2y <- cS2[2]
158 Srl <- vector(mode="list", length=nPolygons)
159 for (i in 1:nPolygons) {
160 xi <- grd_crds[i,1]
161 yi <- grd_crds[i,2]
162 x <- c(xi-cS2x, xi-cS2x, xi+cS2x, xi+cS2x, xi-cS2x)
163 y <- c(yi-cS2y, yi+cS2y, yi+cS2y, yi-cS2y, yi-cS2y)
164 coords = cbind(x, y)
165 if (!is.null(coordnam))
166 dimnames(coords) = list(NULL, coordnam)
167 else
168 rownames(coords) = NULL
169 Srl[[i]] <- Polygons(list(Polygon(coords = coords)), ID = IDs[i])
170 comment(Srl[[i]]) <- "0"
171 }
172 res <- SpatialPolygons(Srl, proj4string = proj4string)
173 # address https://github.com/edzer/sp/issues/3:
174 if (!is.null(coordnam))
175 dimnames(res@bbox)[[1]] = coordnam
176 res
177 }
178 setAs("GridTopology", "SpatialPolygons", function(from)
179 as.SpatialPolygons.GridTopology(from))
180
181 # mostly copied from coordinates() for GridTopology,
182 # makes IDs "c(i)r(j)" matching the coordinates
183 # used with SpatialRing-ed grids for the data frame rowname()
184
185 IDvaluesGridTopology <- function(obj) {
186 if (!is(obj, "GridTopology"))
187 stop("function only works for objects of class or extending GridTopology")
188 cc <- getGridIndex(coordinates(obj), obj)
189 res <- as.matrix(sapply(cc, as.integer))
190 paste("g", res, sep="")
191 }
192