【问题标题】:Improve speed/use of gDistance function by using parallel processing and/or plyr/dplyr?通过使用并行处理和/或 plyr/dplyr 来提高 gDistance 函数的速度/使用?
【发布时间】:2015-02-17 22:06:34
【问题描述】:

我为大约 1000 个人中的每一个提供了一堆位置。总数据集过去约为 250 万,我的处理脚本运行大约需要 20 个小时。然而,现在,我有 2400 万个观察结果,我认为我需要清理我的代码,如果可以的话,也许可以使用并行处理。

对于每个点,我想找到最近的多边形(大多数点不在多边形中)以及该多边形的距离。这些点主要是海上观测,多边形是离点最近的沿海(美国)县。

我一直在使用 rgeos 包中的 gDistance 函数来执行此操作,并且一直在运行一系列循环(我知道,我知道)以分解我每个人的处理。我花了很多时间试图弄清楚如何以某种方式将其移动到 plyr/dplyr 语法中,但无法完全理解。我认为部分问题与我的对象类 SpatialPoint 和 SpatialPoylgonDataFrames 有关。

library(sp)
library(rgeos)
library(plyr)
#  Create SpatialPointsDataFrame
#  My actual dataset has 24 million observations
my.pts <- data.frame(LONGITUDE=c(-85.4,-84.7,-82.7,-82.7,-86.5,-88.9,-94.8,-83.9,-87.8,-82.8),
             LATITUDE=c(30.0,29.9,27.5,28.5,30.4,26.1,29.3,28.0,29.4,27.8),
             MYID=c(1,1,2,2,2,2,3,4,4,4),
             INDEX=1:10)
coordinates(my.pts) <- c("LONGITUDE","LATITUDE")

#  Create two polygons in a SpatialPolygonsDataFrame
#  My actual dataset has 71 polygons (U.S. counties)
x1 <- data.frame(x=c(-92.3, -92.3, -90.7, -90.7, -92.3, -92.3),y=c(27.6, 29.4, 29.4, 27.6, 27.6, 27.6))
x1 <- as.data.frame(x1) 
x1 <- Polygon(rbind(x1,x1[1,]))

x2 <- data.frame(x=c(-85.2, -85.2, -83.3, -83.2, -85.2, -85.2),y=c(26.4, 26.9, 26.9, 26.0, 26.4,     26.4))
x2 <- as.data.frame(x2) 
x2 <- Polygon(rbind(x2,x2[1,]))

poly1 <- Polygons(list(x1),"poly1")
poly2 <- Polygons(list(x2),"poly2")
myShp <- SpatialPolygons(list(poly1,poly2),1:2)
sdf <- data.frame(ID=c(1,2))
row.names(sdf) <- c("poly1","poly2")
 myShp <- SpatialPolygonsDataFrame(myShp,data=sdf)

   #  I have been outputting my results to a list. With this small sample, it's easy to just put everything into the object county.vec. But I worry that the 24 million x 71 object would not be feasible. The non-loop version shows the output I've been getting more easily.

    COUNTY.LIST <- list()
    county.vec <- gDistance(my.pts, myShp, byid=TRUE)
    COUNTY.LIST[[1]] = apply(county.vec, 2, min)
    COUNTY.LIST[[2]] = apply(county.vec, 2, which.min)
    COUNTY.LIST[[3]] = my.pts$INDEX

# I have been putting it into a loop so that county.vec gets dumped for each version of the loop.
# Seems like this could be done using dlply perhaps? And then I would have the power of parallel processing?
idx <- unique(my.pts$MYID)
COUNTY.LIST <- list()
for(i in 1:length(idx)){
    COUNTY.LIST[[i]] <- list()
    county.vec <- gDistance(my.pts[my.pts$MYID==idx[i],], myShp, byid=TRUE)
    COUNTY.LIST[[i]][[1]] = apply(county.vec, 2, min)
    COUNTY.LIST[[i]][[2]] = apply(county.vec, 2, which.min)
    COUNTY.LIST[[i]][[3]] = my.pts$MY[my.pts$MYID==idx[i]]
    rm(county.vec)
}

dlply(my.pts,.(MYID),gDistance(my.pts, myShp, byid=TRUE),.parallel=TRUE)
> dlply(my.pts,.(MYID),gDistance(my.pts, myShp, byid=TRUE))
Error in eval.quoted(.variables, data) : 
envir must be either NULL, a list, or an environment.

# I suspect this error is because my.pts is a SpatialPointsPolygon.  I also recognize that my function call probably isn't right, but first things first.

# I tried another way to reference the MYID field, more inline with treatment of S4 objects...
dlply(my.pts,my.pts@data$MYID,gDistance(my.pts, myShp, byid=TRUE),.parallel=TRUE)

# It yields the same error.

如果大家有任何建议,我将不胜感激。

【问题讨论】:

  • dlply() 可以处理SpatialPointsDataFrame 吗?我不知道。
  • 我猜它不能,但我不寒而栗地对 SO 做出这样的断言!不过,人们总是会想出如此聪明的回应,如果有解决方法或更合适的方法,我不会感到惊讶。

标签: r plyr spatial sp


【解决方案1】:

这是一个老问题,但也许我的简单方法对其他人有帮助。
它使用平行线。我正在写一个一般的例子。它不会运行上一个数据问题。

set.seed(1)
#Create the clusters
library(doParallel)
cl <- makeCluster(detectCores()) 
registerDoParallel(cl)
#Export the environment variables to each cluster
clusterExport(cl,ls())
#Load the library "rgeos" to each cluster
clusterEvalQ(cl, library(rgeos))
#Split the data
ID.Split<-clusterSplit(cl,unique(poly1$ID))
#Define a function that calculates the distance of one ID in relation to the poly2
a<-function(x) gDistance(spgeom1 = poly1[x,], spgeom2 = poly2, byid=TRUE)
#Run the function in each cluster
system.time(m<-clusterApply(cl, x=ID.Split, fun=a))
#Cluster close
stopCluster(cl)
#Merge the results
output<- do.call("cbind", m)

我希望这会有所帮助。

【讨论】:

  • checkForRemoteErrors(val) 中的错误:8 个节点产生错误;第一个错误:找不到对象“poly1”
  • poly1 和 poly2,是您要计算距离的两个多边形。你确定用你的替换变量名吗?
  • 感谢您的回复。是的,我将您的方法应用于我的代码。基本上我需要“加载”要在每个 m 集群上使用的对象。问题是我正在使用一个非常大的空间对象。你是如何克服这个问题的?
  • 如果我在哪里...我加载每个poly 做一些大的集群。所以你减少了你必须做的比较次数。我的意思是,类似于重叠圆圈 (benkeers.files.wordpress.com/2012/04/circles.jpg)。有道理?然后您只需检查圆圈内的对象并仅比较这些对象。因为你在叠加,最后你要比较所有的。公正和想法。
  • 我已经减小了空间线数据框的大小。我正在做的任务与您略有不同,即找到离每个点最近的线。这是更多的内存和计算密集型,因为对于每个点,函数 gDistance 必须计算 N 距离(其中 N 是空间线数据帧中的总行数),然后找到最小化到该点的距离的行
猜你喜欢
  • 2015-08-02
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-23
  • 2022-12-14
  • 1970-01-01
相关资源
最近更新 更多