pandas Geopandas 数据框指向多边形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39104710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Geopandas Dataframe Points to Polygons
提问by GSam11
I have a geopandas dataframe made up of an id and a geometry column which is populated by 2D points. I want to join the points for each unique id to create a polygon, so that my new dataframe will have polygons as its geometry. My code currently looks something like this:
我有一个由 id 和由 2D 点填充的几何列组成的 geopandas 数据框。我想加入每个唯一 id 的点以创建一个多边形,以便我的新数据框将多边形作为其几何形状。我的代码目前看起来像这样:
polygons = geopandas.GeoDataFrame()
for i in id:
group = df[df['id']== i]
polygon = {'type': 'Polygon', 'coordinates': group['geometry']}
polygon['poly'] = polygon
polygons = geopandas.concat([polygon,polygons])
It creates a polygon but when I assign the new variable poly
it says
它创建了一个多边形,但是当我分配新变量时,poly
它说
ValueError: Length of values does not match length of index"
which makes sense since it is still just a list of coordinates and not an actual polygon object. Does anyone know how to make this an actual polygon object that I can add to a column on a geopandas df
?
Thanks in advance :)
这是有道理的,因为它仍然只是一个坐标列表,而不是一个实际的多边形对象。有谁知道如何使它成为我可以添加到 geopandas 列中的实际多边形对象df
?
提前致谢 :)
回答by atkat12
I have achieved something similar with the groupby
function. Assuming your points are actually Shapely Point
objects, and are sorted in the right order, you can try something like this.
我已经实现了与该groupby
功能类似的功能。假设您的点实际上是 ShapelyPoint
对象,并且按正确的顺序排序,您可以尝试这样的操作。
import pandas as pd
import geopandas as gp
from shapely.geometry import Point, Polygon
# Initialize a test GeoDataFrame where geometry is a list of points
df = gp.GeoDataFrame( [['box', Point(1, 0)],
['box', Point(1, 1)],
['box', Point(2,2)],
['box', Point(1,2)],
['triangle', Point(1, 1)],
['triangle', Point(2,2)],
['triangle', Point(3,1)]],
columns = ['shape_id', 'geometry'],
geometry='geometry')
# Extract the coordinates from the Point object
df['geometry'] = df['geometry'].apply(lambda x: x.coords[0])
# Group by shape ID
# 1. Get all of the coordinates for that ID as a list
# 2. Convert that list to a Polygon
df = df.groupby('shape_id')['geometry'].apply(lambda x: Polygon(x.tolist())).reset_index()
# Declare the result as a new a GeoDataFrame
df = gp.GeoDataFrame(df, geometry = 'geometry')
df.plot()
回答by DataK
Great answer @atkat12. But according to your plot, last two box points should be (0,1) and (0,0):
很好的答案@atkat12。但根据你的情节,最后两个框点应该是 (0,1) 和 (0,0):
# Initialize a test GeoDataFrame where geometry is a list of points df = gp.GeoDataFrame( [['box', Point(1, 0)], ['box', Point(1, 1)], ['box', Point(0,1)], ['box', Point(0,0)], ['triangle', Point(1, 1)], ['triangle', Point(2,2)], ['triangle', Point(3,1)]], columns = ['shape_id', 'geometry'], geometry='geometry')
# Initialize a test GeoDataFrame where geometry is a list of points df = gp.GeoDataFrame( [['box', Point(1, 0)], ['box', Point(1, 1)], ['box', Point(0,1)], ['box', Point(0,0)], ['triangle', Point(1, 1)], ['triangle', Point(2,2)], ['triangle', Point(3,1)]], columns = ['shape_id', 'geometry'], geometry='geometry')