Skip to content

contour: test gaps for all-equal auto-levels and empty explicit levels #3352

@Melissari1997

Description

@Melissari1997

Reason or Problem

Two edge cases in contours() are not tested:

  1. All-equal raster with auto-levels. test_no_contours_flat passes explicit levels=[], so it never exercises the auto-level branch (levels=None) on a flat raster where vmin == vmax. That branch calls np.linspace(vmin, vmax, n_levels + 2)[1:-1] which produces identical levels on equal min/max — the function returns an empty result, but no test asserts this.

  2. Empty levels=[]. Passing an empty list as levels returns [] silently. No test documents that this is the intended behavior or that it does not raise.

Proposal

Design: Add two test functions to xrspatial/tests/test_contour.py:

def test_contours_flat_auto_levels():
    raster = xr.DataArray(
        np.ones((10, 10), dtype=np.float64), dims=["y", "x"]
    )
    result = contours(raster, levels=None, n_levels=10)
    assert result == []

def test_contours_empty_explicit_levels():
    raster = xr.DataArray(
        np.random.rand(10, 10), dims=["y", "x"]
    )
    result = contours(raster, levels=[], return_type="numpy")
    assert result == []

    result_gdf = contours(raster, levels=[], return_type="geopandas")
    assert len(result_gdf) == 0

Value: Covers two branches with zero test coverage, preventing regressions on the auto-level range computation and the empty-levels path that the geopandas empty-GeoDataFrame CRS fix (#2700) depends on.

Stakeholders and Impacts

Test-only change. No source modification needed.

Drawbacks

Negligible — two small deterministic tests.

Alternatives

  • Rely on the accuracy and performance sweeps catching regressions here.
  • File separate issues per gap (combined here for minimal overhead).

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTest coverage and parity

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions