| 1 |
.overDF = function(r, data, n, returnList, fn, ...) { |
| 2 |
if (returnList == FALSE && is.null(fn)) |
| 3 |
ret = data[sapply(r, function(x) x[1]), , drop=FALSE] |
| 4 |
else { |
| 5 |
ret = lapply(1:n, function(x) data[r[[x]],,drop=FALSE]) # list of data.frames |
| 6 |
if (returnList == FALSE) { # apply fn: |
| 7 |
ret = do.call(rbind, # rbind each aggregated record |
| 8 |
lapply(ret, # apply to each data.frame in ret: |
| 9 |
function(x) { |
| 10 |
if (nrow(x) == 0) |
| 11 |
data.frame(lapply(x, function(xx) c(xx, NA))) |
| 12 |
else |
| 13 |
data.frame(lapply(x, fn, ...)) |
| 14 |
} |
| 15 |
) |
| 16 |
) |
| 17 |
ret[is.na(ret)] = NA # removes NaN's |
| 18 |
ret = as.data.frame(ret) |
| 19 |
} |
| 20 |
} |
| 21 |
ret |
| 22 |
} |
| 23 |
overDF_for_rgeos = .overDF # to be exported, for rgeos, and spacetime |
| 24 |
|
| 25 |
# we need to invert a list of indexes, i.e. |
| 26 |
# list(c(1,4), c(2,4,5)) |
| 27 |
# needs to become |
| 28 |
# list(c(1), c(2), integer(0), c(1,2), c(2)) |
| 29 |
# the expensive way is to form the full matrix, as in: |
| 30 |
# |
| 31 |
#.invert = function(lst, nr, nc) { |
| 32 |
# stopifnot(nr == length(lst)) |
| 33 |
# m = matrix(FALSE, nr, nc) |
| 34 |
# for (i in 1:nr) |
| 35 |
# m[i,lst[[i]]] = TRUE |
| 36 |
# lapply(1:nc, function(x) which(m[,x])) |
| 37 |
#} |
| 38 |
# but the following does this more efficient, memory-wise: |
| 39 |
.invert = function(x, nr, nc) { |
| 40 |
stopifnot(nr == length(x)) # obsolete argument! |
| 41 |
ret = cbind(rep(1:nr, times = sapply(x, length)), unlist(x)) |
| 42 |
ret = split(ret[,1], ret[,2]) |
| 43 |
# initialize return list with empty cells: |
| 44 |
lst = lapply(1:nc, function(x) integer(0)) |
| 45 |
idx = as.integer(names(ret)) |
| 46 |
lst[idx] = ret |
| 47 |
lst |
| 48 |
} |
| 49 |
|
| 50 |
'%over%' = function(x,y) over(x,y) |
| 51 |
|
| 52 |
# when changing this function, we also might want to change |
| 53 |
# overGeomGeomDF in rgeos, |
| 54 |
# ... and overDFGenericST in spacetime. |
| 55 |
overDFGeneric = function(x, y, returnList = FALSE, fn = NULL, ..., minDimension = -1) { |
| 56 |
stopifnot(identicalCRS(x, y)) |
| 57 |
r = over(x, geometry(y), returnList = TRUE, minDimension = minDimension) |
| 58 |
ret = .overDF(r, y@data, length(x), returnList, fn, ...) |
| 59 |
if (returnList) |
| 60 |
names(ret) = row.names(x) |
| 61 |
else |
| 62 |
row.names(ret) = row.names(x) |
| 63 |
ret |
| 64 |
} |
| 65 |
|
| 66 |
setMethod("over", |
| 67 |
signature(x = "SpatialPoints", y = "SpatialPoints"), |
| 68 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 69 |
stopifnot(identicalCRS(x, y)) |
| 70 |
zd = zerodist2(x, y) |
| 71 |
if (returnList) { |
| 72 |
ret = lapply(1:length(x), function(X) integer(0)) |
| 73 |
s = split(zd[,2],zd[,1]) |
| 74 |
ix = as.integer(names(s)) |
| 75 |
ret[ix] = s |
| 76 |
} else { |
| 77 |
ret = rep(as.integer(NA), length(x)) |
| 78 |
ret[zd[,1]] = zd[,2] |
| 79 |
} |
| 80 |
names(ret) = row.names(x) |
| 81 |
ret |
| 82 |
} |
| 83 |
) |
| 84 |
setMethod("over", |
| 85 |
signature(x = "SpatialPoints", y = "SpatialPolygons"), |
| 86 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 87 |
stopifnot(identicalCRS(x, y)) |
| 88 |
r = pointsInSpatialPolygons(x, y, returnList) |
| 89 |
if (returnList) |
| 90 |
r = .invert(r, length(y), length(x)) |
| 91 |
names(r) = row.names(x) |
| 92 |
r |
| 93 |
} |
| 94 |
) |
| 95 |
setMethod("over", |
| 96 |
signature(x = "SpatialPolygons", y = "SpatialPoints"), |
| 97 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 98 |
stopifnot(identicalCRS(x,y)) |
| 99 |
r = pointsInSpatialPolygons(geometry(y), geometry(x), TRUE) |
| 100 |
if (!returnList) |
| 101 |
r = sapply(r, function(x) x[1]) |
| 102 |
names(r) = row.names(x) |
| 103 |
r |
| 104 |
} |
| 105 |
) |
| 106 |
setMethod("over", |
| 107 |
signature(x = "SpatialGrid", y = "SpatialPoints"), |
| 108 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 109 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 110 |
) |
| 111 |
setMethod("over", |
| 112 |
signature(x = "SpatialGrid", y = "SpatialPolygons"), |
| 113 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 114 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 115 |
) |
| 116 |
setMethod("over", |
| 117 |
signature(x = "SpatialGrid", y = "SpatialPixels"), |
| 118 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 119 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 120 |
) |
| 121 |
setMethod("over", |
| 122 |
signature(x = "SpatialGrid", y = "SpatialGrid"), |
| 123 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 124 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 125 |
) |
| 126 |
setMethod("over", |
| 127 |
signature(x = "SpatialPolygons", y = "SpatialGrid"), |
| 128 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 129 |
over(x = x, y = as(y, "SpatialPoints"), returnList = returnList, fn = fn, ...) |
| 130 |
) |
| 131 |
setMethod("over", signature("SpatialPoints", "SpatialGrid"), |
| 132 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 133 |
stopifnot(identicalCRS(x,y)) |
| 134 |
idx = getGridIndex(coordinates(x), y@grid, all.inside = FALSE) |
| 135 |
r = .index2list(idx, returnList) |
| 136 |
names(r) = row.names(x) |
| 137 |
r |
| 138 |
} |
| 139 |
) |
| 140 |
setMethod("over", signature("SpatialPoints", "SpatialPixels"), |
| 141 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 142 |
stopifnot(identicalCRS(x,y)) |
| 143 |
idx = getGridIndex(coordinates(x), y@grid, all.inside = FALSE) |
| 144 |
idx = match(idx, [email protected]) |
| 145 |
r = .index2list(idx, returnList) |
| 146 |
names(r) = row.names(x) |
| 147 |
r |
| 148 |
} |
| 149 |
) |
| 150 |
setMethod("over", |
| 151 |
signature(x = "SpatialPoints", y = "SpatialPointsDataFrame"), |
| 152 |
overDFGeneric) |
| 153 |
setMethod("over", |
| 154 |
signature(x = "SpatialPoints", y = "SpatialPolygonsDataFrame"), |
| 155 |
overDFGeneric) |
| 156 |
setMethod("over", |
| 157 |
signature(x = "SpatialGrid", y = "SpatialPointsDataFrame"), |
| 158 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 159 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 160 |
) |
| 161 |
setMethod("over", |
| 162 |
signature(x = "SpatialGrid", y = "SpatialPolygonsDataFrame"), |
| 163 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 164 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 165 |
) |
| 166 |
setMethod("over", |
| 167 |
signature(x = "SpatialGrid", y = "SpatialPixelsDataFrame"), |
| 168 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 169 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 170 |
) |
| 171 |
setMethod("over", |
| 172 |
signature(x = "SpatialGrid", y = "SpatialGridDataFrame"), |
| 173 |
function(x, y, returnList = FALSE, fn = NULL, ...) |
| 174 |
over(as(x, "SpatialPoints"), y = y, returnList = returnList, fn = fn, ...) |
| 175 |
) |
| 176 |
setMethod("over", |
| 177 |
signature(x = "SpatialPolygons", y = "SpatialPointsDataFrame"), |
| 178 |
overDFGeneric) |
| 179 |
setMethod("over", |
| 180 |
signature(x = "SpatialPolygons", y = "SpatialGridDataFrame"), |
| 181 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 182 |
stopifnot(identicalCRS(x,y)) |
| 183 |
over(x, as(y, "SpatialPixelsDataFrame"), returnList = returnList, |
| 184 |
fn = fn, ...) |
| 185 |
} |
| 186 |
) |
| 187 |
setMethod("over", signature("SpatialPoints", "SpatialGridDataFrame"), |
| 188 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 189 |
stopifnot(identicalCRS(x,y)) |
| 190 |
idx = over(x, geometry(y)) |
| 191 |
ret = y@data[idx,,drop=FALSE] |
| 192 |
row.names(ret) = row.names(x) |
| 193 |
.index2list(ret, returnList) |
| 194 |
} |
| 195 |
) |
| 196 |
setMethod("over", signature("SpatialPoints", "SpatialPixelsDataFrame"), |
| 197 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 198 |
stopifnot(identicalCRS(x,y)) |
| 199 |
idx = over(x, geometry(y)) |
| 200 |
ret = y@data[idx,,drop=FALSE] |
| 201 |
row.names(ret) = row.names(x) |
| 202 |
.index2list(ret, returnList) |
| 203 |
} |
| 204 |
) |
| 205 |
|
| 206 |
setMethod("over", signature("Spatial", "Spatial"), # catch remaining: |
| 207 |
function(x, y, returnList = FALSE, fn = NULL, ...) { |
| 208 |
warning("No rgeos support in sp from October 2023;\nsee https://r-spatial.org/r/2023/05/15/evolution4.html") |
| 209 |
.Deprecated("sf::st_intersects or sf::aggregate") |
| 210 |
# if (!requireNamespace("rgeos", quietly = TRUE)) |
| 211 |
stop("use sf or terra functions") |
| 212 |
if (is(x, "SpatialMultiPoints") || is(y, "SpatialMultiPoints")) |
| 213 |
overMultiPoints(x, y, returnList = returnList, fn = fn, ...) |
| 214 |
else |
| 215 |
over(x, y, returnList = returnList, fn = fn, ...) # rgeos methods |
| 216 |
} |
| 217 |
) |
| 218 |
|
| 219 |
overMultiPoints = function(x, y, returnList, fn, ...) { |
| 220 |
if (is(x, "SpatialMultiPoints")) { |
| 221 |
x = as(x, "SpatialPoints") |
| 222 |
dimnames(x@coords)[[1]] = attr(x@coords, "groupIndex") # rgeos abuse |
| 223 |
} |
| 224 |
if (is(y, "SpatialMultiPoints")) { |
| 225 |
if (is(y, "SpatialMultiPointsDataFrame")) { |
| 226 |
yy = as(y, "SpatialPointsDataFrame") |
| 227 |
yy@data = y@data # strong sp abuse - yy no longer validates! |
| 228 |
y = yy |
| 229 |
} else |
| 230 |
y = as(y, "SpatialPoints") |
| 231 |
dimnames(y@coords)[[1]] = attr(y@coords, "groupIndex") # rgeos abuse |
| 232 |
} |
| 233 |
# if (!requireNamespace("rgeos", quietly = TRUE)) |
| 234 |
stop("use sf or terra functions") |
| 235 |
# if ("data" %in% slotNames(y)) |
| 236 |
# rgeos::overGeomGeomDF(x, y, returnList = returnList, fn = fn, ...) |
| 237 |
# else |
| 238 |
# rgeos::overGeomGeom(x, y, returnList = returnList, fn = fn, ...) |
| 239 |
|
| 240 |
} |
| 241 |
|
| 242 |
.index2list = function(x, returnList) { |
| 243 |
if (returnList) { |
| 244 |
l = lapply(1:length(x), function(x) { integer(0) }) |
| 245 |
notNA = !is.na(x) |
| 246 |
l[notNA] = x[notNA] |
| 247 |
l |
| 248 |
} else |
| 249 |
x |
| 250 |
} |