-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkLookup.R
38 lines (34 loc) · 1.09 KB
/
mkLookup.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"mkLookup" <- function (x, by.segment = TRUE)
{
if (any(is.na(x$z))) stop("NAs in grid data")
if (!by.segment & !is.logical(x$z)) stop("grid data must be a logical matrix")
csize <- c(diff(x$x[1:2]), diff(x$y[1:2]))
dimXY <- dim(x$z)
binArray <- FALSE
if (length(dimXY) == 3 & by.segment) {
bsegs <- (1:(dimXY[3] * 31)%/%31) * prod(dimXY[1:2])
dimXY <- dimXY[1:2]
binArray <- TRUE
}
function(xy, segment = 1:nrow(xy)) {
xs <- xy[, 1]
ys <- xy[, 2]
i <- round((1/diff(x$x[1:2]))*(xs - x$x[1])+1)
j <- round((1/diff(x$y[1:2]))*(ys - x$y[1])+1)
f <- vector(mode(x$z), length(xs))
k <- (i > 0 & j > 0 & i <= dimXY[1] & j <= dimXY[2])
n <- nrow(xy)
if (any(k)) {
if (binArray) {
f[k] <- bits(x$z[((j[k] - 1) * dim(x$z)[1] +
i[k]) + bsegs[1:n][k]], (segment[k] - 1)%%31)
f == 1
}
else {
f[k] <- x$z[cbind(i[k], j[k])]
f == 1
}
}
else FALSE
}
}