24

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.

RutgerH
  • 3,285
  • 3
  • 27
  • 55
  • 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 '17 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 '17 at 20:54

1 Answers1

45
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

Alz
  • 706
  • 7
  • 7
  • This won't work if your original dataframe has null geometries – user32882 Mar 30 '19 at 16:44
  • @user32882 you cannot even create a GeoDataFrame if your input geometries are not valid – Alz Mar 31 '19 at 22:47
  • not invalid... null geometry (empty) its not the same thing – user32882 Apr 01 '19 at 06:30
  • can you give an example? – Alz Apr 01 '19 at 09:39
  • @Alz Does this mean that the geopandas overlay and geometry manipulation functions are vectorized? – jesnes Jul 15 '20 at 18:40
  • @Alz If soure GeoDataFrame has geographic projection should it be first reprojected to projected coordinate system? – zwornik Sep 27 '20 at 19:23
  • Is there a way to perform the buffer but keeping the squared corners in rectangles? – Roger Almengor Sep 30 '20 at 10:56
  • @jesnes I am sorry for the late response. I am not sure about that, I don't know the internals of Geopandas to be honest. But a simple test tells me that it is probably not the case: %timeit gdf.geometry.buffer(2) and %timeit [i.buffer(2) for i in gdf.geometry] on a somewhat large data give similar numbers (%timeit is an ipython magic command for simple benchmark). If it was vectorized, I would expect the former to be substantially faster. – Alz Oct 03 '20 at 13:00
  • @zwornik the buffer here is a shapely method. Shapely operates on a cartesian plane and have no notion of projection or geography. If you have lat/lon and you expect the buffer to give you distance, you need to project the coordinated into an appropriate coordinate system first. – Alz Oct 03 '20 at 13:17
  • @RogerAlmengor I am not sure if I understand the question, can you explain a bit more? – Alz Oct 03 '20 at 13:21