有没有办法优化 shapely.geometry.shape.contains(a_point) 调用的速度?
Is there way to optimize speed of shapely.geometry.shape.contains(a_point) call?
我们正在使用 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 是否还有其他更快的库,或者我们可以通过某种方式优化形状文件以获得更快的速度吗?
感谢@iant 点使用空间索引。
我的 shapefile 是一个包含很多点的单个 MultiPoligon,.contains()
真的很慢。
我通过将它拆分成更小的形状并使用 Rtree 索引解决了这个问题。
为了分割 shapefile,我使用了 QGIS,正如这里描述的那样 - https://gis.stackexchange.com/a/23694/65569
如何在python中使用RTree的核心思想在这里 - https://gis.stackexchange.com/a/144764/65569
总的来说,这使我的 .contains() 查找速度提高了 1000 倍!
我们正在使用 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 是否还有其他更快的库,或者我们可以通过某种方式优化形状文件以获得更快的速度吗?
感谢@iant 点使用空间索引。
我的 shapefile 是一个包含很多点的单个 MultiPoligon,.contains()
真的很慢。
我通过将它拆分成更小的形状并使用 Rtree 索引解决了这个问题。
为了分割 shapefile,我使用了 QGIS,正如这里描述的那样 - https://gis.stackexchange.com/a/23694/65569
如何在python中使用RTree的核心思想在这里 - https://gis.stackexchange.com/a/144764/65569
总的来说,这使我的 .contains() 查找速度提高了 1000 倍!