首页 > 解决方案 > dist2Line 的快速替代方案

问题描述

我有一个巨大的数据集,其中包含空间数据点和空间多边形。我想计算每个点到最近多边形的距离。

我使用了 geosphere 包中的 dist2Line() 。但是时间太长了。

我需要一个更快的 dist2Line() 版本。

任何帮助将不胜感激!

标签: rspatial

解决方案


你可以尝试并行处理

#points being the spatialpointsdataframe, and lines being your spatiallinesdataframe
require(parallel)
fun<-function(i) data.frame(dist2Line(points[i,], lines)) #some function like this

cl <- makeCluster(detectCores())
clusterEvalQ(cl, { library("geosphere") }) #don't know what this does, but it's how i learned this. 
clusterExport(cl, c("dist2Line", "points", "lines")) #here you have to include all your objects and functions you want to use, and export them to a cluster, whatever that is.
results <- parLapply(cl,1:length(points),fun=fun) #use parLapply to 'loop' through the points and return a list of dataframes. should be a list. 

推荐阅读