21

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.

2
  • I already found one error in my code: gdfHybas = gdfHybas.set_index('PFAF_ID') instead of gdfHybas.set_index('PFAF_ID'). In that case, the index will be preserved in the geoSeries and I can use merge to replace the old geometry with the new buffers
    – RutgerH
    Aug 24, 2017 at 20:27
  • The next problem I encountered when using geopandas merge, is that it does not by default merge on the indices (if you do not specify "on=")
    – RutgerH
    Aug 24, 2017 at 20:54

1 Answer 1

38
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

10
  • This won't work if your original dataframe has null geometries
    – user32882
    Mar 30, 2019 at 16:44
  • @user32882 you cannot even create a GeoDataFrame if your input geometries are not valid
    – Alz
    Mar 31, 2019 at 22:47
  • not invalid... null geometry (empty) its not the same thing
    – user32882
    Apr 1, 2019 at 6:30
  • can you give an example?
    – Alz
    Apr 1, 2019 at 9:39
  • @Alz Does this mean that the geopandas overlay and geometry manipulation functions are vectorized?
    – jesnes
    Jul 15, 2020 at 18:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.