[GIS] How to convert a GeoSeries to a GeoDataFrame with Geopandas

convertgeopandasgeoseriespython

I'm new in GeoPandas and I try to create my first script.

I successfully opened a shapefile and made a union of all entity and an envelope.
As it said in GeoPandas doc, after these 2 geometric manipulations it returns GeoSeries.

But how can I convert my GeoSeries to a GeoDataFrame to finally export it to a shapefile ?

I try this on point shapefile but my GeoDataFrame is empty…

import sys
import geopandas as gpd

shp = (sys.argv[1])

gdf = gpd.read_file(shp)

union = gdf.unary_union

env = union.envelope

envgdf = gpd.GeoDataFrame()

envgdf['geometry'] = env
envgdf.geometry.name

print("\nGeoDataFrame :\n", envgdf)

Returns :

GeoDataFrame :
Empty GeoDataFrame
Columns: [geometry]
Index: []

Best Answer

GeoDataframe now accepts a geometry keyword argument. Taking advantage of that, we can write

envgdf = gpd.GeoDataFrame(geometry=gpd.GeoSeries(env))

This automatically sets the GeoSeries as the geometry column.