gpt4 book ai didi

r - 大数据集的空间匹配

转载 作者:行者123 更新时间:2023-12-04 02:23:10 28 4
gpt4 key购买 nike

我有一个大约有 100000 个点的数据集和另一个大约有 3000 个多边形的数据集。对于每个点,我需要找到最近的多边形(空间匹配)。多边形内的点应与该多边形匹配。

计算所有对距离是可行的,但需要的时间比必要的要长一些。是否有 R 包可以使用空间索引解决此类匹配问题?

我知道 sp包和over函数,但文档没有说明任何关于索引的信息。

最佳答案

您可以尝试使用 gDistance rgeos 中的函数包为此。作为一个例子,看看下面的例子,我从这个 old thread 重写了它.希望能帮助到你。

require( rgeos )
require( sp )

# Make some polygons
grd <- GridTopology(c(1,1), c(1,1), c(10,10))
polys <- as.SpatialPolygons.GridTopology(grd)

# Make some points and label with letter ID
set.seed( 1091 )
pts = matrix( runif( 20 , 1 , 10 ) , ncol = 2 )
sp_pts <- SpatialPoints( pts )
row.names(pts) <- letters[1:10]

# Plot
plot( polys )
text( pts , labels = row.names( pts ) , col = 2 , cex = 2 )
text( coordinates(polys) , labels = row.names( polys ) , col = "#313131" , cex = 0.75 )

enter image description here
# Find which polygon each point is nearest
cbind( row.names( pts ) , apply( gDistance( sp_pts , polys , byid = TRUE ) , 2 , which.min ) )
# [,1] [,2]
#1 "a" "86"
#2 "b" "54"
#3 "c" "12"
#4 "d" "13"
#5 "e" "78"
#6 "f" "25"
#7 "g" "36"
#8 "h" "62"
#9 "i" "40"
#10 "j" "55"

关于r - 大数据集的空间匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15992158/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com