从geodataframe中的多边形创建点列表

2024-06-29 00:14:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我的geodataframe如下所示:

ID_0    ISO NAME_0  ID_1    NAME_1  ID_2    NAME_2  TYPE_2  ENGTYPE_2   NL_NAME_2   VARNAME_2   geometry    soyb_a  percent percent_sum
1489    33  BRA Brazil  12  Mato Grosso 1490    Nova Mutum  Município   Municipality    0   0   POLYGON ((-56.61388 -12.87704, -56.57753 -12.8...   1078374.8   2.923144    2.923144
1405    33  BRA Brazil  11  Mato Grosso do Sul  1406    Sapezal Município   Municipality    0   0   POLYGON ((-57.82408 -19.11719, -57.78419 -19.0...   1027233.8   2.784516    5.707660
1529    33  BRA Brazil  12  Mato Grosso 1530    Sapezal Município   Municipality    0   0   POLYGON ((-58.92996 -12.64107, -58.93618 -12.6...   1027233.8   2.784516    8.492176

我可以在“几何体”列中看到点列表,但我想将这些点拉出并放入列表中。例如,在pandas中,您可以执行类似df['column'].to_list()的操作。但是,在尝试此操作时,我得到一个错误:

gdf.iloc[0]['geometry'].to_list()

AttributeError: 'Polygon' object has no attribute 'to_list'

你知道我如何去掉“多边形”名称,直接得到组成该多边形的点的列表吗?说清楚,我不想要多边形的外部或边界,我想要边界内的所有点


Tags: tonameid列表多边形listgeometrybrazil
1条回答
网友
1楼 · 发布于 2024-06-29 00:14:36

这是一个我用来检查多边形内容的通用函数-不确定它是否正是您要查找的内容。我相信多边形可以具有任意的复杂性,因此可能是在零件内部有零件:

def listPoints(someGeometry):
    '''List the points in a Polygon in a geometry entry - some polygons are more complex than others, so accommodating for that'''    
    pointList = []
    try:
        #Note: might miss parts within parts with this
        for part in someGeometry:
            x, y = part.exterior.coords.xy
            pointList.append(list(zip(x,y)))
    except:
        try:
            x,y = someGeometry.exterior.coords.xy
            pointList.append(list(zip(x,y)))
        except:
            #this will return the geometry as is, enabling you to see if special handling is required - then modify the function as need be
            pointList.append(someGeometry)
    return pointList

然后作为lambda应用:

gdf.geometry.apply(lambda x: listPoints(x)).values.tolist()

enter image description here

相关问题 更多 >