Debian Codegraph @

R/SpatialPoints-methods.R

raw
1 "SpatialPoints" = function(coords, proj4string = CRS(as.character(NA)),
2 bbox = NULL) {
3 coords = coordinates(coords) # checks numeric mode
4 colNames = dimnames(coords)[[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 <- .bboxCoords(coords)
11 new("SpatialPoints", coords = coords, bbox = bbox,
12 proj4string = proj4string) # transpose bbox?
13 }
14
15 .bboxCoords = function(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 setMethod("coordinates", "matrix",
23 function(obj) {
24 if (!is.numeric(obj))
25 stop("cannot derive coordinates from non-numeric matrix")
26 storage.mode(obj) <- "double"
27 if (any(is.na(obj)))
28 stop("NA values in coordinates")
29 if (any(!is.finite(obj)))
30 stop("non-finite coordinates")
31 obj
32 }
33 )
34 setMethod("coordinates", "data.frame", function(obj)coordinates(as.matrix(obj)))
35
36 setMethod("coordinates", "list", function(obj) coordinates(as.data.frame(obj)))
37
38 asWKTSpatialPoints = function(x, digits = getOption("digits")) {
39 data.frame(geometry = paste("POINT(",unlist(lapply(data.frame(
40 t(signif(coordinates(x),digits = digits))),
41 paste, collapse=" ")),")",sep=""))
42 }
43
44 "print.SpatialPoints" <- function(x, ..., digits = getOption("digits"),
45 asWKT = .asWKT)
46 {
47 cat("SpatialPoints:\n")
48 if (asWKT)
49 print(asWKTSpatialPoints(x, digits))
50 else
51 print(x@coords)
52 pst <- paste(strwrap(paste(
53 "Coordinate Reference System (CRS) arguments:",
54 slot(slot(x, "proj4string"), "projargs"))), collapse="\n")
55 cat(pst, "\n")
56 }
57 setMethod("show", "SpatialPoints", function(object) print.SpatialPoints(object))
58
59 plot.SpatialPoints = function(x, pch = 3, axes = FALSE, add = FALSE,
60 xlim = NULL, ylim = NULL, ..., setParUsrBB=FALSE, cex = 1, col = 1,
61 lwd = 1, bg = 1)
62 {
63 if (! add)
64 plot(as(x, "Spatial"), axes = axes, xlim = xlim, ylim = ylim,
65 ..., setParUsrBB=setParUsrBB)
66 cc = coordinates(x)
67 points(cc[,1], cc[,2], pch = pch, cex = cex, col = col, lwd = lwd, bg = bg)
68 }
69 setMethod("plot", signature(x = "SpatialPoints", y = "missing"),
70 function(x,y,...) plot.SpatialPoints(x,...))
71
72 points.SpatialPoints = function(x, y = NULL, ...) points(coordinates(x), ...)
73
74 setMethod("coordinates", "SpatialPoints", function(obj) obj@coords)
75
76 as.data.frame.SpatialPoints = function(x, row.names, optional, ...) data.frame(x@coords)
77
78 setAs("SpatialPoints", "data.frame", function(from) as.data.frame(from))
79
80 row.names.SpatialPoints <- function(x) {
81 ret = dimnames(slot(x, "coords"))[[1]]
82 if (is.null(ret))
83 seq_len(nrow(slot(x, "coords")))
84 else
85 ret
86 }
87
88 "row.names<-.SpatialPoints" <- function(x, value) {
89 dimnames(slot(x, "coords"))[[1]] <- value
90 x
91 }
92
93 setMethod("[", "SpatialPoints", 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, , drop = FALSE]
103 if (drop && nrow(x@coords))
104 x@bbox = .bboxCoords(x@coords)
105 x
106 })
107
108 setMethod("coordnames", signature(x = "SpatialPoints"),
109 function(x) dimnames(x@coords)[[2]])
110
111 setReplaceMethod("coordnames", signature(x = "SpatialPoints", value = "character"),
112 function(x, value) {
113 dimnames(x@bbox)[[1]] = value
114 dimnames(x@coords)[[2]] = value
115 x
116 }
117 )
118
119 length.SpatialPoints = function(x) { nrow(x@coords) }
120
121 setMethod("$", "SpatialPoints",
122 function(x, name) {
123 if (name %in% coordnames(x))
124 return(x@coords[,name])
125 if (!("data" %in% slotNames(x)))
126 stop("no $ method for object without attributes")
127 x@data[[name]]
128 }
129 )