diff --git a/NAMESPACE b/NAMESPACE index 20a1198..d971d36 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -17,6 +17,7 @@ export(as_sf_class) export(as_sf_list) export(as_sfc) export(as_startpoint) +export(as_wgs84) export(as_xy) export(bind_units_col) export(check_coords) @@ -68,6 +69,7 @@ export(is_sfc) export(is_sfg) export(is_shorter) export(is_sp) +export(is_wgs84) export(list_data_files) export(lonlat_to_sfc) export(make_filename) diff --git a/R/as_sf.R b/R/as_sf.R index 97552c2..65ac29d 100644 --- a/R/as_sf.R +++ b/R/as_sf.R @@ -26,9 +26,10 @@ #' @param ... Additional parameters passed to [sf::st_bbox()] when calling #' [as_bbox()] or passed to [sf::st_sf()], [sf::st_as_sf()], or [df_to_sf()] #' for [as_sf()] (depending on class of x) +#' @example examples/as_sf.R #' @export -#' @importFrom sf st_sf st_as_sfc st_bbox st_as_sf st_geometry -#' @importFrom dplyr bind_rows rename +#' @importFrom dplyr case_when bind_rows +#' @importFrom sf st_sf st_sfc st_as_sfc st_bbox st_as_sf st_geometry as_sf <- function(x, crs = NULL, sf_col = "geometry", @@ -129,7 +130,8 @@ as_bbox <- function(x, crs = NULL, ext = TRUE, ...) { #' @name as_sfc #' @rdname as_sf #' @export -#' @importFrom sf st_geometry st_as_sfc +#' @importFrom dplyr case_when +#' @importFrom sf st_geometry st_sfc st_as_sfc as_sfc <- function(x, crs = NULL, ext = TRUE, ...) { if (is_sfc(x)) { return(transform_sf(x, crs = crs)) @@ -170,7 +172,11 @@ as_sfc <- function(x, crs = NULL, ext = TRUE, ...) { #' @export #' @importFrom dplyr summarize group_keys group_nest #' @importFrom janitor make_clean_names -as_sf_list <- function(x, nm = "data", col = NULL, crs = NULL, clean_names = TRUE) { +as_sf_list <- function(x, + nm = "data", + col = NULL, + crs = NULL, + clean_names = TRUE) { check_null(x) check_string(col, allow_null = TRUE) @@ -239,7 +245,13 @@ as_sf_list <- function(x, nm = "data", col = NULL, crs = NULL, clean_names = TRU #' @inheritParams as_sf_list #' @export #' @importFrom dplyr mutate -make_sf_grid_list <- function(x, style = "rect", ncol = 2, nrow = 2, .id = "grid_id", crs = NULL, ...) { +make_sf_grid_list <- function(x, + style = "rect", + ncol = 2, + nrow = 2, + .id = "grid_id", + crs = NULL, + ...) { grid <- st_make_grid_ext(x, style = style, ncol = ncol, nrow = nrow, .id = .id, ...) x <- st_join_ext(x, grid, largest = TRUE) @@ -267,7 +279,11 @@ make_sf_grid_list <- function(x, style = "rect", ncol = 2, nrow = 2, .id = "grid #' @name as_sf_class #' @rdname as_sf #' @export -as_sf_class <- function(x, class = NULL, allow_null = TRUE, call = caller_env(), ...) { +as_sf_class <- function(x, + class = NULL, + allow_null = TRUE, + call = caller_env(), + ...) { if (is.null(class) && is_true(allow_null)) { return(x) } @@ -316,6 +332,18 @@ as_crs <- function(crs = NULL, check = FALSE, call = parent.frame()) { ) } +#' @name as_crs +#' @rdname as_sf +#' @export +as_wgs84 <- function(x) { + if (is_wgs84(x)) { + return(x) + } + + st_transform_ext(x, 4326) +} + + #' Convert data to a data frame with X/Y coordinate pairs #' #' Wraps [as_points()], [as_sfc()], and [sf_bbox_point()] to allow the diff --git a/R/is_sf.R b/R/is_sf.R index b541867..d89e2ea 100644 --- a/R/is_sf.R +++ b/R/is_sf.R @@ -15,10 +15,11 @@ #' - [is_raster]: is x a `Raster` class object? #' - [is_sp]: is x a `Spatial` class object of any type? #' - [is_geo_coords]: is x likely a geodetic coordinate pair (a length 2 numeric vector, with a max absolute value less than or equal to 180)? +#' - [is_wgs84]: is x using the [WSG84](https://en.wikipedia.org/wiki/World_Geodetic_System) coordinate reference system? #' - [is_same_crs]: do x and y have the same coordinate reference system? #' +#' @example examples/is_sf.R #' @export -#' @md is_sf <- function(x, ext = FALSE, allow_null = FALSE, allow_list = FALSE) { classes <- "sf" @@ -116,10 +117,19 @@ is_geo_coords <- function(x, allow_null = FALSE) { is.numeric(x) && (length(x) == 2) && (max(abs(x)) <= 180) } +#' @name is_wgs84 +#' @rdname is_sf +#' @export +is_wgs84 <- function(x) { + is_same_crs(x, 4326) +} + #' @name is_same_crs #' @rdname is_sf #' @importFrom sf st_crs #' @export is_same_crs <- function(x, y) { - sf::st_crs(x) == sf::st_crs(y) + crs_x <- sf::st_crs(x) + crs_y <- sf::st_crs(y) + (crs_x == crs_y) || (crs_x$input == crs_y$input) } diff --git a/examples/as_sf.R b/examples/as_sf.R new file mode 100644 index 0000000..71dba4c --- /dev/null +++ b/examples/as_sf.R @@ -0,0 +1,12 @@ +nc <- read_sf_ext(system.file("shape/nc.shp", package = "sf")) +nc[["category"]] <- sample(c("A", "B", "C", "D", "E"), nrow(nc), replace = TRUE) + +as_sf(nc$geometry) + +as_bbox(nc) + +as_sf_list(nc, col = "category") + +as_crs(nc) + +as_xy(nc[1,]) diff --git a/examples/is_sf.R b/examples/is_sf.R new file mode 100644 index 0000000..db48bf5 --- /dev/null +++ b/examples/is_sf.R @@ -0,0 +1,13 @@ +nc <- read_sf_ext(system.file("shape/nc.shp", package = "sf")) + +is_sf(nc) + +is_sfc(nc$geometry) + +is_sf_list(list(nc[1, ], nc[2, ])) + +is_bbox(sf::st_bbox(nc)) + +is_wgs84(sf::st_transform(nc, 4326)) + +is_same_crs(nc, 4267) diff --git a/man/as_sf.Rd b/man/as_sf.Rd index d9a6831..b102142 100644 --- a/man/as_sf.Rd +++ b/man/as_sf.Rd @@ -7,6 +7,7 @@ \alias{as_sf_list} \alias{as_sf_class} \alias{as_crs} +\alias{as_wgs84} \title{Convert an object to a simple feature or bounding box object} \usage{ as_sf(x, crs = NULL, sf_col = "geometry", ext = TRUE, ...) @@ -20,6 +21,8 @@ as_sf_list(x, nm = "data", col = NULL, crs = NULL, clean_names = TRUE) as_sf_class(x, class = NULL, allow_null = TRUE, call = caller_env(), ...) as_crs(crs = NULL, check = FALSE, call = parent.frame()) + +as_wgs84(x) } \arguments{ \item{x}{A \code{sf}, \code{bbox}, \code{sfc}, \code{raster}, \code{sp}, or data frame object that can @@ -75,3 +78,17 @@ is applied. If a character object is passed to \code{\link[=as_bbox]{as_bbox()}} \code{\link[osmdata:getbb]{osmdata::getbb()}} using \code{format_out = "matrix"} which is converted into a bounding box. } +\examples{ +nc <- read_sf_ext(system.file("shape/nc.shp", package = "sf")) +nc[["category"]] <- sample(c("A", "B", "C", "D", "E"), nrow(nc), replace = TRUE) + +as_sf(nc$geometry) + +as_bbox(nc) + +as_sf_list(nc, col = "category") + +as_crs(nc) + +as_xy(nc[1,]) +} diff --git a/man/is_sf.Rd b/man/is_sf.Rd index a68b82f..3ccb274 100644 --- a/man/is_sf.Rd +++ b/man/is_sf.Rd @@ -10,6 +10,7 @@ \alias{is_sp} \alias{is_coords} \alias{is_geo_coords} +\alias{is_wgs84} \alias{is_same_crs} \title{What is the class or spatial attributes of this feature?} \usage{ @@ -29,6 +30,8 @@ is_sp(x, allow_null = FALSE) is_geo_coords(x, allow_null = FALSE) +is_wgs84(x) + is_same_crs(x, y) } \arguments{ @@ -58,6 +61,22 @@ What is the class or spatial attributes of this feature? \item \link{is_raster}: is x a \code{Raster} class object? \item \link{is_sp}: is x a \code{Spatial} class object of any type? \item \link{is_geo_coords}: is x likely a geodetic coordinate pair (a length 2 numeric vector, with a max absolute value less than or equal to 180)? +\item \link{is_wgs84}: is x using the \href{https://en.wikipedia.org/wiki/World_Geodetic_System}{WSG84} coordinate reference system? \item \link{is_same_crs}: do x and y have the same coordinate reference system? } } +\examples{ +nc <- read_sf_ext(system.file("shape/nc.shp", package = "sf")) + +is_sf(nc) + +is_sfc(nc$geometry) + +is_sf_list(list(nc[1, ], nc[2, ])) + +is_bbox(sf::st_bbox(nc)) + +is_wgs84(sf::st_transform(nc, 4326)) + +is_same_crs(nc, 4267) +}