Debian Codegraph @

R/SpatialMultiPoints-methods.R

raw
1 "SpatialMultiPoints" = function(coords, proj4string = CRS(as.character(NA)),
2 bbox = NULL) {
3 coords = lapply(coords, coordinates) # checks numeric mode
4 #colNames = dimnames(coords[[1]])[[2]]
5 #if (is.null(colNames))
6 # colNames = paste("coords.x", 1:(dim(coords)[2]), sep = "")
7 #rowNames = dimnames(coords)[[1]]
8 #dimnames(coords) = list(rowNames, colNames) # preserve row names if non-NULL
9 if (is.null(bbox))
10 bbox <- .bboxMultiCoords(coords)
11 new("SpatialMultiPoints", coords = coords, bbox = bbox, proj4string = proj4string)
12 }
13
14 .bboxMultiCoords = function(coords) {
15 coords = do.call(rbind, coords)
16 stopifnot(nrow(coords) > 0)
17 bbox = t(apply(coords, 2, range))
18 dimnames(bbox)[[2]] = c("min", "max")
19 as.matrix(bbox)
20 }
21
22 asWKTSpatialMultiPoints = function(x, digits = getOption("digits")) {
23 data.frame(geometry = paste("MULTIPOINT (",
24 sapply(x@coords, function(x)
25 paste(apply(signif(x, digits = digits), 1, paste, collapse = " "), collapse = ","))
26 , ")" , sep = ""))
27 }
28
29 "print.SpatialMultiPoints" <- function(x, ..., digits = getOption("digits"),
30 asWKT = .asWKT)
31 {
32 cat("SpatialMultiPoints:\n")
33 if (asWKT)
34 print(asWKTSpatialMultiPoints(x, digits))
35 else
36 print(x@coords)
37 pst <- paste(strwrap(paste(
38 "Coordinate Reference System (CRS) arguments:",
39 proj4string(x))), collapse="\n")
40 cat(pst, "\n")
41 }
42 setMethod("show", "SpatialMultiPoints", function(object) print.SpatialMultiPoints(object))
43
44 plot.SpatialMultiPoints = function(x, pch = 3, axes = FALSE, add = FALSE,
45 xlim = NULL, ylim = NULL, ..., setParUsrBB=FALSE, cex = 1, col = 1,
46 lwd = 1, bg = 1)
47 {
48 if (! add)
49 plot(as(x, "Spatial"), axes = axes, xlim = xlim, ylim = ylim,
50 ..., setParUsrBB=setParUsrBB)
51 cc = coordinates(x)
52 n = length(x)
53 l = sapply(x@coords, nrow)
54 points(cc[,1], cc[,2],
55 pch = rep(rep(pch, length.out = n), l),
56 cex = rep(rep(cex, length.out = n), l),
57 col = rep(rep(col, length.out = n), l),
58 lwd = rep(rep(lwd, length.out = n), l),
59 bg = rep(rep(bg, length.out = n), l))
60 }
61 setMethod("plot", signature(x = "SpatialMultiPoints", y = "missing"),
62 function(x,y,...) plot.SpatialMultiPoints(x,...))
63
64 points.SpatialMultiPoints = function(x, y = NULL, ...) plot(x, add = TRUE, ...)
65
66 setMethod("coordinates", "SpatialMultiPoints",
67 function(obj) {
68 if (length(obj@coords) == 0)
69 matrix(nrow = 0, ncol = 2)
70 else
71 matrix(do.call(rbind, obj@coords), ncol = ncol(obj@coords[[1]]),
72 dimnames = list(rep(1:length(obj@coords), sapply(obj@coords, nrow))))
73 }
74 )
75
76 as.data.frame.SpatialMultiPoints = function(x, row.names, optional, ...) data.frame(coordinates(x@coords))
77
78 setAs("SpatialMultiPoints", "data.frame", function(from) as.data.frame(from))
79
80 row.names.SpatialMultiPoints <- function(x) {
81 ret = names(slot(x, "coords"))
82 if (is.null(ret))
83 ret = seq_len(length(slot(x, "coords")))
84 ret
85 }
86 names.SpatialMultiPoints <- row.names.SpatialMultiPoints
87
88 "row.names<-.SpatialMultiPoints" <- function(x, value) {
89 names(slot(x, "coords")) <- value
90 x
91 }
92
93 setMethod("[", "SpatialMultiPoints", function(x, i, j, ..., drop = TRUE) {
94 if (!missing(j))
95 warning("j index ignored")
96 if (is.character(i))
97 i <- match(i, row.names(x))
98 else if (is(i, "Spatial"))
99 i = !is.na(over(x, geometry(i)))
100 if (any(is.na(i)))
101 stop("NAs not permitted in row index")
102 x@coords = x@coords[i]
103 if (drop && length(x@coords))
104 x@bbox = .bboxMultiCoords(x@coords)
105 x
106 })
107
108 setMethod("coordnames", signature(x = "SpatialMultiPoints"),
109 function(x) dimnames(x@coords[[1]])[[2]])
110
111 setReplaceMethod("coordnames", signature(x = "SpatialMultiPoints", value = "character"),
112 function(x, value) {
113 dimnames(x@bbox)[[1]] = value
114 dimnames(x@coords[[1]])[[2]] = value
115 x
116 }
117 )
118
119 length.SpatialMultiPoints = function(x) length(x@coords)
120
121 setMethod("$", "SpatialMultiPoints",
122 function(x, name) {
123 if (name %in% coordnames(x))
124 return(SpatialMultiPoints(lapply(x@coords, function(x) x[,name])))
125 if (!("data" %in% slotNames(x))) # we're not a SpatialMultiPointsDataFrame
126 stop("no $ method for object without attributes")
127 x@data[[name]]
128 }
129 )
130
131 setAs("SpatialMultiPoints", "SpatialPoints",
132 function(from) {
133 cc = coordinates(from)
134 attr(cc, "groupIndex") = dimnames(cc)[[1]]
135 dimnames(cc)[[1]] = NULL # if we'd retain them, rgeos would interpret the SpatialPoints as SpatialMultiPoints
136 SpatialPoints(cc, from@proj4string, from@bbox)
137 }
138 )
139
140 split.SpatialPoints = function(x, f, drop = FALSE, ...) {
141 lst = lapply(split(as.data.frame(coordinates(x)), f), as.matrix)
142 SpatialMultiPoints(lst, x@proj4string, x@bbox)
143 }