vibespatial.raster.polygonize

Raster-to-vector polygonize pipeline.

Converts labeled raster regions into polygon geometries. CPU baseline uses rasterio.features.shapes; GPU path uses marching-squares NVRTC kernels.

Beads o17.8.12-8.15.

Functions

plan_polygonize_pipeline(→ vibespatial.fusion.FusionPlan)

Create a fusion plan for the polygonize pipeline.

polygonize_cpu(→ tuple[list, list])

Polygonize a raster using rasterio (CPU baseline).

polygonize_gpu(→ tuple[list, list])

Polygonize a raster using GPU marching-squares kernels.

polygonize_owned(→ tuple[list, list])

Polygonize a raster into geometries and values.

polygonize_to_gdf(raster, *[, connectivity, ...])

Polygonize a raster into a GeoDataFrame.

Module Contents

vibespatial.raster.polygonize.plan_polygonize_pipeline() vibespatial.fusion.FusionPlan

Create a fusion plan for the polygonize pipeline.

vibespatial.raster.polygonize.polygonize_cpu(raster: vibespatial.raster.buffers.OwnedRasterArray, *, spec: vibespatial.raster.buffers.PolygonizeSpec | None = None, band: int | object = _BAND_SENTINEL) tuple[list, list]

Polygonize a raster using rasterio (CPU baseline).

Parameters

rasterOwnedRasterArray

Input raster to polygonize.

specPolygonizeSpec or None

Optional polygonize configuration.

bandint

1-indexed band to polygonize. Defaults to band 1 with a warning when multiband input is received.

Returns (geometries, values) lists.

vibespatial.raster.polygonize.polygonize_gpu(raster: vibespatial.raster.buffers.OwnedRasterArray, *, spec: vibespatial.raster.buffers.PolygonizeSpec | None = None, band: int | object = _BAND_SENTINEL) tuple[list, list]

Polygonize a raster using GPU marching-squares kernels.

Parameters

rasterOwnedRasterArray

Input raster to polygonize.

specPolygonizeSpec or None

Optional polygonize configuration.

bandint

1-indexed band to polygonize. Defaults to band 1 with a warning when multiband input is received.

Pipeline (per unique raster value):
  1. Transfer raster to device (once, shared across all values)

  2. classify_cells kernel – binary-classify each 2x2 cell for target value

  3. Compact non-trivial cells via CuPy boolean indexing

  4. edge_count kernel + prefix sum – compute write offsets

  5. emit_edges kernel – emit directed edges with precomputed affine offsets

  6. Accumulate edges across all unique values

  7. Single bulk D2H transfer of concatenated edge arrays

  8. Chain edges into rings, convert to Shapely geometries (host)

Multi-value correctness: marching-squares classifies cells as in/out relative to a single target value. To produce complete closed rings for every distinct raster value, we iterate over each unique non-nodata value and run the classify/compact/count/emit pipeline once per value.

Optimizations (o18.3.22):
  • Occupancy-based launch config (no hardcoded block sizes)

  • Precomputed affine half-step offsets eliminate per-thread recomputation

  • Grid-stride loops with ILP=4 on all kernels

  • __constant__ edge lookup tables in emit_edges and edge_count kernels

  • Reused device buffers (d_cell_class, d_cell_indices) across value iters

  • No intermediate syncs between same-stream kernel launches

  • Single bulk D2H transfer of concatenated results at the end

Returns (geometries, values) lists, same format as CPU path.

vibespatial.raster.polygonize.polygonize_owned(raster: vibespatial.raster.buffers.OwnedRasterArray, *, connectivity: int = 4, simplify_tolerance: float | None = None, max_polygons: int | None = 1000000, value_field: str = 'value', use_gpu: bool | None = None, band: int | object = _BAND_SENTINEL) tuple[list, list]

Polygonize a raster into geometries and values.

Parameters

rasterOwnedRasterArray

Input raster to polygonize.

connectivityint

4 or 8 neighbor connectivity.

simplify_tolerancefloat or None

Optional Douglas-Peucker simplification tolerance.

max_polygonsint or None

Maximum polygon count (explosion guardrail).

value_fieldstr

Name for the value attribute.

use_gpubool or None

Force GPU (True), force CPU (False), or auto-dispatch (None).

bandint

1-indexed band to polygonize (rasterio convention). Defaults to band 1 with a warning when multiband input is received.

Returns

(geometries, values)

Lists of Shapely geometries and their associated raster values.

vibespatial.raster.polygonize.polygonize_to_gdf(raster: vibespatial.raster.buffers.OwnedRasterArray, *, connectivity: int = 4, simplify_tolerance: float | None = None, max_polygons: int | None = 1000000, value_field: str = 'value', band: int | object = _BAND_SENTINEL)

Polygonize a raster into a GeoDataFrame.

Parameters

rasterOwnedRasterArray

Input raster.

connectivity, simplify_tolerance, max_polygons, value_field

See polygonize_owned.

bandint

1-indexed band to polygonize (rasterio convention). Defaults to band 1 with a warning when multiband input is received.

Returns

GeoDataFrame

With geometry column and a value column named value_field.