Skip to content

Commit

Permalink
fixes #2357
Browse files Browse the repository at this point in the history
  • Loading branch information
edzer committed Apr 13, 2024
1 parent c7e9d30 commit 32aa6c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
25 changes: 16 additions & 9 deletions R/sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ 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
#' there is more than one and \code{sf_column_name} is \code{NULL}, the first one is taken.
#' @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))
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 3 additions & 1 deletion man/st_as_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 32aa6c8

Please sign in to comment.