1

I'm trying to create a buffer around a multi-linestring object with st_buffer.

Here's my initial attempt:

library(rnaturalearth) library(dplyr) library(sf) library(rmapshaper)

proj_sahel <- "+proj=aea +lat_1=9.71 +lat_2=22.58 +lon_0=2.81"

boundaries <- ne_download(scale = 10, category = "cultural", type = "admin_0_boundary_lines_land", returnclass = "sf")

buffer50 <- boundaries %>% filter(adm0_a3_l == "MLI" & adm0_a3_r %in% c("BFA", "NER")) %>% st_transform(crs = proj_sahel) %>% st_buffer(dist = 50000) %>% st_cast("POLYGON")

It works fine except for the unfortunate artifact right in the middle of the buffer zone as you can see below: buffer artifact

I tried simplifying the geometry before and after st_transform with ms_simplify, it didn't make any difference. How can I avoid this or clean it after the fact?

  • That is not an artifact, you in fact, have two geometries in your line data and the function is buffering both of them. Have you looked at the dim() of the boundaries object or the result of applying the dplyr::filter function to it? You could try rgeos::gBuffer with byid = FALSE. – Jeffrey Evans Apr 29 at 16:46
  • Where is proj_sahel defined? – Spacedman Apr 29 at 16:48
  • 1
    Please don't use library(tidyverse) - be minimal and only specify packages you really need. I think here you are only using library(dplyr). Attaching all the tidyverse packages is not best practice. – Spacedman Apr 29 at 16:49
1

Construct the union of your buffered features:

> plot(buffer$geom)

enter image description here

> plot(st_union(buffer$geom))

enter image description here

  • Thanks! Can't believe I didn't realise this. As for the comment about best practices and the definition of proj_sahel you're right, I do make use of the rest of the packages, this was just a lazy copy/paste of a tiny portion of code. I'll edit the question. – zouav Apr 29 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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