| spatial_clip | R Documentation |
Streams a large layer x through the engine and cuts each batch's geometry
against a small resident mask (a study boundary, a buffer, a set of
patches). By default this clips – the intersection with the mask, the GIS
"Clip" tool – keeping only the parts of x that fall inside mask. With
erase = TRUE it instead erases – the difference, the "Erase"/"Difference"
tool – keeping the parts of x outside mask. The mask is dissolved to a
single geometry once and held resident while the billion-row left stream
flows past one batch at a time.
spatial_clip(
x,
mask,
erase = FALSE,
geom = "geometry",
coords = NULL,
crs = NA,
out_geom = NULL,
flush_rows = NULL
)
x |
A |
mask |
An |
erase |
If |
geom |
Name of the input geometry column holding hex-WKB or WKT strings.
Default |
coords |
Optional length-2 character vector naming the x and y
coordinate columns to assemble point geometry from (e.g. |
crs |
Coordinate reference system of the input geometry, in any form
|
out_geom |
Name of the output geometry column. Defaults to |
flush_rows |
Transformed rows buffered before a spill flush. Larger
values mean fewer, bigger temporary files. |
Geometry travels through the engine as hex-encoded WKB in a string column and
the CRS is carried on the returned node; use collect_sf() to materialize.
On projected or unprojected planar data the cut runs natively on the GEOS C
API straight off the hex-WKB column (the mask parsed once); geographic
coordinates with spherical geometry on (sf::sf_use_s2()) and
coordinate-assembled (coords) input cut through sf instead. When
mask carries no CRS it inherits the stream's.
A vectra_node of the cut geometry with x's attributes, backed by
temporary .vtr spills and carrying the input CRS.
spatial_filter() to keep whole features by location without
cutting them, spatial_map() for per-feature transforms, collect_sf().
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
mask <- sf::st_union(nc[nc$NAME %in% c("Ashe", "Alleghany"), ])
f <- tempfile(fileext = ".vtr")
write_vtr(data.frame(
NAME = nc$NAME,
geometry = sf::st_as_binary(sf::st_geometry(nc), hex = TRUE)
), f)
# Clip every county polygon to the two-county mask, streaming.
clipped <- tbl(f) |> spatial_clip(mask, crs = sf::st_crs(nc))
collect_sf(clipped)
unlink(f)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.