contours: Extract contour iso-lines from a streamed raster

View source: R/spatial_more.R

contoursR Documentation

Extract contour iso-lines from a streamed raster

Description

Traces contour lines at one or more levels from a .vec raster with marching squares, reading the raster one tile-row strip at a time (each strip expanded by one row so a cell straddling the strip boundary is traced once). Each strip contributes line segments, which are accumulated into a lazy vectra_node carrying a level column and hex-WKB geometry. With merge = TRUE the segments of each level are joined into continuous lines.

Usage

contours(x, levels, band = 1L, merge = TRUE, crs = NA, flush_rows = NULL)

Arguments

x

A vectra_raster (from vec_open_raster()) or a path to a .vec raster.

levels

Numeric vector of contour levels to trace.

band

Band to contour (1-based). Default 1.

merge

If TRUE (default) join each level's segments into continuous lines with sf::st_line_merge(); if FALSE return the raw per-cell segments.

crs

Coordinate reference system recorded on the node. Defaults to the raster's EPSG, else unknown.

flush_rows

Rows buffered before a spill flush. NULL (the default) instead flushes once a spill buffer's size crosses the streaming memory budget (a fraction of vectra_mem(), set with options(vectra.memory = )); an explicit value caps each buffer at that many rows.

Details

Extraction is the sort / partition tier of the spatial toolbox: bounded to one haloed strip at a time. The optional final merge collects the segment set, which is small relative to the raster, and joins it per level; this is the small all-to-all step on the output, not on the grid. Geometry assembly and the merge are delegated to sf (an optional dependency).

Value

A vectra_node with a level column and a hex-WKB geometry column, materialise it with collect_sf().

See Also

polygonize() for area features, terrain() for the DEM derivatives contours often accompany, collect_sf() to materialise as sf.

Examples


z <- outer(1:20, 1:20, function(r, c) r + c)
f <- tempfile(fileext = ".vec")
vec_write_raster(z, f, dtype = "f64", extent = c(0, 0, 20, 20))

iso <- contours(f, levels = c(15, 25, 35))
collect_sf(iso)
unlink(f)


vectra documentation built on July 10, 2026, 5:08 p.m.