1

I'm trying to generate a coastline buffer by using st_buffer() with data from the rnaturalearth library. However, when I create the buffer, it extends outside the realm of the projection, rather than wrapping around to to the other "side." This creates problems for downstream shape use. Other coastline buffer problems (e.g. 1, 2) on GIS SE have not involved issues of projection edges or not been in R.

library(rgdal)
library(rnaturalearth)
library(sf)
library(ggplot2)

robin = CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

ROI = ne_countries(returnclass = 'sf') %>%
  st_combine() %>%
  st_transform(robin)
coastalWaters = ROI %>%
  st_buffer(500000)
ggplot() +
  geom_sf(data = coastalWaters, fill = "lightblue", col = "transparent") +
  geom_sf(data = ROI)

The reproducible code above leads to a plot that looks like this (red circles annotated by me):

bufferIssue

So, my question is, how can I buffer a global set of polygons with an "understood" wrapping? Solutions will preferably use the sf library of functions.

1
  • 1
    In the sf package, I believe there is an st_wrap_dateline function, maybe that could help solving the issue? – FSimardGIS Sep 10 '20 at 0:08
2

Edit: added picture of result

As FSimardGIS mentioned in the comments, the key here is to use st_wrap_dateline. The trick is that you can't use st_transformation before creating the buffer because (I think) it converts it to the projection's coordinates, not the lat/lon which st_wrap_dateline requires.

st_buffer then wants the buffer distance in arc degrees, so I threw in a quick calculation to convert km to arc seconds, but might need to be refined.

I then added coord_sf to ggplot so that ggplot could do the projection for the visual.

library(rgdal)
library(rnaturalearth)
library(sf)
library(ggplot2)

robin = CRS("+proj=robin +lon_0=0 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs")

ROI = ne_countries(returnclass = 'sf') %>%
  st_combine() 

#Please check the math on this part.
#KM/earth circumference * degrees in circle
buffer_in_km <- 500
buffer_as_arc_degrees<- buffer_in_km/40075*360

coastalWaters = ROI %>%
  st_buffer(buffer_as_arc_degrees)  %>% st_wrap_dateline()

ggplot() +
  geom_sf(data = coastalWaters, fill = "lightblue", col = "transparent") +
  geom_sf(data = ROI) +
  coord_sf(crs = robin)

enter image description here

5
  • Using st_wrap_dateline() here results in a weird result. It looks like every section of the coastalWaters shape that overlaps with the dateline is lost at the actual buffer position, and creates latitudinal "belts" across the projection instead. Question updated to include an image. – JepsonNomad Sep 11 '20 at 16:34
  • did you copy/paste the code exactly and then run it? I had a similar issue when I was looking into this. It's related to st_wrap_dateline() not being used correctly. If you can post your updated code, we might be able to identify the issue. Also, check for error messages when running. st_wrap_dateline() is probably throwing an error. – Roger-123 Sep 11 '20 at 17:26
  • 1
    Yes, I copied your exact code. There were a few gdal warnings but the relevant messages are probably these two: "st_buffer does not correctly buffer longitude/latitude data" and "bounding box has potentially an invalid value range for longlat data" - leading me to believe that the "wrap" was converted to a "belt" because of the bounding box issue – JepsonNomad Sep 11 '20 at 17:49
  • 1
    Ah - it is the gdal issue - related to this: github.com/r-spatial/sf/issues/1207. Marking your answer as resolved because my problem now appears to be software, not workflow. – JepsonNomad Sep 11 '20 at 17:57
  • st_buffer does say that about long/lat but I've still seen that method used on spatial data frequently, so I'm not sure if that's a real issue or more that it's close but not exact. I get both of those issues you mentioned. Not sure about the first, but I think the second is the issue that st_wrap_dateline is supposed to clean up. Looking at your link, you might be on to something as to why your st_wrap_dateline() is behaving differently than mine. Good call on figuring that one out. – Roger-123 Sep 11 '20 at 18:03

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.