检查点列表是否在多多边形内(Python)

时间:2018-10-22 02:18:22

标签: python-3.x polygon shapefile shapely geopandas

我正在尝试检查是否在我的shapefile(多多边形)中找到了点列表(存储在df.Coords中)。我在下面编写了代码,但是,我意识到python只读取多面体最后一层的坐标。

import shapefile
from shapely.geometry import shape, Point

# read your shapefile
r = shapefile.Reader("C:\\filename.shp")

# get the shapes
shapes = r.shapes()

# build a shapely polygon from your shape
hold = []
for k in range(len(shapes)-1): 
    polygon = shape(shapes[k])
    hold.append(polygon)


    for x in df.Coords: 

        if polygon.contains(Point(x)):

            hold.append(x) 

我试图在stackoverflow上搜索一些代码(参考:https://gis.stackexchange.com/questions/208546/check-if-a-point-falls-within-a-multipolygon-with-python/208574)并对其进行编辑(如下所示)。但是,我认为我编辑不正确。

from shapely.geometry import Polygon, Point, MultiPolygon
import shapefile 

polygon = shapefile.Reader("C:\\filename.shp") 

polygon = polygon.shapes() 

shpfilePoints = [ shape.points for shape in polygon ]

#print(shpfilePoints)


polygons = shpfilePoints
hold = []

for polygon in polygons:
    poly = Polygon(polygon)
    hold.append(poly)
    for x in hold: 

        for y in df.Coords:
            if x.contains(Point(y)):
                hold.append(y) 
print('hold',hold)

如何检查我的点列表(df.Coords)是否在我的多边形(具有26层)中?

0 个答案:

没有答案