Transforms both layers to a projected CRS, keeps features in x that touch the mask y, computes sf::st_intersection(), aggregates clipped area per identifier, and drops features whose clipped fraction of their original area is below min_area_ratio.

intersect_mask_filter_area(
  x,
  y,
  x_id = NULL,
  crs = NULL,
  min_area_ratio = 0.01,
  repair = TRUE
)

Arguments

x

An sf::sf object with POLYGON or MULTIPOLYGON geometries.

y

An sf::sf mask layer with polygon geometries.

x_id

Name of the column in x with unique identifiers. If NULL (default), a column .row_id is added (row order after reprojection).

crs

Target projected CRS for area and intersection, from sf::st_crs(). If NULL (default), a SIRGAS 2000 Albers (Brazil) definition in metre units is used. Pass another projected CRS with meaningful area units when working outside Brazil.

min_area_ratio

Numeric in (0, 1]: keep a feature when area_clip / area_full is greater than or equal to this value. Default 0.01 (about 1% of the feature area inside the mask).

repair

If TRUE, apply sf::st_make_valid() to x and y after transforming (warnings are suppressed per call).

Value

A list with clipped, an sf::sf object with intersection geometries that passed the threshold, and summary, a dplyr::tibble() with the ID column, area_full, area_clip, area_ratio, and logical keep.

Details

For each feature in x, area_full is its area before clipping and area_clip is the sum of areas from intersecting x with y. The ratio summary$area_ratio is area_clip / area_full: the fraction of each x feature that falls inside y (not the fraction of y covered by x). Only polygon geometries are supported for x and y: points and lines are not meaningful for an area ratio. For example, min_area_ratio = 0.5 retains a feature only when at least half of its area overlaps the mask; the default 0.01 drops only very small edge overlaps.

See also

Examples

# \donttest{
ring <- matrix(
  c(0, 0, 1e6, 0, 1e6, 1e6, 0, 1e6, 0, 0),
  ncol = 2L,
  byrow = TRUE
)
crs_pl <- sf::st_crs(3857)
y <- sf::st_sf(geometry = sf::st_sfc(sf::st_polygon(list(ring)), crs = crs_pl))
inner <- matrix(
  c(1e5, 1e5, 9e5, 1e5, 9e5, 9e5, 1e5, 9e5, 1e5, 1e5),
  ncol = 2L,
  byrow = TRUE
)
x <- sf::st_sf(
  id = "feat_1",
  geometry = sf::st_sfc(sf::st_polygon(list(inner)), crs = crs_pl)
)
out <- intersect_mask_filter_area(x, y, x_id = "id", crs = crs_pl, repair = FALSE)
nrow(out$summary)
#> [1] 1
# }