Monday, 12 September 2016

python - Geopandas buffer using geodataframe while maintaining the dataframe


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.



Answer



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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...