有没有办法优化shapely.geometry.shape.contains(a_point)调用的速度?

时间:2016-03-29 10:55:26

标签: python geospatial shapefile

我们正在使用shapely库检查某个随机点是否存在于形状文件中的某些禁区中。

with fiona.open(path) as source:
    geometry = get_exclusive_item(source[0])
geom = shapely.geometry.shape(geometry['geometry'])

def check(lat, lng):
    point = shapely.geometry.Point(lng, lat)
    return not geom.contains(point)  

但是最新的电话geom.contains(point)需要大约一秒钟才能完成。是否还有其他更快的python库,或者我们能以某种方式优化形状文件以获得更好的速度吗?

1 个答案:

答案 0 :(得分:1)

感谢@iant点使用空间索引。

我的shapefile是一个包含很多分数的MultiPoligon,因为.contains()非常慢。

我通过将其拆分为更小的形状并使用Rtree索引解决了这个问题。

  1. 要分割shapefile,我使用了QGIS,如此处所述 - https://gis.stackexchange.com/a/23694/65569

  2. 如何在python中使用RTree的核心思想是 - https://gis.stackexchange.com/a/144764/65569

  3. 总的来说,这为.contains()查找提供了1000倍的加速!