Skip to content

Commit b8dc2b3

Browse files
committed
address comment from @bart1
1 parent 3ec76b8 commit b8dc2b3

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

R/sf.R

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ 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 = as.data.frame(lapply(x[coords], as.numeric))
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+
}
4856
if (na.fail && anyNA(cc))
4957
stop("missing values in coordinates not allowed")
5058
# classdim = getClassDim(rep(0, length(coords)), length(coords), dim, "POINT")
5159
# x$geometry = structure( points_rcpp(attr(x, "points"), dim),
52-
if (length(coords) == 2)
53-
dim = "XY"
54-
stopifnot(length(coords) == nchar(dim), dim %in% c("XY", "XYZ", "XYZM", "XYM"))
5560
points = as.matrix(cc)
5661
dimnames(points) = NULL
5762
x$geometry = structure(vector("list", length = nrow(cc)),

R/sfc.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,24 @@ st_sfc = function(..., crs = NA_crs_, precision = 0.0, check_ring_dir = FALSE, d
162162

163163

164164
#' @export
165-
#"[<-.sfc" = function (x, i, j, value) {
166165
"[<-.sfc" = function (x, i, value) {
167-
if (is.null(value) || inherits(value, "sfg"))
166+
if (is.null(value) || inherits(value, "sfg")) {
167+
is_points = inherits(value, "POINT")
168168
value = list(value)
169+
} else
170+
is_points = inherits(value, "sfc_POINT")
171+
if (inherits(x, "sfc_POINT") && !is.null(attr(x, "points"))) {
172+
if (is_points) {
173+
repl = if (!is.null(pts <- attr(value, "points")))
174+
pts
175+
else
176+
do.call(rbind, value)
177+
attr(x, "points")[i, ] = repl
178+
return(structure(x, n_empty = sum(is.na(attr(x, "points")[,1])))) # RETURNS
179+
} else
180+
x = x[] # realize
181+
}
169182
x = unclass(x) # becomes a list, but keeps attributes
170-
171183
ret = st_sfc(NextMethod(), recompute_bbox = TRUE)
172184
structure(ret, n_empty = sum(sfc_is_empty(ret)))
173185
}

R/tidyverse.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,13 @@ group_split.sf <- function(.tbl, ..., .keep = TRUE) {
5252
#' }
5353
filter.sf <- function(.data, ..., .dots) {
5454
agr = st_agr(.data)
55+
g = st_geometry(.data)
5556
class(.data) <- setdiff(class(.data), "sf")
56-
.re_sf(NextMethod(), sf_column_name = attr(.data, "sf_column"), agr)
57+
if (inherits(g, "sfc_POINT") && !is.null(pts <- attr(g, "points"))) {
58+
.data[[ attr(.data, "sf_column") ]] = pts
59+
st_as_sf(NextMethod(), coords = attr(.data, "sf_column"), agr = agr, remove = FALSE) # FIXME: doesn't handle tibble?
60+
} else
61+
.re_sf(NextMethod(), sf_column_name = attr(.data, "sf_column"), agr)
5762
}
5863

5964
#' @name tidyverse

0 commit comments

Comments
 (0)