Skip to content

Add contour line extraction via marching squares (#964)#974

Merged
brendancol merged 7 commits intomasterfrom
issue-964
Mar 5, 2026
Merged

Add contour line extraction via marching squares (#964)#974
brendancol merged 7 commits intomasterfrom
issue-964

Conversation

@brendancol
Copy link
Contributor

Summary

  • Adds contours() function for extracting isolines from 2D rasters using marching squares
  • Supports all four backends: NumPy (@ngjit kernel), Dask+NumPy (chunked via da.overlap), CuPy and Dask+CuPy (CPU fallback, same approach as polygonize)
  • Stitches segments into polylines with closed-ring detection; optional GeoDataFrame output
  • Second raster-to-vector tool alongside polygonize

Changes

  • xrspatial/contour.py -- marching squares kernel, four backend functions, segment stitcher, public contours() API
  • xrspatial/tests/test_contour.py -- 22 tests: correctness, NaN handling, edge cases, backend equivalence, GeoDataFrame output, accessor integration
  • examples/user_guide/22_Contour_Lines.ipynb -- user guide notebook with terrain generation, contour extraction, hillshade overlay
  • __init__.py, accessor.py -- exports and .xrs.contours() accessor
  • docs/source/reference/utilities.rst -- API docs entry
  • README.md -- feature matrix row

Test plan

  • pytest xrspatial/tests/test_contour.py -- 21 passed, 1 skipped (skimage reference test, skimage not installed)
  • GPU tests pass in CI (CuPy backend)
  • Notebook renders correctly

Closes #964

@github-actions github-actions bot added the performance PR touches performance-sensitive code label Mar 5, 2026
New xrspatial.contour module with contours() function that extracts
isolines from 2D rasters. Supports numpy, cupy, dask+numpy, and
dask+cupy backends. Includes segment stitching into polylines and
optional GeoDataFrame output.
Corrected edge assignments in the marching squares lookup table.
Previous table had wrong edges for most cases (e.g. case 1 used
top edge instead of bottom edge). Added comprehensive test suite
covering correctness, NaN handling, edge cases, backend equivalence,
GeoDataFrame output, accessor integration, and closed-ring detection.
Matplotlib-based interactive app for exploring contour lines on
procedural terrain. Supports click-to-add levels, scroll density
control, filled contours, hillshade toggle, contour labels, index
contours, and colour scheme cycling.
@brendancol brendancol merged commit a752c52 into master Mar 5, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance PR touches performance-sensitive code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add contour line extraction from raster DEMs

1 participant