Skip to contents

Returns A5 cells at resolution whose centres fall inside the polygon. Multi-feature inputs (a MULTIPOLYGON, an sfc of multiple polygons, or a POLYGON with holes) are handled natively: per polygon part, the outer-ring cells are computed and any hole-ring cells are subtracted, then the results are unioned across parts. The final cell set is compacted; use a5_uncompact() to expand to a uniform-resolution grid.

Usage

a5_polygon_to_cells(x, resolution)

Arguments

x

A polygon-like geometry. One of:

  • Any geometry handleable by wk::wk_handle() (e.g. wk::wkt(), wk::wkb(), wk::rct(), sf, sfc) containing one or more POLYGON / MULTIPOLYGON features.

  • A SpatVector of polygons (requires the terra package).

  • A two-column numeric matrix (cbind(lon, lat)) of vertices, interpreted as a single outer ring.

  • A data.frame with columns lon and lat, interpreted as a single outer ring.

resolution

Integer scalar target resolution (0-30).

Value

An a5_cell vector at or coarser than resolution.

Details

Membership is determined by centre-point containment: a cell is included iff its centroid lies inside the polygon, with hole rings properly subtracted. This is distinct from a5_grid()'s boundary-intersection semantics; for the same polygon the two functions can return slightly different cell sets near the boundary.

Coordinates must be WGS 84 longitude/latitude in degrees. Rings are closed automatically; a trailing duplicate vertex is dropped if present.

Where no A5 cell centroids at the specified resolution fall within the geometry, an empty a5_cell vector is returned.

Matrix and data.frame inputs are treated as a single ring; for multi-feature data or polygons with holes, pass an sf, sfc, wk, or SpatVector geometry instead.

Examples

poly <- wk::wkt(
  "POLYGON ((-3.3 55.9, -3.1 55.9, -3.1 56, -3.3 56, -3.3 55.9))"
)
cells <- a5_polygon_to_cells(poly, resolution = 8)
length(cells)
#> [1] 0