Skip to content

Commit bc97892

Browse files
committed
factor out fun to conver crop input to sf
1 parent fb652db commit bc97892

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

R/crop.R

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

212+
.to_sf <- \(x) {
213+
if (inherits(x, "sf")) {
214+
y <- x
215+
st_geometry(y) <- "geometry"
216+
} else if (inherits(x, "sfc")) {
217+
y <- st_sf(geometry=x)
218+
} else if (inherits(x, "sfg")) {
219+
y <- st_sf(geometry=st_sfc(x))
220+
} else if (inherits(x, "bbox")) {
221+
y <- st_sf(geometry=st_as_sfc(x))
222+
} else if (is.matrix(x)) {
223+
x <- .check_pol(x)
224+
y <- st_sf(geometry=st_sfc(st_polygon(list(x))))
225+
} else {
226+
# bounding box
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
216237
#' @importFrom sf st_sf st_sfc st_as_sfc st_bbox st_polygon st_geometry<-
217238
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-
}
239+
y <- .to_sf(y)
235240
df <- data(transform(x, j))
236-
fd <- data(SpatialDataShape(fd))
241+
fd <- data(SpatialDataShape(y))
237242
ok <- ddbs_intersects(df, fd, sparse=TRUE)
238243
x[pull(ok, .data$id_x), ]
239244
})

0 commit comments

Comments
 (0)