Skip to content

Commit 203187d

Browse files
committed
filter empty point; #2059
1 parent b8dc2b3 commit 203187d

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

R/sf.R

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,24 @@ st_as_sf.data.frame = function(x, ..., agr = NA_agr_, coords, wkt,
4444
else
4545
x$geometry = st_as_sfc(as.character(x[[wkt]]))
4646
} else if (! missing(coords)) {
47-
cc = if (length(coords) == 1) {
48-
stopifnot(is.matrix(x[[coords]]), is.numeric(x[[coords]]))
49-
x[[coords]]
50-
} else {
51-
if (length(coords) == 2)
52-
dim = "XY"
53-
stopifnot(length(coords) == nchar(dim), dim %in% c("XY", "XYZ", "XYZM", "XYM"))
54-
as.data.frame(lapply(x[coords], as.numeric))
55-
}
56-
if (na.fail && anyNA(cc))
57-
stop("missing values in coordinates not allowed")
58-
# classdim = getClassDim(rep(0, length(coords)), length(coords), dim, "POINT")
59-
# x$geometry = structure( points_rcpp(attr(x, "points"), dim),
60-
points = as.matrix(cc)
61-
dimnames(points) = NULL
47+
if (length(coords) == 1) {
48+
stopifnot(is.matrix(x[[coords]]), is.numeric(x[[coords]]))
49+
cc = x[[coords]]
50+
} else {
51+
if (length(coords) == 2)
52+
dim = "XY"
53+
stopifnot(length(coords) == nchar(dim), dim %in% c("XY", "XYZ", "XYZM", "XYM"))
54+
cc = do.call(cbind, lapply(x[coords], as.numeric))
55+
if (na.fail && anyNA(cc))
56+
stop("missing values in coordinates not allowed")
57+
}
58+
dimnames(cc) = NULL
6259
x$geometry = structure(vector("list", length = nrow(cc)),
63-
points = points,
60+
points = cc,
6461
points_dim = dim,
6562
n_empty = 0L, precision = 0, crs = NA_crs_,
66-
bbox = structure(
67-
c(xmin = min(cc[[1]], na.rm = TRUE),
68-
ymin = min(cc[[2]], na.rm = TRUE),
69-
xmax = max(cc[[1]], na.rm = TRUE),
70-
ymax = max(cc[[2]], na.rm = TRUE)), class = "bbox"),
71-
class = c("sfc_POINT", "sfc"), names = NULL)
63+
bbox = bbox.pointmatrix(cc),
64+
class = c("sfc_POINT", "sfc"), names = NULL)
7265

7366
if (is.character(coords))
7467
coords = match(coords, names(x))

0 commit comments

Comments
 (0)