5

I have created polygons with CRS = UTM (see here), which I need to transform to latitude/longitude. Some of my polygons cross the dateline, which causes the transformation to create a polygon that crosses the entire globe.

library(sp)
library(rgeos)

points <- data.frame(x = c(-179.5, 0), y = c(-50, -30))
coordinates(points) <- ~x+y
proj4string(points) <- '+init=epsg:4326'

points_sp <- spTransform(points, CRS('+init=epsg:32621')) # wrong zone, but ignored for example
# expand point to circle
circles <- gBuffer(points_sp, width = 1e+5)
# transform back to WGS84
circles_latlon <- spTransform(circles, CRS(proj4string(points)))

par(mfrow = c(1, 2))
plot(circles, axes = TRUE, main = 'UTM')
plot(circles_latlon, axes = TRUE, main = 'WGS84')

enter image description here

Is there a solution to split the polygons? Or and alternative solution that takes care of the dateline issue?

1

1 Answer 1

2
+50

How about using st_wrap_dateline from the sf package?

library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.4, proj.4 4.9.3
point1 <- st_point(c(-179.5, -50))
point2 <- st_point(c(0, -30))
points <- st_sfc(point1, point2, crs = 4236)

points_trans <- st_transform(points, 32621)
circles <- st_buffer(points_trans, 100000)
circles_latlon <- st_transform(circles, 4326)
circles_latlon2 <- st_wrap_dateline(circles_latlon, options = c("WRAPDATELINE=YES"))

par(mfrow = c(1, 3))
plot(circles, axes = TRUE, main = 'UTM')
plot(circles_latlon, axes = TRUE, main = 'WGS84')
plot(circles_latlon2, axes = TRUE, main = 'WGS84 with wrapdateline')

# check the results
mapview::mapview(circles_latlon2)

You can convert the output to a Spatial object with:

circles_latlon2_sp <- as(circles_latlon2, "Spatial")
4
  • Thanks, this could work if I convert back to spatialPolygons after (in order to use it with raster::extract). However, I get: Warning message: In CPL_wrap_dateline(x, options, quiet) : GDAL Error 6: GEOS support not enabled. When I load sf I get Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3. Any clue how to address this?
    – Lukas
    Sep 11, 2018 at 17:57
  • Thanks again. The conversion wasn't the problem. It was the GDAL error, which causes the circles_latlon2 <- st_wrap_dateline(circles_latlon, options = c("WRAPDATELINE=YES")) to fail. Do you have a suggestion how to enable GEOS support?
    – Lukas
    Sep 13, 2018 at 15:07
  • Sadly, I do not have an answer to this issue..
    – Jot eN
    Sep 13, 2018 at 19:13
  • I solved it. See link
    – Lukas
    Sep 18, 2018 at 13:21

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.