R/intersects_mask_filter_area.R
intersect_mask_filter_area.RdTransforms 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,
dissolve = TRUE
)An sf::sf object with POLYGON or MULTIPOLYGON geometries.
An sf::sf mask layer with polygon geometries.
Name of the column in x with unique identifiers. If NULL
(default), a column .row_id is added (row order after reprojection).
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.
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).
If TRUE, apply sf::st_make_valid() to x and y after
transforming (warnings are suppressed per call).
If TRUE (default), union clipped fragments so clipped
has one row per kept x_id (see Details). FALSE returns one row per
x-y fragment, as in versions prior to 0.1.0.
A list with clipped, an sf::sf object with intersection
geometries that passed the threshold (one row per x_id when
dissolve = TRUE, one row per fragment when dissolve = FALSE), and
summary, a dplyr::tibble() with the ID column, area_full,
area_clip, area_ratio, and logical keep.
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.
y is treated as a mask: only its geometry is used in
sf::st_intersection(), so attributes of y never appear in clipped and
never collide with attributes of x.
When dissolve = TRUE (the default), clipped has exactly one row per
kept x_id: fragments produced by misaligned boundaries between x and
y (for example two administrative meshes from different vintages) are
unioned back into a single feature. Non-geometry attributes of x are
assumed constant within an x_id and reduced with the first value per
group; if an attribute actually varies within an x_id, a warning()
names the offending column(s) instead of silently picking one value.
dissolve = FALSE keeps the previous behaviour: one row per surviving
x-y fragment.
# \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
# }