25

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.

asked Aug 24, 2017 at 19:38
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 Commented 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=") Commented Aug 24, 2017 at 20:54

1 Answer 1

46
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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.