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?
It is pretty straight forward to create a buffer on a geopandas geoSeries.
-
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 buffersRutgerH– RutgerH2017年08月24日 20:27:21 +00:00Commented 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– RutgerH2017年08月24日 20:54:24 +00:00Commented Aug 24, 2017 at 20:54
1 Answer 1
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()
-
This won't work if your original dataframe has null geometriesuser32882– user328822019年03月30日 16:44:06 +00:00Commented Mar 30, 2019 at 16:44
-
@user32882 you cannot even create a
GeoDataFrame
if your input geometries are not validAlz– Alz2019年03月31日 22:47:18 +00:00Commented Mar 31, 2019 at 22:47 -
not invalid... null geometry (empty) its not the same thinguser32882– user328822019年04月01日 06:30:24 +00:00Commented Apr 1, 2019 at 6:30
-
-
@Alz Does this mean that the geopandas overlay and geometry manipulation functions are vectorized?jesnes– jesnes2020年07月15日 18:40:18 +00:00Commented Jul 15, 2020 at 18:40