diff --git a/NEWS.md b/NEWS.md index 6ce0bb3b1..a509214ef 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # version 1.0-17 +* when `dim` is not `XYZM`, `sf_as_sf.data.frame()` interprets a length 4 `coords` argument to specify the corners of a rectangular polygon; #2357 + * `st_interpolate_aw()` gains an `na.rm` argument, for removing features with `NA` attributes before interpolating; #830 # version 1.0-16 diff --git a/R/sf.R b/R/sf.R index 4daabb7be..846575b32 100644 --- a/R/sf.R +++ b/R/sf.R @@ -10,7 +10,7 @@ st_as_sf = function(x, ...) UseMethod("st_as_sf") #' @param agr character vector; see details section of \link{st_sf} #' @param coords in case of point data: names or numbers of the numeric columns holding coordinates #' @param wkt name or number of the character column that holds WKT encoded geometries -#' @param dim passed on to \link{st_point} (only when argument coords is given) +#' @param dim specify what 3- or 4-dimensional points reflect: passed on to \link{st_point} (only when argument coords is given) #' @param remove logical; when coords or wkt is given, remove these columns from data.frame? #' @param na.fail logical; if \code{TRUE}, raise an error if coordinates contain missing values #' @param sf_column_name character; name of the active list-column with simple feature geometries; in case @@ -18,6 +18,8 @@ st_as_sf = function(x, ...) UseMethod("st_as_sf") #' @param ... passed on to \link{st_sf}, might included named arguments \code{crs} or \code{precision} #' @details setting argument \code{wkt} annihilates the use of argument \code{coords}. If \code{x} contains a column called "geometry", \code{coords} will result in overwriting of this column by the \link{sfc} geometry list-column. Setting \code{wkt} will replace this column with the geometry list-column, unless \code{remove} is \code{FALSE}. #' +#' If `coords` has length 4, and `dim` is not `XYZM`, the four columns are taken as the xmin, ymin, xmax, ymax corner coordinates of a rectangle, and polygons are returned. +#' #' @examples #' pt1 = st_point(c(0,1)) #' pt2 = st_point(c(1,1)) @@ -50,14 +52,19 @@ st_as_sf.data.frame = function(x, ..., agr = NA_agr_, coords, wkt, # classdim = getClassDim(rep(0, length(coords)), length(coords), dim, "POINT") if (is.null(sf_column_name)) sf_column_name = "geometry" - x[[sf_column_name]] = structure( points_rcpp(as.matrix(cc), dim), - n_empty = 0L, precision = 0, crs = NA_crs_, - bbox = structure( - c(xmin = min(cc[[1]], na.rm = TRUE), - ymin = min(cc[[2]], na.rm = TRUE), - xmax = max(cc[[1]], na.rm = TRUE), - ymax = max(cc[[2]], na.rm = TRUE)), class = "bbox"), - class = c("sfc_POINT", "sfc" ), names = NULL) + x[[sf_column_name]] = if (nchar(dim) < 4 && ncol(cc) == 4) { # create POLYGONs: + fn = function(x) st_as_sfc(st_bbox(c(xmin = x[[1]], ymin = x[[2]], xmax = x[[3]], ymax = x[[4]]))) + do.call(c, apply(as.matrix(cc), 1, fn)) + } else { # points: + structure( points_rcpp(as.matrix(cc), dim), + n_empty = 0L, precision = 0, crs = NA_crs_, + bbox = structure( + c(xmin = min(cc[[1]], na.rm = TRUE), + ymin = min(cc[[2]], na.rm = TRUE), + xmax = max(cc[[1]], na.rm = TRUE), + ymax = max(cc[[2]], na.rm = TRUE)), class = "bbox"), + class = c("sfc_POINT", "sfc" ), names = NULL) + } if (remove) { if (is.character(coords)) diff --git a/man/st_as_sf.Rd b/man/st_as_sf.Rd index 7dbe4e908..ec676de9b 100644 --- a/man/st_as_sf.Rd +++ b/man/st_as_sf.Rd @@ -54,7 +54,7 @@ st_as_sf(x, ...) \item{wkt}{name or number of the character column that holds WKT encoded geometries} -\item{dim}{passed on to \link{st_point} (only when argument coords is given)} +\item{dim}{specify what 3- or 4-dimensional points reflect: passed on to \link{st_point} (only when argument coords is given)} \item{remove}{logical; when coords or wkt is given, remove these columns from data.frame?} @@ -74,6 +74,8 @@ Convert foreign object to an sf object } \details{ setting argument \code{wkt} annihilates the use of argument \code{coords}. If \code{x} contains a column called "geometry", \code{coords} will result in overwriting of this column by the \link{sfc} geometry list-column. Setting \code{wkt} will replace this column with the geometry list-column, unless \code{remove} is \code{FALSE}. + +If \code{coords} has length 4, and \code{dim} is not \code{XYZM}, the four columns are taken as the xmin, ymin, xmax, ymax corner coordinates of a rectangle, and polygons are returned. } \examples{ pt1 = st_point(c(0,1))