Skip to contents

Applies a user-provided segmentation function to each tile described in a tiling metadata table and writes the resulting 1-layer integer segmentation to disk. Segment IDs are made globally unique across tiles by applying a running offset to positive labels.

Usage

segment_tiles(
  tile_meta,
  segment_fun,
  seg_args,
  out_dir,
  out_prefix = "seg",
  overwrite = TRUE,
  cleanup_tiles = FALSE,
  force_positive = TRUE,
  verbose = TRUE
)

Arguments

tile_meta

A `data.frame` describing input tiles on disk. Must contain a `file` column with paths to tile rasters (e.g., output of [make_tiles_disk()]).

segment_fun

A function implementing segmentation on a single tile. It must accept a `SpatRaster` as first argument and return a 1-layer `SpatRaster` of integer segment labels (or something coercible to that).

seg_args

A named list of additional arguments passed to `segment_fun` via `do.call()`.

out_dir

Character. Directory where segmented tile rasters will be written.

out_prefix

Character. Filename prefix for segmented tile outputs.

overwrite

Logical. Whether to overwrite existing output files.

cleanup_tiles

Logical. If `TRUE`, delete the input tile rasters after successful segmentation.

force_positive

Logical. If `TRUE`, assumes the segmenter may emit non-positive labels and enforces semantics where only labels `> 0` are offset to ensure global uniqueness (leaving `0` and `NA` unchanged).

verbose

Logical. If `TRUE`, display progress information.

Value

A character vector of file paths to segmented tile rasters (one per row in `tile_meta`), in processing order.

Details

This function is designed to be used as a step in the tiled segmentation engine, but can also be used standalone when users want explicit control over tiling/segmentation and subsequent merging.

**Failure handling.** If segmentation fails on a tile (error or no values), an all-`NA` segmentation tile is written to disk so downstream steps can proceed deterministically.

**Global uniqueness.** After each tile is segmented, the maximum positive ID is computed and used to update a global offset. Positive labels are shifted by the current offset to prevent ID collisions across tiles. The output is written as `INT4S` (32-bit signed integer); an error is raised if the offset would overflow this range.

See also

[make_tiles_disk()], `.tile_max_id()`, writeRaster