| 1 |
point.in.polygon = function(point.x, point.y, pol.x, pol.y, |
| 2 |
mode.checked=FALSE) { |
| 3 |
if (mode.checked) res <- .Call(R_point_in_polygon_sp, point.x, |
| 4 |
point.y, pol.x, pol.y) |
| 5 |
else res <- .Call(R_point_in_polygon_sp, as.numeric(point.x), |
| 6 |
as.numeric(point.y), as.numeric(pol.x), as.numeric(pol.y)) |
| 7 |
res |
| 8 |
} |
| 9 |
|
| 10 |
pointsInPolygon = function(pts, Polygon, |
| 11 |
mode.checked=FALSE) { |
| 12 |
pts = coordinates(pts) |
| 13 |
cc = slot(Polygon, "coords") |
| 14 |
point.in.polygon(pts[,1], pts[,2], cc[,1], cc[,2], |
| 15 |
mode.checked=mode.checked) |
| 16 |
} |
| 17 |
|
| 18 |
pointsInPolygons = function(pts, Polygons, #which = FALSE, |
| 19 |
mode.checked=FALSE) { |
| 20 |
rings = slot(Polygons, "Polygons") |
| 21 |
res = matrix(unlist(lapply(rings, function(x, pts) |
| 22 |
pointsInPolygon(pts, x, mode.checked=mode.checked), |
| 23 |
pts = pts)), ncol=length(rings)) |
| 24 |
res <- res > 0 |
| 25 |
# holes <- sapply(rings, function(y) slot(y, "hole")) |
| 26 |
# areas <- sapply(rings, function(x) slot(x, "area")) |
| 27 |
# if (any(holes) && any(res[,holes])) { |
| 28 |
# holerows <- which(res[,holes,drop=FALSE], arr.ind=TRUE)[,1] |
| 29 |
# odd <- rowSums(res[holerows,,drop=FALSE])%%2 != 0 |
| 30 |
# for (i in seq(along = holerows)) { |
| 31 |
# in_p <- which.min(areas[res[holerows[i],,drop=FALSE]]) |
| 32 |
# res[holerows[i],] <- FALSE |
| 33 |
# if (odd[i]) res[holerows[i], in_p] <- TRUE |
| 34 |
# } |
| 35 |
# res[,holes] <- FALSE |
| 36 |
# } |
| 37 |
# revised 100716 |
| 38 |
ret <- rowSums(res) %% 2 != 0 |
| 39 |
# ret <- apply(res, 1, any) |
| 40 |
# if (which) { |
| 41 |
# reta <- integer(length(ret)) |
| 42 |
# for (i in seq(along = ret)) { |
| 43 |
# if (ret[i]) reta[i] <- which(res[i,]) |
| 44 |
# else reta[i] <- as.integer(NA) |
| 45 |
# } |
| 46 |
# ret <- reta |
| 47 |
# } |
| 48 |
ret |
| 49 |
} |
| 50 |
|
| 51 |
pointsInSpatialPolygons0 = function(pts, SpPolygons) { |
| 52 |
sr = slot(SpPolygons, "polygons") |
| 53 |
res = lapply(sr, function(x, pts) pointsInPolygons(pts, x), pts = pts) |
| 54 |
#ret = rep(as.numeric(NA), nrow(coordinates(pts))) |
| 55 |
#for (i in seq(along = res)) |
| 56 |
# ret[res[[i]] > 0] = i |
| 57 |
#apply(do.call(rbind, res), 2, which) |
| 58 |
do.call(rbind, res) |
| 59 |
} |
| 60 |
|
| 61 |
pointsInSpatialPolygons = function(pts, SpPolygons, returnList = FALSE) { |
| 62 |
pls = slot(SpPolygons, "polygons") |
| 63 |
lb <- lapply(pls, function(x) as.double(bbox(x))) |
| 64 |
cpts <- coordinates(pts) |
| 65 |
storage.mode(cpts) <- "double" |
| 66 |
mode.checked <- storage.mode(cpts) == "double" |
| 67 |
cand <- .Call(tList, .Call(pointsInBox, lb, cpts[,1], cpts[,2]), |
| 68 |
as.integer(length(pls))) |
| 69 |
# rm(cand0) |
| 70 |
# gc(verbose=FALSE) |
| 71 |
res <- pointsInPolys2(pls, cand, cpts, mode.checked=mode.checked, |
| 72 |
returnList = returnList) |
| 73 |
res |
| 74 |
} |
| 75 |
|
| 76 |
pointsInPolys2 <- function(pls, cand, pts, mode.checked=FALSE, |
| 77 |
returnList = FALSE) { |
| 78 |
n <- nrow(pts) |
| 79 |
if (returnList) |
| 80 |
#res = sapply(1:length(pls), function(x) integer(0)) |
| 81 |
res <- rep.int(list(integer()), length(pls)) |
| 82 |
else |
| 83 |
res <- rep(as.integer(NA), n) |
| 84 |
# print(cand) |
| 85 |
for (i in seq_along(cand)) { |
| 86 |
candi <- cand[[i]] |
| 87 |
if (length(candi) > 0) { |
| 88 |
ptsi <- pts[candi,,drop=FALSE] |
| 89 |
ret <- pointsInPolygons(ptsi, pls[[i]], mode.checked=mode.checked) |
| 90 |
#for (j in seq(along=candi)) { |
| 91 |
# jj <- candi[j] |
| 92 |
# if (is.na(res[jj])) |
| 93 |
# res[jj] <- ifelse(ret[j], i, as.integer(NA)) |
| 94 |
#} |
| 95 |
if (returnList) |
| 96 |
res[[i]] = candi[ret] |
| 97 |
else |
| 98 |
res[candi[ret]] = i |
| 99 |
} |
| 100 |
} |
| 101 |
res |
| 102 |
} |