从地理数据框中的多边形创建点列表
Create list of points from Polygon in 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
我可以在 'geometry' 列中看到点列表,但想将这些点拉出并放入列表中。例如,在 pandas 中,您可以执行类似 df['column'].to_list() 的操作。但是,尝试此操作时出现错误:
gdf.iloc[0]['geometry'].to_list()
AttributeError: 'Polygon' object has no attribute 'to_list'
有什么想法可以删除“多边形”名称并从字面上只获取构成该多边形的点列表吗?明确地说,我不想要多边形的外部或边界,我想要所有边界内的点。
这是我用来检查多边形内容的通用函数 - 不确定它是否正是您要找的。我相信多边形可以具有任意的复杂性,因此您可以在零件中包含零件:
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()
我有一个如下所示的地理数据框:
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
我可以在 'geometry' 列中看到点列表,但想将这些点拉出并放入列表中。例如,在 pandas 中,您可以执行类似 df['column'].to_list() 的操作。但是,尝试此操作时出现错误:
gdf.iloc[0]['geometry'].to_list()
AttributeError: 'Polygon' object has no attribute 'to_list'
有什么想法可以删除“多边形”名称并从字面上只获取构成该多边形的点列表吗?明确地说,我不想要多边形的外部或边界,我想要所有边界内的点。
这是我用来检查多边形内容的通用函数 - 不确定它是否正是您要找的。我相信多边形可以具有任意的复杂性,因此您可以在零件中包含零件:
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()