1

I am trying to use Python and GeoPandas to create variable distance buffers for a polygons shapefile I have. I brought this shapefile into python using GeoPandas. One of the columns of the GeoDataFrame has a Distances column with a unique distance for each polygon (row). I want to buffer all of the polygons in the shapefile/GeoDataFrame, but where each individual polygon is buffered at a distance corresponding to its own corresponding distance in the Distances column.

I tried this:

polygons = gpd.read_file('polygons.shp')
distances = polygons['Distances']
polygons_buffered = polygons.buffer(distances)
polygons_buffered.to_file("polygons_buffered.shp")
polygons_buffered = gpd.read_file('polygons_buffered.shp')
polygons_buffered.head()

And then I see that the only data that is preserved in the attribute table is and FID and geometry, but all of my attribute columns with all of my associated data are gone. I brought the shapefile into QGIS, and see that the variable distances buffers were created as a wanted, but that these buffers were missing all of the data I wanted them to contain.

How can I fix my code so that I create these variable distance buffers based on my "Distances" column, but so that all of the attribute data within each of the original polygons is preserved within its newly buffered polygon?

5
  • polygons_buffered = polygons.buffer(distances) that looks like you're only storing the geometry to the new gdf. I would use apply on the original gdf instead
    – Encomium
    Dec 3, 2021 at 20:18
  • Ok so if I were to use .apply() on my original gdf, would that look like: polygons_buffered = polygons.apply(buffer(distances))? I am just a bit confused if I would be "applying" the .buffer(distances) part to the original gdf or if I wrote that correctly. Dec 3, 2021 at 20:22
  • Hold on, let me test this on my end!
    – Encomium
    Dec 3, 2021 at 20:24
  • I tried polygons.apply(buffer(distances)) and it did not work for me. Dec 3, 2021 at 20:25
  • Have a look at my answer. I assume your original shapefile is in a projected CRS?
    – Encomium
    Dec 3, 2021 at 20:31

1 Answer 1

1

Looks like you're only storing the geometry field in the polygons_buffered GDF. You can apply the buffer function directly to the original GDF and either modify the geometry field in place or store in another gdf (which requires you to store more than just the computed field in the new gdf, which is where you had issues with your original code). Since you're exporting immediately as a SHP, I'd go with the former option:

polygons['geometry'] = polygons['geometry'].buffer(polygons['Distance'])
polygons.head()
polygons.plot()
2
  • Solved! Your suggestion there (the "former option" you label it) did it! Thank you very much! Dec 3, 2021 at 20:42
  • My pleasure. Sorry about the possible confusion in the earlier comments. I guess I use the word apply to not only mean the literal 'apply' function :)
    – Encomium
    Dec 3, 2021 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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