将 GeoPandas 多边形数据框扩展为每行一个多边形
Explanding GeoPandas Multipolygon Dataframe To One Poly Per Line
这个问题与另一个问题类似,但 none 的解决方案对我有用。请注意,我已经对这些解决方案和结果进行了多次尝试。如果另一个图书馆能做到这一点,我愿意接受。
我正在尝试使用包含多个多边形的 GeoPandas 扩展 GeoJson 文件。
当前地理数据框(3 行)
fill fill-opacity stroke stroke-opacity stroke-width title geometry
0 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-80.69500140880155 22.2885709067316...
1 #08c1e6 0.3 #08c1e6 1 1 Severe Hail (POLYGON ((-103.4850007575523 29.2010260633722...
2 #682aba 0.3 #682aba 1 1 Damaging Hail (POLYGON ((-104.2750007349772 32.2629245180204...`
所需的地理数据框(200 多行)
fill fill-opacity stroke stroke-opacity stroke-width title geometry
0 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-80.69500140880155 22.2885709067316...
1 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-102.8150007766983 28.2180513479277...
2 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.4850007575523 29.0940821135748...
3 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.5650007552662 30.9947420843694...
4 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.6150007538374 31.0173836504729...
正在使用的 geojson 文件示例文件:https://drive.google.com/file/d/1m6cMR4jF3QWp07e23sIdb0UF9xLD062s/view?usp=sharing
我试过但没有成功:
df3.set_index(['title'])['geometry'].apply(pd.Series).stack().reset_index()
(Returns原来不变gdf)
def cartesian(x):
return np.vstack(np.array([np.array(np.meshgrid(*i)).T.reshape(-1,7) for i in x.values]))
ndf = pd.DataFrame(cartesian(df3),columns=df3.columns)
(Returns原来不变gdf)
import geopandas as gpd
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon
def explode(indata):
indf = gpd.GeoDataFrame.from_file(indata)
outdf = gpd.GeoDataFrame(columns=indf.columns)
for idx, row in indf.iterrows():
if type(row.geometry) == Polygon:
outdf = outdf.append(row,ignore_index=True)
if type(row.geometry) == MultiPolygon:
multdf = gpd.GeoDataFrame(columns=indf.columns)
recs = len(row.geometry)
multdf = multdf.append([row]*recs,ignore_index=True)
for geom in range(recs):
multdf.loc[geom,'geometry'] = row.geometry[geom]
outdf = outdf.append(multdf,ignore_index=True)
return outdf
explode(GEOJSONFILE)
(Returns原来不变gdf)
这是我在这里提出的第一个问题,如果需要任何其他信息或详细信息,请告诉我。
更新:发现 explode() 函数的问题是由于文件格式问题造成的,其中几何图形本质上是多多边形的多多边形,导致仅第一个多边形循环。爆炸功能有效。
您可以使用 Geopandas explode()
。
exploded = original_df.explode()
从文档字符串复制:
Explode muti-part geometries into multiple single geometries.
Each row containing a multi-part geometry will be split into
multiple rows with single geometries, thereby increasing the vertical
size of the GeoDataFrame.
The index of the input geodataframe is no longer unique and is
replaced with a multi-index (original index with additional level
indicating the multiple geometries: a new zero-based index for each
single part geometry per multi-part geometry).
Returns
-------
GeoDataFrame
Exploded geodataframe with each single geometry
as a separate entry in the geodataframe.
这个问题与另一个问题类似,但 none 的解决方案对我有用。请注意,我已经对这些解决方案和结果进行了多次尝试。如果另一个图书馆能做到这一点,我愿意接受。
我正在尝试使用包含多个多边形的 GeoPandas 扩展 GeoJson 文件。
当前地理数据框(3 行)
fill fill-opacity stroke stroke-opacity stroke-width title geometry
0 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-80.69500140880155 22.2885709067316...
1 #08c1e6 0.3 #08c1e6 1 1 Severe Hail (POLYGON ((-103.4850007575523 29.2010260633722...
2 #682aba 0.3 #682aba 1 1 Damaging Hail (POLYGON ((-104.2750007349772 32.2629245180204...`
所需的地理数据框(200 多行)
fill fill-opacity stroke stroke-opacity stroke-width title geometry
0 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-80.69500140880155 22.2885709067316...
1 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-102.8150007766983 28.2180513479277...
2 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.4850007575523 29.0940821135748...
3 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.5650007552662 30.9947420843694...
4 #9bf1e2 0.3 #9bf1e2 1 1 Hail Possible (POLYGON ((-103.6150007538374 31.0173836504729...
正在使用的 geojson 文件示例文件:https://drive.google.com/file/d/1m6cMR4jF3QWp07e23sIdb0UF9xLD062s/view?usp=sharing
我试过但没有成功:
df3.set_index(['title'])['geometry'].apply(pd.Series).stack().reset_index()
(Returns原来不变gdf)
def cartesian(x):
return np.vstack(np.array([np.array(np.meshgrid(*i)).T.reshape(-1,7) for i in x.values]))
ndf = pd.DataFrame(cartesian(df3),columns=df3.columns)
(Returns原来不变gdf)
import geopandas as gpd
from shapely.geometry.polygon import Polygon
from shapely.geometry.multipolygon import MultiPolygon
def explode(indata):
indf = gpd.GeoDataFrame.from_file(indata)
outdf = gpd.GeoDataFrame(columns=indf.columns)
for idx, row in indf.iterrows():
if type(row.geometry) == Polygon:
outdf = outdf.append(row,ignore_index=True)
if type(row.geometry) == MultiPolygon:
multdf = gpd.GeoDataFrame(columns=indf.columns)
recs = len(row.geometry)
multdf = multdf.append([row]*recs,ignore_index=True)
for geom in range(recs):
multdf.loc[geom,'geometry'] = row.geometry[geom]
outdf = outdf.append(multdf,ignore_index=True)
return outdf
explode(GEOJSONFILE)
(Returns原来不变gdf)
这是我在这里提出的第一个问题,如果需要任何其他信息或详细信息,请告诉我。
更新:发现 explode() 函数的问题是由于文件格式问题造成的,其中几何图形本质上是多多边形的多多边形,导致仅第一个多边形循环。爆炸功能有效。
您可以使用 Geopandas explode()
。
exploded = original_df.explode()
从文档字符串复制:
Explode muti-part geometries into multiple single geometries.
Each row containing a multi-part geometry will be split into
multiple rows with single geometries, thereby increasing the vertical
size of the GeoDataFrame.
The index of the input geodataframe is no longer unique and is
replaced with a multi-index (original index with additional level
indicating the multiple geometries: a new zero-based index for each
single part geometry per multi-part geometry).
Returns
-------
GeoDataFrame
Exploded geodataframe with each single geometry
as a separate entry in the geodataframe.