@@ -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<-
217237setMethod ("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})
0 commit comments