Skip to content

Commit 7b97380

Browse files
committed
[.sfc and [<-.sfc can deal with matrix/array lists
1 parent 5cdd168 commit 7b97380

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# version 1.0-17
22

3+
* `[.sfc` and `[<-.sfc` use matrix/array type subsetting for `sfc` objects that have a `dim` attribute
4+
35
* add `st_exterior_ring()` to extract exterior rings (remove holes); #2406
46

57
* add `text.sf()`, `text.sfc()`, `points.sf()`, `points.sfc()` to annotate base plots at geometry centroids; #2399

R/sfc.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,27 @@ st_sfc = function(..., crs = NA_crs_, precision = 0.0, check_ring_dir = FALSE, d
143143
#' @name sfc
144144
#' @param x object of class \code{sfc}
145145
#' @param i record selection. Might also be an \code{sfc}/\code{sf} object to work with the \code{op} argument
146-
#' @param j ignored
146+
#' @param j ignored if `op` is specified
147147
#' @param op function, geometrical binary predicate function to apply when
148148
#' \code{i} is a \code{sf}/\code{sfc} object. Additional arguments can be
149149
#' specified using \code{...}, see examples.
150+
#' @details if `x` has a `dim` attribute (i.e. is an `array` or `matrix`) then `op` cannot be used.
150151
#' @export
151152
"[.sfc" = function(x, i, j, ..., op = st_intersects) {
152-
if (!missing(i) && (inherits(i, "sf") || inherits(i, "sfc") || inherits(i, "sfg")))
153-
i = lengths(op(x, i, ...)) != 0
154153
precision = st_precision(x)
155154
crs = st_crs(x)
156155
dim = if (length(x)) class(x[[1]])[1] else "XY"
157-
x = unclass(x)[i] # now a list
158-
st_sfc(x, crs = crs, precision = precision, dim = dim)
156+
if (!missing(i) && (inherits(i, "sf") || inherits(i, "sfc") || inherits(i, "sfg"))) {
157+
i = lengths(op(x, i, ...)) != 0
158+
st_sfc(unclass(x)[i], crs = crs, precision = precision, dim = dim)
159+
} else
160+
st_sfc(NextMethod(), crs = crs, precision = precision, dim = dim)
159161
}
160162

161-
162163
#' @export
163-
#"[<-.sfc" = function (x, i, j, value) {
164-
"[<-.sfc" = function (x, i, value) {
164+
"[<-.sfc" = function (x, i, j, ..., value) {
165165
if (is.null(value) || inherits(value, "sfg"))
166166
value = list(value)
167-
x = unclass(x) # becomes a list, but keeps attributes
168-
169167
ret = st_sfc(NextMethod(), recompute_bbox = TRUE)
170168
structure(ret, n_empty = sum(sfc_is_empty(ret)))
171169
}
@@ -204,6 +202,8 @@ print.sfc = function(x, ..., n = 5L, what = "Geometry set for", append = "") {
204202
if (ne > 0)
205203
cat(paste0(" (with ", ne, ifelse(ne > 1, " geometries ", " geometry "), "empty)"))
206204
}
205+
if (!is.null(dim(x)))
206+
cat(paste0(" [dim: ", paste(dim(x), collapse = " x "), "]"))
207207
cat("\n")
208208
if (length(x)) {
209209
cat(paste0("Geometry type: ", cls, "\n"))

man/sfc.Rd

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sf.Rproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ RnwWeave: knitr
1313
LaTeX: XeLaTeX
1414

1515
AutoAppendNewline: Yes
16-
StripTrailingWhitespace: No
1716

1817
BuildType: Package
1918
PackageUseDevtools: Yes

0 commit comments

Comments
 (0)