问题描述
我正在使用gIntersection从SpatialPolygonsDataFrame一次按一个多边形修剪一个全国范围的路径网络.我遍历每个多边形,剪切路径网络,计算剪切路径的长度,然后将其保存到名为path.lgth的数据框中:
I am using gIntersection to clip a nationwide path network by polygons one at a time from a SpatialPolygonsDataFrame. I am looping through each polygon, clipping the path network, calculating the length of the clipped paths, and saving this to a dataframe called path.lgth:
poly<-readShapePoly("C:\\temp\\polygons.shp")
paths<-readShapeLines("C:\\temp\\paths.shp")
#loop through all polygons clipping lines
path.lgth<-data.frame()
for (i in 1:length(poly)){
clip<-gIntersection(paths,poly[i,])
lgth<-gLength(clip)
vid<-poly@data[i,3]
data<-cbind(vid,lgth)
path.lgth<-rbind(path.lgth,data)
print(i)
}
vid线仅提取多边形ID,并将其路径长度保存在数据框中.
The vid line just extracts the polygon ID to save in the dataframe with the path length.
我的问题是,完成第一个多边形需要花费太长时间(大约12分钟!).有没有办法加快速度?我不确定gIntersection在数学上是做什么的(是否检查所有路径以查看它们是否覆盖了多边形?).我简化了路径,所以它们只是功能之一.
My problem is that it takes way too long to do the first polygon (around 12 minutes!). Is there a way to speed this up? I'm not sure what gIntersection does mathematically (is it checking all paths to see if they overlay with the polygon?). I have simplified my paths so they are only one feature.
感谢您的帮助.
推荐答案
由于我遇到过同样的问题,因此,这是减少处理时间的解决方法.(这是一个已有4年历史的问题!我希望像我这样的某些人仍然面临此类问题吗?)
我建议首先仅选择每个gIntersection步骤中涉及的线要素,其关闭函数为"gIntersects",该函数返回逻辑值,并且比快得多 gIntersection!
since I was facing the same kind of issue, here's my workaround to reduce processing time.(it's a 4 years-old question! I hope that some people -like me- are still facing such problems?)
I'd advise to first select only the line features that are involved in each gIntersection step with the close function 'gIntersects', which returns a logical and which is much faster than gIntersection!
因此您的代码将是这样的:
Therefore your code would be like that:
poly<-readShapePoly("C:\\temp\\polygons.shp")
paths<-readShapeLines("C:\\temp\\paths.shp")
#loop through all polygons clipping lines
path.lgth<-data.frame()
for (i in 1:length(poly)){
# FIRST gIntersects to subset the features you need
LogiSubPaths <- gIntersects(paths, poly[i, )[1,] #The result is a dataframe with one row
clip <- gIntersection(path[LogiSubPaths,],poly[i, ]) #Subset the features that you need to gIntersect the current polygon 'i'
lgth <- gLength(clip)
vid <- poly@data[i,3]
data <- cbind(vid,lgth)
path.lgth <- rbind(path.lgth,data)
print(i)
}`
值得尝试一个真实的数据集,以确认输出是否符合您的需求.
Worth to give a try on a real dataset to confirm that the output matches your need.
这篇关于rgeos gIntersection循环中花费的时间太长,无法剪切路径网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!