Last active
March 14, 2019 05:13
-
-
Save mdsumner/2be160dcd66f35893e4e7c926f264c22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
st_recenter <- st_recentre <- function(x, clon = NULL, ..., tryfix = TRUE) { | |
if (is.null(clon)) return(x) | |
if (!st_is_longlat(x)) stop("recentring not appropriate for non longlat data") | |
## try to fix problematic geometry | |
if (tryfix) { | |
if (all(grepl("POLYGON", st_geometry_type(x)))) x <- suppressWarnings(st_buffer(sf::st_as_sf(x), 0)) | |
x <- st_wrap_dateline(x, options = c("WRAPDATELINE=YES", "DATELINEOFFSET=180")) | |
} | |
wbox <- st_as_sfc(st_bbox(c(xmin = -180, ymin = -90, xmax = (clon)%%360 - 180, ymax = 90), crs = st_crs(x))) | |
west <- suppressWarnings(st_intersection(x, wbox)) | |
west <- st_set_geometry(west, st_geometry(west) + c(360, 0)) | |
west <- st_set_crs(west, st_crs(x)) | |
east <- suppressWarnings(st_crop(x, st_bbox(c(xmin = (clon)%%360 - 180, xmax = 180, ymin = -90, ymax = 90)))) | |
xx <- rbind( | |
west, east | |
) | |
## ensure geometries are of consistent type | |
xx <- sf::st_cast(xx) | |
xx | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few improvements