r - 使用 R 的多个多边形中的点
问题描述
目前我有两个data.frame,一个是多边形(poly.x, poly.y, enum)
,一个(pt.x, pt.y)
是多边形enum
的id。我试图确定哪些点属于哪些多边形,所以我得到了一个 data.frame (pt.x, pt.y, enum)
。
我的第一次尝试使用point.in.polygon
包sp
和lapply
函数来查找该点所属的多边形。虽然我的代码有效,但在大型数据集上需要很长时间。
我的第二次尝试over
也从sp
包中使用,由关于 gis stackexchange 的问题拼凑而成。虽然它要快得多,但我似乎无法从中获得正确的输出,over
因为它是1
s 和NA
s 的数据帧。
下面我包含了一个简化的工作示例(npoly
可以更改以测试不同方法的速度)以及我的工作尝试使用sp::point.in.polygon
和我sp::over
尝试的无意义输出。只要速度快,我不会担心最终使用哪种方法。
任何帮助将非常感激!
#-------------------------------------------
# Libraries
library(ggplot2) # sample plots
library(dplyr) # bind_rows(), etc
library(sp) # spatial data
# Sample data
npoly = 100
# polygons
localpolydf <- data.frame(
x = rep(c(0, 1, 1, 0), npoly) + rep(0:(npoly-1), each = 4),
y = rep(c(0, 0, 1, 1), npoly),
enum = rep(1:npoly, each = 4))
# points
offsetdf <- data.frame(
x = seq(min(localpolydf$x) - 0.5, max(localpolydf$x) + 0.5, by = 0.5),
y = runif(npoly*2 + 3, 0, 1))
# Sample plot
ggplot() +
geom_polygon(aes(x, y, group = enum),
localpolydf, fill = NA, colour = "black") +
geom_point(aes(x, y), offsetdf)
#-------------------------------------------
# Dplyr and lapply solution for point.in.polygon
ptm <- proc.time() # Start timer
# create lists
offsetlist <- split(offsetdf, rownames(offsetdf))
polygonlist <- split(localpolydf, localpolydf$enum)
# lapply over each pt in offsetlist
pts <- lapply(offsetlist, function(pt) {
# lapply over each polygon in polygonlist
ptpoly <- lapply(polygonlist, function(poly) {
data.frame(
enum = poly$enum[1],
ptin = point.in.polygon(pt[1,1], pt[1,2], poly$x, poly$y))
})
ptpoly <- bind_rows(ptpoly) %>% filter(ptin != 0)
if (nrow(ptpoly) == 0) return(data.frame(x = pt$x, y = pt$y, enum = NA, ptin = NA))
ptpoly$x = pt$x
ptpoly$y = pt$y
return(ptpoly[c("x", "y", "enum", "ptin")])
})
pts_apply <- bind_rows(pts)
proc.time() - ptm # end timer
#-------------------------------------------
# Attempted sp solution for over
ptm <- proc.time() # Start timer
# Split the dataframe into a list based on enum and then remove enum from df in the list
polygonlist <- split(localpolydf, localpolydf$enum)
polygonlist <- lapply(polygonlist, function(x) x[,c("x", "y")])
# Convert the list to Polygon, then create a Polygons object
polygonsp <- sapply(polygonlist, Polygon)
polygonsp <- Polygons(polygonsp, ID = 1)
polygonsp <- SpatialPolygons(list(polygonsp))
plot(polygonsp)
# Convert points to coordinates
offsetps <- offsetdf
coordinates(offsetps) <- ~x+y
points(offsetps$x, offsetps$y)
# Determine polygons points are in
pts_sp <- over(offsetps, polygonsp)
proc.time() - ptm # end timer
#===========================================
# Output
# Apply: point.in.polygon
> head(pts_apply)
x y enum ptin
1 -0.5 0.2218138 NA NA
2 4.0 0.9785541 4 2
3 4.0 0.9785541 5 2
4 49.0 0.3971479 49 2
5 49.0 0.3971479 50 2
6 49.5 0.1177206 50 1
user system elapsed
4.434 0.002 4.435
# SP: over
> head(pts_sp)
1 2 3 4 5 6
NA 1 1 NA 1 NA
user system elapsed
0.048 0.000 0.047
解决方案
推荐阅读
- python - 跨多个进程的共享字典未更新
- python - 如何修复python中的“名称'self'未定义”错误?
- visual-studio - Angular 项目无法在具有 Visual Studio 2019 的 docker 上运行
- javascript - 去抖功能单元测试
- c# - 如何访问嵌套命名空间以避免在 .NET 中完全限定
- python - 如果在 Python 3.7 中运行超过一次,函数将不再起作用
- python - 按值排序字典,然后按键
- c++ - 确定每次更改源文件时重新配置 CMake 的原因
- python - 识别其中存在特定字符的文件并重命名文件
- python - 如何在 sklearn 中将预测与输入数据测试结合起来