[R-sig-Geo] Fwd: How to create inward (shrinking) buffer zones with st_buffer()

Nathaniel Henry henry@@p@t|@|@@n@|y@|@ @end|ng |rom gm@||@com
Thu Feb 22 20:50:48 CET 2024


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 using geospatialtrainingsolutions.co.uk> wrote:

> Dear Xiang,
>
> I've not had a chance to try your code, but your example probably won't
> work with the CRS set to WGS84. The buffer will use the units of the
> coordinate system, so degrees, which will be far too large for the
> bristol_zones example.
>
> Assuming bristol_zones are in the UK, you need to reproject to BNG - 27700.
>
> Also I am not sure whether bristol_zones from spDataLarge are in sp or
> sf format.
>
> Once those are addressed - I see no reason why the example in the Stack
> Exchange should work.
>
> Best wishes,
> Nick.
>
> On 19/02/2024 10:04, Xiang Ye via R-sig-Geo wrote:
> > Dear community,
> >
> > I am learning some basic geometry operation functions of sf package
> including st_buffer().
> >
> > It seems there should be no wonder if I provide a negative value to the
> dist argument in st_buffer(), I should expect an inward/shrinking buffer
> zone (I also followed here:
> https://gis.stackexchange.com/questions/392505/can-i-use-r-to-do-a-buffer-inside-polygons-shrink-polygons-negative-buffer).
> However, it turn out to be as long as I provide a negative value, the
> output will be an empty geometry:
> >
> > library(sf)
> > library(spDataLarge)
> > st_geometry(bristol_zones[1, ]) -> a    # a is the exemplary data set
> >
> >> a
> > Geometry set for 1 feature
> > Geometry type: MULTIPOLYGON
> > Dimension:     XY
> > Bounding box:  xmin: -2.534502 ymin: 51.40487 xmax: -2.488435 ymax:
> 51.43478
> > Geodetic CRS:  WGS 84
> > MULTIPOLYGON (((-2.510462 51.42878, -2.507985 5...
> >> st_buffer(a, 100)
> > Geometry set for 1 feature
> > Geometry type: POLYGON
> > Dimension:     XY
> > Bounding box:  xmin: -2.536248 ymin: 51.40393 xmax: -2.486907 ymax:
> 51.43598
> > Geodetic CRS:  WGS 84
> > POLYGON ((-2.517834 51.43188, -2.518218 51.4318...
> >> st_buffer(a, -100)
> > Geometry set for 1 feature  (with 1 geometry empty)
> > Geometry type: POLYGON
> > Dimension:     XY
> > Bounding box:  xmin: NA ymin: NA xmax: NA ymax: NA
> > Geodetic CRS:  WGS 84
> > POLYGON EMPTY
> >
> > So I would like to know if it is possible to create inward buffer zones
> with st_buffer()? If st_buffer() is not designed to perform this, what is
> the best alternative?
> >
> > Thank you, and have a great start of the week!
> >
> > 叶翔 YE, Xiang
> > THINKING SPATIALLY<http://www.linkedin.com/in/spatialyexiang>.
> > Ph.D. in Spatial Statistics
> >
> >       [[alternative HTML version deleted]]
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> --
> Nick Bearman
> +44 (0) 7717745715
> nick using geospatialtrainingsolutions.co.uk
>
> Please let me know if I can make any adjustments related to disability or
> neurodivergence to improve how we interact.
>
> Due to my own life/work balance, you may get emails from me outside of
> normal working hours. Please do not feel any pressure to respond outside of
> your own working pattern.
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo using r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list