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

时间:2021-03-04 02:49:18

标签: python shapely

我有一个与 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 个多边形),或者是否出现了问题?无论哪种方式,有没有办法改进这个过程?有没有更好的方法来检查一系列点落在几个多边形中的哪个而不检查每个可能的关系?

0 个答案:

没有答案