Debian Codegraph @

R/SpatialMultiPointsDataFrame-methods.R

raw
1 "SpatialMultiPointsDataFrame" = function(coords, data,
2 proj4string = CRS(as.character(NA)), match.ID, bbox = NULL) {
3
4 if (is(coords, "SpatialMultiPoints") &&
5 !is.na(slot(proj4string, "projargs")))
6 warning("If the coords argument is a SpatialMultiPoints object, set its CRS first;\n the proj4string argument to this function is ignored.")
7
8 if (!is(coords, "SpatialMultiPoints"))
9 coords = SpatialMultiPoints(coords, proj4string = proj4string, bbox = bbox)
10 mtch = NULL
11 cc.ID = names(coords@coords)
12 if (missing(match.ID)) { # sort it out:
13 if (is.null(cc.ID) || any(cc.ID == ""))
14 match.ID = FALSE # nothing to match to!
15 else {
16 mtch = match(cc.ID, row.names(data))
17 match.ID = !any(is.na(mtch)) # && length(unique(mtch)) == nrow(data)
18 if (match.ID && any(mtch != 1:nrow(data)))
19 warning("forming a SpatialMultiPointsDataFrame based on maching IDs, not on record order. Use match.ID = FALSE to match on record order")
20 }
21 } else if (is.character(match.ID)) {
22 row.names(data) = data[, match.ID[1]]
23 match.ID = TRUE
24 }
25
26 if (match.ID) {
27 if (!is.null(cc.ID) && is(data, "data.frame")) { # match ID:
28 if (is.null(mtch))
29 mtch = match(cc.ID, row.names(data))
30 if (any(is.na(mtch)))
31 stop("row.names of data and coords do not match")
32 if (length(unique(mtch)) != nrow(data))
33 stop("row.names of data and dimnames of coords do not match")
34 data = data[mtch, , drop = FALSE]
35 }
36 }
37 if (is.character(attr(data, "row.names"))) # i.e., data has "real" row names
38 names(coords@coords) = row.names(data)
39 new("SpatialMultiPointsDataFrame", coords, data = data)
40 }
41
42 # setMethod("coordinates", "SpatialMultiPointsDataFrame", function(obj) obj@coords)
43
44 setMethod("addAttrToGeom", signature(x = "SpatialMultiPoints", y = "data.frame"),
45 function(x, y, match.ID, ...)
46 SpatialMultiPointsDataFrame(x, y, match.ID = match.ID, ...)
47 )
48
49 .asWKT = FALSE
50 print.SpatialMultiPointsDataFrame = function(x, ..., digits = getOption("digits"),
51 asWKT = .asWKT) {
52 if (asWKT)
53 df = data.frame(asWKTSpatialMultiPoints(x, digits), x@data)
54 else { # old style
55 cc = substring(paste(as.data.frame(
56 t(signif(coordinates(x), digits)))),2,999)
57 ix = rep(seq_along(x@coords), sapply(x@coords, nrow))
58 df = data.frame("coordinates" = cc, x@data[ix, , drop=FALSE])
59 }
60 print(df, ..., digits = digits)
61 }
62 setMethod("show", "SpatialMultiPointsDataFrame", function(object) print(object))
63
64 dim.SpatialMultiPointsDataFrame = function(x) dim(x@data)
65
66 as.data.frame.SpatialMultiPointsDataFrame = function(x, ...) {
67 l = sapply(x@coords, nrow)
68 ix = rep(1:length(l), l)
69 data.frame(coordinates(x), index = ix, x@data[ix,,drop=FALSE], ...)
70 }
71
72 setAs("SpatialMultiPointsDataFrame", "data.frame", function(from)
73 as.data.frame.SpatialMultiPointsDataFrame(from))
74
75 names.SpatialMultiPointsDataFrame <- function(x) names(x@data)
76 "names<-.SpatialMultiPointsDataFrame" <- function(x, value) {
77 checkNames(value)
78 names(x@data) = value
79 x
80 }
81
82 points.SpatialMultiPointsDataFrame = function(x, y = NULL, ...)
83 points(as(x, "SpatialMultiPoints"), ...)
84
85 text.SpatialMultiPointsDataFrame = function(x, ...) {
86 lst = list(x = coordinates(x), ...)
87 if (!is.null(x$pos) && is.null(lst$pos))
88 lst$pos = x$pos
89 if (!is.null(x$offset) && is.null(lst$offset))
90 lst$offset = x$offset
91 if (!is.null(x$labels) && is.null(lst$labels))
92 lst$labels = parse(text = x$lab)
93 do.call(text, lst)
94 }
95
96 #row.names.SpatialMultiPointsDataFrame <- function(x) {
97 # ret = dimnames(slot(x, "coords"))[[1]]
98 # if (is.null(ret))
99 # ret = row.names(x@data)
100 # ret
101 #}
102
103 #"row.names<-.SpatialMultiPointsDataFrame" <- function(x, value) {
104 # dimnames(slot(x, "coords"))[[1]] <- value
105 # rownames(slot(x, "data")) <- value
106 # x
107 #}
108
109 setMethod("[", "SpatialMultiPointsDataFrame", function(x, i, j, ..., drop = TRUE) {
110 missing.i = missing(i)
111 missing.j = missing(j)
112 nargs = nargs() # e.g., a[3,] gives 2 for nargs, a[3] gives 1.
113 if (missing.i && missing.j) {
114 i = TRUE
115 j = TRUE
116 } else if (missing.j && !missing.i) {
117 if (nargs == 2) {
118 j = i
119 i = TRUE
120 } else {
121 j = TRUE
122 }
123 } else if (missing.i && !missing.j)
124 i = TRUE
125 if (is.matrix(i))
126 stop("matrix argument not supported in SpatialMultiPointsDataFrame selection")
127 if (is(i, "Spatial"))
128 i = !is.na(over(x, geometry(i)))
129 if (is.character(i))
130 i <- match(i, row.names(x))
131 if (any(is.na(i)))
132 stop("NAs not permitted in row index")
133 x@coords = x@coords[i]
134 if (length(x@coords))
135 x@bbox = .bboxMultiCoords(x@coords)
136 x@data = x@data[i, j, ..., drop = FALSE]
137 x
138 })
139
140 # setMethod("split", "SpatialMultiPointsDataFrame", split.data.frame)
141
142 setMethod("geometry", "SpatialMultiPointsDataFrame",
143 function(obj) as(obj, "SpatialMultiPoints"))
144
145 length.SpatialMultiPointsDataFrame = function(x) { length(x@coords) }
146
147 setAs("SpatialMultiPointsDataFrame", "SpatialPointsDataFrame",
148 function(from) {
149 l = sapply(from@coords, nrow)
150 ix = rep(1:length(l), l)
151 new("SpatialPointsDataFrame", as(geometry(from), "SpatialPoints"),
152 data = from@data[ix,,drop=FALSE], coords.nrs = numeric(0))
153 }
154 )