Skip to contents

Returns cell_id + pct_covered (0-100) for the cells a polygon overlaps. pct_covered is the fraction of each cell inside the polygon, and it is not cosmetic — scores_for_cells() and species_for_cells() weight by it, so partially covered edge cells count proportionally.

Usage

cells_in_polygon(poly, src, res = 0.05)

Arguments

poly

an sf polygon (assumed or transformable to EPSG:4326)

src

a DBI connection (preferred), or a single-layer terra::SpatRaster of integer cell ids

res

ignored, kept for back-compatibility; the resolution now comes from the grid registry, which is the only place it was ever right

Value

a tibble with columns cell_id (integer) and pct_covered (0-100)

Details

Pass a DB connection. Then the grid is read from the database being queried (grid_for_con()) and cannot disagree with it. Both generations take the SAME path: cells_in_polygon_grid() computes the cells from the grid definition alone, and the result is restricted to the cell_ids the release actually holds. No raster is opened on any version — the v7 branch used to read the 0-360 cell-id raster and returned terra's coverage fractions, which differed from the v8 path by a mean 0.66 pp on edge cells for no reason anyone could act on.

Longitudes are unwrapped on the way in with unwrap_polygon(), because an sf polygon is the one place a wrapped antimeridian ring can still arrive. The grid function it delegates to reads coordinates literally and guesses nothing.

Passing a terra::SpatRaster directly still works and still uses terra::extract(): a cell-id COG is a lookup IMAGE whose pixel values are ids from a frame that may not be the grid's, so arithmetic cannot be applied to it. Nothing can verify such a raster matches the database — see cell_id_raster() for how that failed silently on v8.