Skip to content

Fix NaN pixels producing spurious polygons in numpy/dask backends#1194

Merged
brendancol merged 1 commit into
masterfrom
fix/polygonize-nan-masking
Apr 14, 2026
Merged

Fix NaN pixels producing spurious polygons in numpy/dask backends#1194
brendancol merged 1 commit into
masterfrom
fix/polygonize-nan-masking

Conversation

@brendancol
Copy link
Copy Markdown
Contributor

Closes #1190

Summary

  • The numpy backend for polygonize() didn't mask NaN pixels in float rasters. Since NaN != NaN, each NaN pixel became its own isolated region and emitted a single-pixel polygon with a NaN value.
  • The CuPy backend already handled this correctly via ~cp.isnan(data). This applies the same masking in _polygonize_numpy, which the dask backend inherits.

Test plan

  • test_polygonize_nan_pixels_excluded -- 2x2 float raster with NaN, both 4- and 8-connectivity
  • test_polygonize_nan_pixels_excluded_dask -- 3x3 float raster with NaN, dask chunked
  • Full polygonize suite: 110 passed, 13 skipped (GPU not available)

)

The numpy backend for polygonize() passed float arrays straight to
_calculate_regions without masking NaN pixels. Since NaN != NaN, each
NaN pixel became its own isolated region and emitted a spurious
single-pixel polygon with a NaN value.

The CuPy backend already handled this correctly by building a valid-pixel
mask via ~cp.isnan(data). Apply the same approach in _polygonize_numpy:
detect float dtype and fold ~np.isnan(values) into the mask before
region calculation. The dask backend inherits this fix since it calls
_polygonize_numpy per chunk.
@github-actions github-actions Bot added the performance PR touches performance-sensitive code label Apr 13, 2026
@brendancol brendancol merged commit 686bd6f into master Apr 14, 2026
11 checks passed
@brendancol brendancol deleted the fix/polygonize-nan-masking branch May 5, 2026 03:44
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.

polygonize numpy backend emits spurious polygons for NaN pixels

1 participant