有没有办法优化shapely.geometry.shape.contains(a_point)调用的速度? [英] Is there way to optimize speed of shapely.geometry.shape.contains(a_point) call?
问题描述
我们正在使用 shapely 库来检查存储在某些禁区中的随机点是否存在形状文件.
We are using a shapely library to check that some random point is not in some prohibited areas stored in a shape file.
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的更快的库,还是我们可以通过某种方式优化形状文件以获得更快的速度?
But the latest call geom.contains(point)
takes about a second to complete. Is there any other faster libraries for python, or could we optimize a shape files somehow to get better speed?
推荐答案
感谢@iant点使用空间索引.
Thank for the @iant point to use a spatial indexes.
我的shapefile是一个具有很多点的MultiPoligon,这使得.contains()
真的很慢.
My shapefile was a single MultiPoligon with a lot of points, makes .contains()
are really slow.
我通过将其分成较小的形状并使用Rtree索引解决了该问题.
I solved the issue by splitting it into smaller shapes and use Rtree index.
-
要分割shapefile,我使用了QGIS,如此处所述- https://gis.stackexchange.com/a/23694/65569
此处介绍如何在python中使用RTree的核心思想- https://gis.stackexchange.com/a/144764/65569
The core idea how to use RTree in python is here - https://gis.stackexchange.com/a/144764/65569
总共使我对.contains()查找的速度提高了1000倍!
In total this gaves me 1000x speed-up for .contains() lookups!
这篇关于有没有办法优化shapely.geometry.shape.contains(a_point)调用的速度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!