Skip to contents

Apply sf::st_make_valid() to invalid ROIs (which also drops duplicate consecutive vertices) and clamp out-of-bounds vertices into the image bounds, reporting what changed.

Usage

sg_roi_fix_geometry(project, verbose = TRUE, call = rlang::caller_env())

Arguments

project

An sg_roi_project.

verbose

Logical; whether to report repairs. Default TRUE.

call

The calling environment, for error reporting.

Value

The sg_roi_project with repaired geometry. Repairs are reported via cli::cli_inform() when verbose is TRUE. Issues that cannot be repaired (zero area, degenerate, NA coordinates) are left in place and reported.

Examples

# a rectangle reaching past the image edge is clamped into the image
lyr <- sg_roi_layer_add(sg_roi_layer("tissue"),
                        sg_roi_rect(-2, -2, 8, 8, label = "tissue"))
proj <- sg_roi_project(sg_roi_frame(c(20, 20)), lyr)
fixed <- sg_roi_fix_geometry(proj)
#> ✔ Repaired 1 ROI.
sg_roi_rois(fixed)$area_px
#> [1] 64