Debian Codegraph @

R/SpatialPointsDataFrame-methods.R

raw
1 "SpatialPointsDataFrame" = function(coords, data, coords.nrs = numeric(0),
2 proj4string = CRS(as.character(NA)), match.ID, bbox = NULL) {
3
4 if (is(coords, "SpatialPoints") &&
5 !is.na(slot(proj4string, "projargs")))
6 warning("If the coords argument is a SpatialPoints object, set its CRS first;\n the proj4string argument to this function is ignored.")
7
8 if (!is(coords, "SpatialPoints"))
9 coords = coordinates(coords)
10 # make sure data.frame becomes double matrix; NA checks
11 mtch = NULL
12 cc.ID = row.names(coords) # row.names works for both matrix AND SpatialPoints
13 if (missing(match.ID)) { # sort it out:
14 if (is.null(cc.ID))
15 match.ID = FALSE # nothing to match to!
16 else {
17 mtch = match(cc.ID, row.names(data))
18 match.ID = !any(is.na(mtch)) # && length(unique(mtch)) == nrow(data)
19 if (match.ID && any(mtch != 1:nrow(data)))
20 warning("forming a SpatialPointsDataFrame based on maching IDs, not on record order. Use match.ID = FALSE to match on record order")
21 }
22 } else if (is.character(match.ID)) {
23 row.names(data) = data[, match.ID[1]]
24 match.ID = TRUE
25 }
26
27 if (match.ID) {
28 if (!is.null(cc.ID) && is(data, "data.frame")) { # match ID:
29 if (is.null(mtch))
30 mtch = match(cc.ID, row.names(data))
31 if (any(is.na(mtch)))
32 stop("row.names of data and coords do not match")
33 if (length(unique(mtch)) != nrow(data))
34 stop("row.names of data and dimnames of coords do not match")
35 data = data[mtch, , drop = FALSE]
36 }
37 }
38 if (!is(coords, "SpatialPoints"))
39 coords = SpatialPoints(coords, proj4string = proj4string, bbox = bbox)
40 # EJP, Tue Aug 13 19:54:04 CEST 2013
41 if (is.character(attr(data, "row.names"))) # i.e., data has "real" row names
42 dimnames(coords@coords)[[1]] = row.names(data)
43 new("SpatialPointsDataFrame", coords, data = data, coords.nrs = coords.nrs)
44 }
45
46 setMethod("coordinates", "SpatialPointsDataFrame", function(obj) obj@coords)
47
48 setMethod("addAttrToGeom", signature(x = "SpatialPoints", y = "data.frame"),
49 function(x, y, match.ID, ...)
50 SpatialPointsDataFrame(x, y, match.ID = match.ID, ...)
51 )
52
53 setReplaceMethod("coordinates", signature(object = "data.frame", value = "ANY"),
54 function(object, value) {
55 coord.numbers = NULL
56 if (inherits(value, "formula")) {
57 cc = model.frame(value, object, na.action = na.fail) # retrieve coords
58 if (dim(cc)[2] == 2) {
59 nm = as.character(as.list(value)[[2]])[2:3]
60 coord.numbers = match(nm, names(object))
61 } else if (dim(cc)[2] == 3) {
62 nm = c(as.character(as.list((as.list(value)[[2]])[2])[[1]])[2:3],
63 as.character(as.list(value)[[2]])[3])
64 coord.numbers = match(nm, names(object))
65 } # else: give up.
66 } else if (is.character(value)) {
67 cc = object[, value] # retrieve coords
68 coord.numbers = match(value, names(object))
69 } else if (is.null(dim(value)) && length(value) > 1) { # coord.columns?
70 if (any(value != as.integer(value)) || any(value < 1))
71 stop("coordinate columns should be positive integers")
72 cc = object[, value] # retrieve coords
73 coord.numbers = value
74 } else # raw coordinates given; try transform them to matrix:
75 cc = coordinates(value)
76 if (any(is.na(cc)))
77 stop("coordinates are not allowed to contain missing values")
78 if (!is.null(coord.numbers)) {
79 object = object[ , -coord.numbers, drop = FALSE]
80 stripped = coord.numbers
81 # ... but as.data.frame(x) will merge them back in, so nothing gets lost.
82 if (ncol(object) == 0)
83 #stop("only coords columns present: use SpatialPoints to create a points object")
84 return(SpatialPoints(cc))
85 } else
86 stripped = numeric(0)
87 SpatialPointsDataFrame(coords = cc, data = object, coords.nrs = stripped,
88 match.ID = FALSE)
89 }
90 )
91
92 .asWKT = FALSE
93 print.SpatialPointsDataFrame = function(x, ..., digits = getOption("digits"),
94 asWKT = .asWKT) {
95 #EJP, Fri May 21 12:40:59 CEST 2010
96 if (asWKT)
97 df = data.frame(asWKTSpatialPoints(x, digits), x@data)
98 else { # old style
99 cc = substring(paste(as.data.frame(
100 t(signif(coordinates(x), digits)))),2,999)
101 df = data.frame("coordinates" = cc, x@data)
102 }
103 row.names(df) = row.names(x@data)
104 print(df, ..., digits = digits)
105 }
106 setMethod("show", "SpatialPointsDataFrame", function(object) print(object))
107
108 dim.SpatialPointsDataFrame = function(x) dim(x@data)
109
110 as.data.frame.SpatialPointsDataFrame = function(x, ...) {
111 if (length([email protected]) > 0) {
112 maxi = max([email protected], (ncol(x@data) + ncol(x@coords)))
113 ret = list()
114 for (i in 1:ncol(x@coords))
115 ret[[[email protected][i]]] = x@coords[,i]
116 names(ret)[[email protected]] = dimnames(x@coords)[[2]]
117 idx.new = (1:maxi)[-([email protected])]
118 for (i in 1:ncol(x@data))
119 ret[[idx.new[i]]] = x@data[,i]
120 names(ret)[idx.new] = names(x@data)
121 ret = ret[unlist(lapply(ret, function(x) !is.null(x)))]
122 data.frame(ret, ...)
123 } else
124 data.frame(x@data, x@coords, ...)
125 }
126
127 setAs("SpatialPointsDataFrame", "data.frame", function(from)
128 as.data.frame.SpatialPointsDataFrame(from))
129
130 names.SpatialPointsDataFrame <- function(x) names(x@data)
131 "names<-.SpatialPointsDataFrame" <- function(x, value) {
132 checkNames(value)
133 names(x@data) = value
134 x
135 }
136
137 points.SpatialPointsDataFrame = function(x, y = NULL, ...)
138 points(as(x, "SpatialPoints"), ...)
139
140 text.SpatialPointsDataFrame = function(x, ...) {
141 lst = list(...)
142 if (!is.null(x$srt)) {
143 if (length(unique(x$srt)) == 1)
144 lst$srt = x$srt[1]
145 else { # print each label individually:
146 lapply(seq_len(length(x)), function(i) text(x[i,], ...))
147 return(invisible())
148 }
149 }
150 lst$x = coordinates(x)
151 if (!is.null(x$pos) && is.null(lst$pos))
152 lst$pos = x$pos
153 if (!is.null(x$offset) && is.null(lst$offset))
154 lst$offset = x$offset
155 if (!is.null(x$labels) && is.null(lst$labels))
156 lst$labels = parse(text = x$labels)
157 if (!is.null(x$adjx) && !is.null(x$adjy))
158 lst$adj = c(x$adjx, x$adjy)
159 do.call(text, lst)
160 }
161
162 row.names.SpatialPointsDataFrame <- function(x) {
163 ret = dimnames(slot(x, "coords"))[[1]]
164 if (is.null(ret))
165 ret = row.names(x@data)
166 ret
167 }
168
169 "row.names<-.SpatialPointsDataFrame" <- function(x, value) {
170 dimnames(slot(x, "coords"))[[1]] <- value
171 rownames(slot(x, "data")) <- value
172 x
173 }
174
175 setMethod("[", "SpatialPointsDataFrame", function(x, i, j, ..., drop = TRUE) {
176 missing.i = missing(i)
177 missing.j = missing(j)
178 nargs = nargs() # e.g., a[3,] gives 2 for nargs, a[3] gives 1.
179 if (missing.i && missing.j) {
180 i = TRUE
181 j = TRUE
182 } else if (missing.j && !missing.i) {
183 if (nargs == 2) {
184 j = i
185 i = TRUE
186 } else {
187 j = TRUE
188 }
189 } else if (missing.i && !missing.j)
190 i = TRUE
191 if (is.matrix(i))
192 stop("matrix argument not supported in SpatialPointsDataFrame selection")
193 if (is(i, "Spatial"))
194 i = !is.na(over(x, geometry(i)))
195 if (is.character(i))
196 i <- match(i, row.names(x))
197 if (any(is.na(i)))
198 stop("NAs not permitted in row index")
199 if (!isTRUE(j)) # i.e., we do some sort of column selection
200 [email protected] = numeric(0) # will move coordinate colums last
201 x@coords = x@coords[i, , drop = FALSE]
202 if (nrow(x@coords))
203 x@bbox = .bboxCoords(x@coords)
204 x@data = x@data[i, j, ..., drop = FALSE]
205 x
206 })
207
208 setMethod("split", "SpatialPointsDataFrame", split.data.frame)
209
210 setMethod("geometry", "SpatialPointsDataFrame",
211 function(obj) as(obj, "SpatialPoints"))
212
213 length.SpatialPointsDataFrame = function(x) { nrow(x@coords) }