I believe it would be nice to add to geopandas further standard vector geometry operations.
For the start, I was thinking about Multipart To Singlepart and Singlepart To Multipart. From my experience, it is cumbersome to switch tools and fall back to QGIS, or alternative instead of doing everything in geopandas. What do you guys think?
Multipart To Singlepart should not be more than a few lines of Python code at least when the operation is simply formulated brute force.
I created a simple solution within the geopandas/pandas ecosystem for myself to geoprocess multipart to singlepart. Tested with this file of shapefile, availbale at: https://www.dropbox.com/sh/y41lbpgtclerqs8/AABQVRcFfoH_VyJK-yWpmO6Ia?dl=0
The results seem to be consistent with what QGIS produces.
import geopandas as gp
import pandas as pd
gpdf = gp.read_file('Germany/vg2500_bld.shp')
def multi2single(gpdf):
gpdf_singlepoly = gpdf[gpdf.geometry.type == 'Polygon']
gpdf_multipoly = gpdf[gpdf.geometry.type == 'MultiPolygon']
for i, row in gpdf_multipoly.iterrows():
Series_geometries = pd.Series(row.geometry)
df = pd.concat([gp.GeoDataFrame(row, crs=gpdf_multipoly.crs).T]*len(Series_geometries), ignore_index=True)
df['geometry'] = Series_geometries
gpdf_singlepoly = pd.concat([gpdf_singlepoly, df])
gpdf_singlepoly.reset_index(inplace=True, drop=True)
return gpdf_singlepoly
The reverse function single2multipart would be addressed by allowing to not pass any column to the geopandas dissolve function.
Any chance this could get added to geopandas' functionality library?
^ What @phildias said.
I already have this script implemented in momepy, but GeoPandas is in fact much better place for it. I鈥檒l do a PR in a couple of days.
Apparently, this has been already implemented in #671 as explode(). I think it is a bit of issue with our documentation as it does not include it (same for collect() which is the opposite of explode). I will close this and open another issue to resolve the visibility of it.
Apparently, this has been already implemented in #671 as
explode(). I think it is a bit of issue with our documentation as it does not include it (same forcollect()which is the opposite ofexplode). I will close this and open another issue to resolve the visibility of it.
Hi, could you share the link to the new one you opened? thx
@Jieyi-Deng if you mean that one regarding documentation it is here #1052
For anyone that stumbles into this issue in the future, it is also worth mentioning that you can use gdf.buffer(0) to re-compute/flatten multipart geometries via Shapely to reduce complexity and overlapping of multipolygon geometries. This does not explicitly change them to Polygon, but I believe fully-overlapping multipolygon parts are squashed into a single Polygon.
Most helpful comment
I created a simple solution within the geopandas/pandas ecosystem for myself to geoprocess multipart to singlepart. Tested with this file of shapefile, availbale at: https://www.dropbox.com/sh/y41lbpgtclerqs8/AABQVRcFfoH_VyJK-yWpmO6Ia?dl=0
The results seem to be consistent with what QGIS produces.
The reverse function single2multipart would be addressed by allowing to not pass any column to the geopandas dissolve function.