Skip to content

Commit 33765d8

Browse files
authored
factor out fun .to_sf() to convert crop,frame input to sf
Former-commit-id: c81bd49 Former-commit-id: c3a0f63cd685f8c84898ff2e669eddbf29388c54
1 parent 7c97756 commit 33765d8

2 files changed

Lines changed: 43 additions & 19 deletions

File tree

R/crop.R

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -209,31 +209,35 @@ setMethod("crop", "SpatialDataArray", \(x, y, j=1, ...) {
209209
if (ii) x[, i, j] else x[i, j]
210210
})
211211

212+
#' @importFrom sf st_sf st_sfc st_as_sfc st_bbox st_polygon st_geometry<-
213+
.to_sf <- \(x) {
214+
if (inherits(x, "sf")) {
215+
y <- x
216+
st_geometry(y) <- "geometry"
217+
} else if (inherits(x, "sfc")) {
218+
y <- st_sf(geometry=x)
219+
} else if (inherits(x, "sfg")) {
220+
y <- st_sf(geometry=st_sfc(x))
221+
} else if (inherits(x, "bbox")) {
222+
y <- st_sf(geometry=st_as_sfc(x))
223+
} else if (is.matrix(x)) {
224+
x <- .check_pol(x)
225+
y <- st_sf(geometry=st_sfc(st_polygon(list(x))))
226+
} else {
227+
.check_box(x)
228+
y <- st_sf(geometry=st_as_sfc(st_bbox(unlist(x))))
229+
}
230+
return(y)
231+
}
232+
212233
#' @export
213234
#' @rdname crop
214235
#' @importFrom dplyr pull .data
215236
#' @importFrom duckspatial ddbs_intersects
216-
#' @importFrom sf st_sf st_sfc st_as_sfc st_bbox st_polygon st_geometry<-
217237
setMethod("crop", "SpatialDataFrame", \(x, y, j=1, ...) {
218-
if (inherits(y, "sf")) {
219-
fd <- y
220-
st_geometry(fd) <- "geometry"
221-
} else if (inherits(y, "sfc")) {
222-
fd <- st_sf(geometry=y)
223-
} else if (inherits(y, "sfg")) {
224-
fd <- st_sf(geometry=st_sfc(y))
225-
} else if (inherits(y, "bbox")) {
226-
fd <- st_sf(geometry=st_as_sfc(y))
227-
} else if (is.matrix(y)) {
228-
mx <- .check_pol(y)
229-
fd <- st_sf(geometry=st_sfc(st_polygon(list(mx))))
230-
} else {
231-
# bounding box
232-
.check_box(y)
233-
fd <- st_sf(geometry=st_as_sfc(st_bbox(unlist(y))))
234-
}
238+
y <- .to_sf(y)
235239
df <- data(transform(x, j))
236-
fd <- data(SpatialDataShape(fd))
240+
fd <- data(SpatialDataShape(y))
237241
ok <- ddbs_intersects(df, fd, sparse=TRUE)
238242
x[pull(ok, .data$id_x), ]
239243
})

tests/testthat/test-crop.R

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ test_that("crop,sdLabel", {
9292
expect_equal(dim(m <- crop(l, y)), c(h, w))
9393
})
9494

95+
test_that("crop input 'y' .to_sf()", {
96+
ok <- \(x) {
97+
expect_is(x, "sf")
98+
expect_identical(names(x), "geometry")
99+
expect_no_error(SpatialDataShape(x))
100+
expect_equal(as.integer(st_bbox(x)), c(0,-1,2,1))
101+
}
102+
# from matrix
103+
m <- matrix(c(0,-1, 2,-1, 2,1, 0,1, 0,-1), ncol=2, byrow=TRUE)
104+
ok(.to_sf(m))
105+
# from 'sf(c)'
106+
y <- st_sfc(st_polygon(list(m)))
107+
ok(.to_sf(st_sf(y)))
108+
ok(.to_sf(y))
109+
# from 'bbox'
110+
y <- list(xmin=0, xmax=2, ymin=-1, ymax=1)
111+
ok(.to_sf(st_bbox(unlist(y))))
112+
ok(.to_sf(y))
113+
})
114+
95115
test_that("crop-box,sdPoint", {
96116
n <- length(p <- point(x))
97117
# this shouldn't do anything

0 commit comments

Comments
 (0)