1
.overDF = function(r, data, n, returnList, fn) {
1
.overDF = function(r, data, n, returnList, fn, ...) {
2
2
ret = lapply(1:n, function(x) data[r[[x]],,drop=FALSE])
5
fn = function(x) { x[1,,drop=FALSE] }
6
ret = do.call(rbind, lapply(ret, fn))
5
fn = function(x, ...) { x[1,,drop=FALSE] }
6
ret = do.call(rbind, lapply(ret, fn, ...))
7
7
# ret[match(1:n, ix),,drop=FALSE]
8
8
ret[is.nan(ret)] = NA
9
ret = as.data.frame(ret)
10
11
stopifnot(is.null(fn))
15
# we need to invert a list of indexes, i.e.
16
# list(c(1,4), c(2,4,5))
18
# list(c(1), c(2), integer(0), c(1,2), c(2))
19
# the expensive way is to form the full matrix, as in:
14
21
#.invert = function(lst, nr, nc) {
15
22
# stopifnot(nr == length(lst))
16
23
# m = matrix(FALSE, nr, nc)
18
25
# m[i,lst[[i]]] = TRUE
19
26
# lapply(1:nc, function(x) which(m[,x]))
28
# but the following does this more efficient:
22
30
.invert = function(x, nr, nc) {
23
31
stopifnot(nr == length(x)) # obsolete argument!
24
32
ret = cbind(rep(1:nr, times = sapply(x, length)), unlist(x))
25
33
ret = split(ret[,1], ret[,2])
34
# initialize return list with empty cells:
26
35
lst = lapply(1:nc, function(x) integer(0))
27
36
idx = as.integer(names(ret))
32
41
'%over%' = function(x,y) over(x,y)
43
overDFGeneric = function(x, y, returnList = FALSE, fn = NULL, ...) {
44
stopifnot(identical(proj4string(x),proj4string(y)))
45
r = over(x, geometry(y), returnList = TRUE)
46
ret = .overDF(r, y@data, length(x), returnList, fn, ...)
48
row.names(ret) = row.names(x)
35
53
signature(x = "SpatialPoints", y = "SpatialPoints"),
36
function(x, y, returnList = FALSE, fn = NULL) {
54
function(x, y, returnList = FALSE, fn = NULL, ...) {
37
55
stopifnot(identical(proj4string(x),proj4string(y)))
38
56
zd = zerodist2(x, y)
40
ret = lapply(1:length(x), integer(0))
58
ret = lapply(1:length(x), function(X) integer(0))
41
59
s = split(zd[,2],zd[,1])
42
60
ix = as.integer(names(s))
70
signature(x = "SpatialPoints", y = "SpatialPointsDataFrame"),
52
73
signature(x = "SpatialPoints", y = "SpatialPolygons"),
53
function(x, y, returnList = FALSE, fn = NULL) {
74
function(x, y, returnList = FALSE, fn = NULL, ...) {
54
75
stopifnot(identical(proj4string(x),proj4string(y)))
55
76
r = pointsInSpatialPolygons(x, y, returnList)
62
83
signature(x = "SpatialPoints", y = "SpatialPolygonsDataFrame"),
63
function(x, y, returnList = FALSE, fn = NULL) {
84
function(x, y, returnList = FALSE, fn = NULL, ...) {
64
85
stopifnot(identical(proj4string(x),proj4string(y)))
65
86
r = pointsInSpatialPolygons(x, geometry(y), returnList=TRUE)
66
87
r = .invert(r, length(y), length(x))
67
ret = .overDF(r, y@data, length(x), returnList, fn)
88
ret = .overDF(r, y@data, length(x), returnList, fn, ...)
68
89
#ret = y@data[r,,drop=FALSE]
70
91
row.names(ret) = row.names(x)
76
97
signature(x = "SpatialPolygons", y = "SpatialPoints"),
77
function(x, y, returnList = FALSE, fn = NULL) {
98
function(x, y, returnList = FALSE, fn = NULL, ...) {
78
99
stopifnot(identical(proj4string(x),proj4string(y)))
79
100
r = pointsInSpatialPolygons(geometry(y), geometry(x), TRUE)
87
108
signature(x = "SpatialPolygons", y = "SpatialPointsDataFrame"),
88
function(x, y, returnList = FALSE, fn = NULL) {
89
stopifnot(identical(proj4string(x),proj4string(y)))
91
r = over(x, geometry(y), returnList = TRUE)
92
ret = .overDF(r, y@data, length(x), returnList, fn)
94
row.names(ret) = row.names(x)
99
111
signature(x = "SpatialPolygons", y = "SpatialGridDataFrame"),
100
function(x, y, returnList = FALSE, fn = NULL) {
112
function(x, y, returnList = FALSE, fn = NULL, ...) {
101
113
stopifnot(identical(proj4string(x),proj4string(y)))
102
114
over(x, as(y, "SpatialPixelsDataFrame"), returnList = returnList,
107
119
setMethod("over", signature("SpatialPoints", "SpatialGrid"),
108
function(x, y, returnList = FALSE, fn = NULL) {
120
function(x, y, returnList = FALSE, fn = NULL, ...) {
109
121
stopifnot(identical(proj4string(x),proj4string(y)))
122
stopifnot(returnList == FALSE)
110
123
getGridIndex(coordinates(x), y@grid, all.inside = FALSE)
114
127
setMethod("over", signature("SpatialPoints", "SpatialGridDataFrame"),
115
function(x, y, returnList = FALSE, fn = NULL) {
128
function(x, y, returnList = FALSE, fn = NULL, ...) {
116
129
stopifnot(identical(proj4string(x),proj4string(y)))
117
#idx = over(geometry(y), x)
118
#ret = .overDF(idx, y@data, length(x), returnList, fn)
119
#row.names(ret) = row.names(x)
130
stopifnot(returnList == FALSE)
121
131
idx = over(x, geometry(y))
122
132
ret = y@data[idx,,drop=FALSE]
123
133
row.names(ret) = row.names(x)
128
138
setMethod("over", signature("SpatialPoints", "SpatialPixels"),
129
function(x, y, returnList = FALSE, fn = NULL) {
139
function(x, y, returnList = FALSE, fn = NULL, ...) {
130
140
stopifnot(identical(proj4string(x),proj4string(y)))
141
stopifnot(returnList == FALSE)
131
142
idx = getGridIndex(coordinates(x), y@grid, all.inside = FALSE)
132
143
idx = match(idx, y@grid.index)
137
148
setMethod("over", signature("SpatialPoints", "SpatialPixelsDataFrame"),
138
function(x, y, returnList = FALSE, fn = NULL) {
149
function(x, y, returnList = FALSE, fn = NULL, ...) {
139
150
stopifnot(identical(proj4string(x),proj4string(y)))
151
stopifnot(returnList == FALSE)
140
152
idx = over(x, geometry(y))
141
153
ret = y@data[idx,,drop=FALSE]
142
154
row.names(ret) = row.names(x)
159
aggregate.Spatial = function(x, by, FUN = mean, ...) {
162
by = as(by, "SpatialPolygons")
163
df = over(by, x, fn = FUN, ...)
164
addAttrToGeom(by0, df, match.ID = FALSE)