python - 二元/一元谓词函数将所有对象与python中的所有其他对象进行交叉比较
问题描述
我以前问过一个非常相似的问题。由于arcpy
解决方案非常繁琐,我现在正在寻找基本相同的功能geopandas
。问题是:应用二元谓词函数(例如touches
)的最快/最佳方法是什么,其中的每个特征x
都与任何一个或不同数据集的每个其他特征进行比较。我希望输出类似于 R 中的默认行为:x
y
如果 y 缺失,
st_predicate(x, x)
则被有效调用,并返回一个带有对角元素的方阵st_predicate(x[i], x[i])
。
用一些虚拟数据和函数来举例说明st_overlaps()
:
library(sf)
b0 = st_polygon(list(rbind(c(-1,-1), c(1,-1), c(1,1), c(-1,1), c(-1,-1))))
a0 = b0 * 0.8
a1 = a0 * 0.5 + c(2, 0.7)
a2 = a0 + 1
a3 = b0 * 0.5 + c(2, -0.5)
x = st_sfc(a0,a1,a2,a3)
plot(x)
st_overlaps(x)
#> Sparse geometry binary predicate list of length 4, where the predicate was `overlaps'
#> 1: 3
#> 2: 3
#> 3: 1, 2
#> 4: (empty)
如何在python
/中实现类似的行为geopandas
?显然,geopandas
自动对齐x
和x
/y
并执行元素明智的比较(请参阅this SO question和github上的this issue)。在 python 中,运行x.overlaps(x)
只返回一个带有四个True
值的熊猫系列。
import geopandas as gpd
x.overlaps(x)
0 True
1 True
2 True
3 True
解决方案
这绝对不是最快的方法,因为它只是一个简单的迭代器,但如果您的数据不是很大,它可能会完成工作。
import geopandas as gpd
from shapely.geometry import Polygon
b0 = Polygon([(-1,-1), (1,-1), (1,1), (-1,1)])
a1 = Polygon([(1.5,0.2), (2.5,0.2), (2.5,1.2), (1.5,1.2)])
a2 = Polygon([(0,0), (2,0), (2,2), (0,2)])
a3 = Polygon([(1.5,-1), (2.5,-1), (2.5,-0.2), (1.5,-0.2)])
series = gpd.GeoSeries([b0, a1, a2, a3])
results = {}
for poly in series.iteritems():
results[poly[0]] = []
for poly2 in series.drop(poly[0]).iteritems():
if poly[1].overlaps(poly2[1]):
results[poly[0]].append(poly2[0])
它会给你你的价值观。
{0: [2], 1: [2], 2: [0, 1], 3: []}
但是,请注意它会先检查 A->B,然后再检查 B->A,并且它还会检查多边形,即使它们显然很远。为了加快速度,您可以使用 rtree 空间索引仅检查可能重叠的那些,而不是检查每个多边形(两次)。
推荐阅读
- vue.js - VeeValidate 检查整个范围内的错误
- dialogflow-es - Google Assistant Created in DialogFlow Endpoint Changes
- generics - 作为使用关联类型的特征的结构成员
- angular - 从数组中删除空元素
- angularjs - angular-ui-swiper - 如何设置选项?
- c# - lambda 事件处理程序会导致什么样的内存泄漏?
- xamarin - 如何在 xamarin 表单中维护错误登录
- jenkins - 在进入阶段之前设置变量的声明性管道脚本
- javascript - 当我应用它时,我的纹理接缝出现在我的 3D 模型中
- php - Debian 9 - 安装“xdebug master”而不是“Xdebug v2.7.0beta1”