【问题标题】:Create list of points from Polygon in geodataframe从地理数据框中的多边形创建点列表
【发布时间】:2020-11-10 19:17:38
【问题描述】:

我有一个如下所示的地理数据框:

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'

有什么想法可以去掉“多边形”的名称,并从字面上得到构成该多边形的点列表吗?要清楚,我不想要多边形的外部或边界,我想要所有的点在边界内。

【问题讨论】:

    标签: python pandas geopandas


    【解决方案1】:

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

    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()
    

    【讨论】:

    • 这不是我想要的。我真的想要一个浮点数的原始列表,它代表点。像 [[x,y],[x1,y1],[x2,y2]...]
    • 好的 - 稍微修改了我的功能 - 我认为这更接近您的需要。
    • 现在这就是我要找的。谢谢!
    猜你喜欢
    • 2018-12-13
    • 2019-04-03
    • 1970-01-01
    • 2012-04-02
    • 2021-08-02
    • 2022-01-06
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多