在python中有效地为多边形分配点

问题描述

我有一个Assign points to polygons effeciently 几乎相同的问题,但我使用的是 Python。

我已经将一个经纬度列表转换为一个匀称点列表,并且我有一个从 shapefile 中读入的多边形列表:

import shapely
import shapefile

points = [Point(lon,lat) for lon,lat in zip(lons,lats)]
shp = shapefile.Reader('file.shp')
polys = shp.shapes()
records = shp.records()

我想生成一个列表,其中包含每个点的多边形 ID(存储在 records[0] 中)。

到目前为止,我的方法是这样的:

polyIDs = [0] * len(points)

for i in range(len(points)):
    count = 0
    for j in polys:
        if points[i].within(shape(j)):
            polyIDs[i] = records[count][0]
        count += 1

我不确定这些代码是否有效并且需要很长时间(我有 40,000 个点和 2,000 个多边形),或者是否出现了问题?无论哪种方式,有没有办法改进这个过程?有没有更好的方法来检查一系列点落在几个多边形中的哪个而不检查每个可能的关系?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)