【问题标题】:Is there way to optimize speed of shapely.geometry.shape.contains(a_point) call?有没有办法优化 shapely.geometry.shape.contains(a_point) 调用的速度?
【发布时间】:2016-03-29 10:55:25
【问题描述】:

我们正在使用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 库,或者我们可以以某种方式优化形状文件以获得更好的速度?

【问题讨论】:

  • 你的 shapefile 有空间索引吗?
  • 您安装了 GEOS 吗?并确保 Shapely 能找到它?

标签: python geospatial shapefile


【解决方案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

总的来说,这让我的 .contains() 查找速度提高了 1000 倍!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多