在地理数据框中从多边形创建点列表

时间:2020-11-10 19:17:38

标签: python pandas geopandas

我有一个看起来像这样的地理数据框:

piv = df.pivot_table(index=['subjectedit', 'groupedit'], columns=['testday'],
                     values=['01. Tristeza Aparente'], aggfunc='sum').reset_index().droplevel(1, axis=1)
piv['new'] = piv.iloc[:,-2] / piv.iloc[:,-1]
df = pd.merge(df,piv[['subjectedit', 'groupedit','new']], how='left', on=['subjectedit', 'groupedit'])
df
Out[1]: 
   01. Tristeza Aparente  groupedit  subjectedit testday  new
0                    4.0    placebo          1.0   Basal  1.0
1                    4.0    placebo          1.0      t2  1.0
2                    4.0    placebo          2.0   Basal  NaN
3                    1.0    placebo          3.0   Basal  NaN
4                    4.0  treatment         20.0   Basal  2.0
5                    2.0  treatment         20.0      t2  2.0
6                    4.0  treatment         21.0   Basal  4.0
7                    1.0  treatment         21.0      t2  4.0

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

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

有什么想法可以删除'Polygon'的名称,而实际上只是获取组成该Polygon的点的列表吗?要清楚,我不需要多边形的外部或边界,而是想要所有点在边界内。

1 个答案:

答案 0 :(得分: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()

enter image description here