Skip to content

Commit

Permalink
Allow predicates in crop, closing #6
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdamoses committed Aug 24, 2023
1 parent 07c2ce3 commit d68d372
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
22 changes: 14 additions & 8 deletions R/geometry_operation.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,22 @@ st_n_intersects <- function(x, y) st_n_pred(x, y, st_intersects)
.g <- gs[[s]][, c("geometry", id_col)]
st_agr(.g) <- "constant"
o <- op(.g, y_use)
# Aggregate in case cropping broke some items into multiple pieces
if (any(!rownames(o) %in% rownames(.g))) {
o <- aggregate(o,
by = setNames(list(id = o[[id_col]]), id_col),
FUN = unique
)
# If it's a predicate
if (is(o, "sgbp")) {
inds <- lengths(o) > 0L
return(gs[[s]][inds,])
} else {
# Aggregate in case cropping broke some items into multiple pieces
if (any(!rownames(o) %in% rownames(.g))) {
o <- aggregate(o,
by = setNames(list(id = o[[id_col]]), id_col),
FUN = unique
)
}
return(merge(o, st_drop_geometry(gs[[s]]), by = id_col, all = TRUE))
}
merge(o, st_drop_geometry(gs[[s]]), by = id_col, all = TRUE)
} else {
gs[[s]]
return(gs[[s]])
}
})
gs_sub <- do.call(rbind, gs_sub)
Expand Down
9 changes: 7 additions & 2 deletions tests/testthat/test-geometry_operation.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,13 @@ test_that("When a sample is removed by cropping", {
expect_equal(sampleIDs(sfe_cropped), "sample01")
})

test_that("Crop to subset with intersection without cropping geometries", {

test_that("Use geometry predicate to crop", {
sfe_cropped <- crop(sfe_visium, bbox_use, sample_id = "all", op = st_intersects)
cg <- spotPoly(sfe_cropped, "all")
expect_true(all(st_any_pred(cg, bbox_use, pred = st_intersects)))
# Not actually cropped
expect_false(all(st_any_pred(cg, bbox_use, pred = st_covered_by)))
expect_true(st_geometry_type(cg, by_geometry = FALSE) == "POLYGON")
})

annotGeometry(sfe_visium, "bbox", sample_id = "sample01") <- bbox_sf
Expand Down

0 comments on commit d68d372

Please sign in to comment.