Geopandas Buffer – Creating Buffer Using GeoDataFrame While Maintaining DataFrame

buffergeopandaspython

The objective is to create a geoDataFrame with buffered geometries AND with all the "attributes" of the original geodataframe.

I am able to perform a buffer on my geoDataFrame using:

gdfHybasBuffer = gdfHybas['geometry'].buffer(-0.005,resolution=16)

but the result is a geoSeries and not a geoDataFrame, and therefore does not contain the data from the original geoDataFrame nor does it contain an index to join the data to the original data. Is there a better way to perform a buffer while maintaining the original attribute data?

github Code

It is pretty straight forward to create a buffer on a geopandas geoSeries.

Best Answer

from shapely.geometry import Point
import pandas as pd
import geopandas as gpd
p1 = Point((1,2))
p2 = Point((5,6))
df = pd.DataFrame({'a': [11,22]})
gdf = gpd.GeoDataFrame(df, geometry = [p1,p2])
gdf
#out: 
#   a   geometry
#0  11  POINT (1 2)
#1  22  POINT (5 6)

You can directly assign the buffer as a new geometry column to your GeoDataFrame:

gdf['geometry'] = gdf.geometry.buffer(2)
#out:
#   a   geometry
#0  11  POLYGON ((3 2, 2.990369453344394 1.80396571934...
#1  22  POLYGON ((7 6, 6.990369453344394 5.80396571934...
gdf.plot()

buffer

Related Question