Skip to content

The output format: ordered, closed, nested

Most contouring libraries emit line segments: two-point pieces in whatever order the grid scan found them. Every real consumer then needs a post-process — stitch segments into curves, close loops, attach holes to rings, fix orientations. That stage is where segment-based pipelines spend their complexity (and much of their time).

The sweep emits finished topology directly:

Each curve is a polyline with vertices in traversal order. Stroke it, dash it, offset it, simplify it — no assembly required.

By default (closed=False) a contour that reaches the raster edge is an open polyline and is_closed is False; every other contour is a closed loop. That is the contour-line output GDAL, ContourPy and VTK produce.

With closed=True every contour is closed along the raster edge (the outside counts as lower than every level), so each ring bounds a region z ≥ level, the same rings d3-contour and contour-rs produce.

is_hill records which side is uphill: whether values inside the loop are above the iso-value. This is what lets you fill contour bands correctly (hypsometric tinting), place labels on the uphill side, and answer “is this a peak or a basin?” without re-sampling the field.

Rings of all levels form one containment tree, emitted with the rings:

  • parent: the innermost ring that encloses this one, of any level (-1 for the outermost rings).
  • owner: for a hole, the outer ring of its z ≥ level polygon; a hill owns itself.

The sweep reads both from its active-edge order as each ring starts, so no point-in-polygon tests are needed. result.polygons() groups holes by owner into polygons-with-holes per level (one GeoJSON MultiPolygon per threshold via result.to_geojson()). result.bands() (or isocurves.filled(), in one native call) gives filled-contour polygons for every region between consecutive levels, including below the first and above the last. Each ring is the exterior of the band just inside it, with its children as holes. Where a band reaches the raster edge, the edge stretches its exterior shares with its holes are cancelled and the boundary relinked, so every polygon is OGC-valid; a band cut in two by a contour that crosses it comes out as two polygons.

In a segment-based pipeline, topology is reconstructed after the fact — the ring-assembly pass behind gdal_contour -p (quadratic until GDAL 3.14 made it near-linear, but still a separate pass), or the several output types ContourPy maintains for different downstream consumers. The sweep discovers curves by tracing them, so ordering, closure, orientation and nesting are byproducts of the traversal, not a second pass. You get the stronger output format and the speed from the same property — measured here.