Skip to content

How to create inward (shrinking) buffer zones with st_buffer()

1 message · Nathaniel Henry

#
Hi Xiang,

As an alternative, you could just convert the polygon boundaries to lines,
buffer those lines, and remove them from the original polygons. I think
this will work regardless of your CRS/s2 settings, and you don't have to
worry about how the buffer function is handling negative distance
internally.

Example:
bristol_polys <- sf::st_read(
  "
https://martinjc.github.io/UK-GeoJSON/json/eng/wards_by_lad/topo_E06000023.json
",
  crs = 'EPSG:4326'
) |> sf::st_transform(crs = 'EPSG:27700')
# Convert to polylines and buffer
buffered_lines <- sf::st_cast(bristol_polys, to = 'MULTILINESTRING') |>
  sf::st_make_valid() |>
  sf::st_buffer(dist = units::set_units(100, 'm')) |>
  sf::st_union()
# Remove the buffers from the polygons
shrunk_polygons <- sf::st_difference(x = bristol_polys, y = buffered_lines)

Best,
Nat
--
*Nathaniel Henry, DPhil*
Henry Spatial Analysis <https://henryspatialanalysis.com/>
*Cell: +1-341-226-6277*

On Thu, 22 Feb 2024 at 08:28, Nick Bearman <
nick at geospatialtrainingsolutions.co.uk> wrote: