Skip to content

r.reclass.area: Implement -i flag to invert area size filter - #8098

Merged
petrasovaa merged 3 commits into
OSGeo:mainfrom
bhuvan-somisetty:fix-8097-reclass-area-invert
Oct 27, 2026
Merged

petrasovaa merged 3 commits into
OSGeo:mainfrom
bhuvan-somisetty:fix-8097-reclass-area-invert

Conversation

@bhuvan-somisetty

Copy link
Copy Markdown
Contributor

Description

This PR implements the -i (invert) flag functionality in r.reclass.area:

  • In reclass(): passed invert boolean flag and updated the inclusion logic:
    in_range = lower <= hectares <= upper
    if not in_range if invert else in_range:
        rules += "{} = {} {}\n".format(*[f[i] for i in [0, 2, -2]])
    Clumps with area between lower and upper are filtered out when -i is set, preserving clumps smaller than lower or greater than upper.
  • Added complete translatable informational and fatal messages for each threshold permutation with invert=True.
  • Handled mutual exclusions: added % excludes: -m, -i in parser rules (since -m prunes small clumps during clumping prior to inversion) and raised gs.fatal(_("Flag -i is not supported with method='rmarea'")) when combining -i with vector-based method='rmarea'.
  • Documented -i behavior in r.reclass.area.md and r.reclass.area.html.
  • Added unit tests for inverted filtering with lower, lower + upper, and validation for unsupported method='rmarea'.

Motivation and context

Fixes #8097

The -i flag was defined in the interface definition of r.reclass.area.py (% flag: i, % description: Invert selection (remove areas within size range)), but was never passed to reclass() or evaluated, leaving it completely inoperative.

How has this been tested?

  • Ran ruff check scripts/r.reclass.area/ and ruff format --check scripts/r.reclass.area/ (all passed cleanly).
  • Added unit tests in scripts/r.reclass.area/testsuite/test_r_reclass_area.py:
    • test_reclass_lower_invert: checks that clumps below the lower threshold are retained.
    • test_reclass_lower_upper_invert: checks that clumps outside the [lower, upper] range are retained.
    • test_rmarea_invert_unsupported: verifies proper module failure when -i is passed with method=rmarea.

Screenshots (if appropriate)

N/A

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as before)

Checklist

  • PR title provides summary of the changes and starts with one of the pre-defined prefixes
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.

Signed-off-by: bhuvan-somisetty somisettybhuvan5@gmail.com

In `scripts/r.reclass.area/r.reclass.area.py`:
- Wire up the `-i` flag in `main()` by passing `invert=flags["i"]` to `reclass()`.
- In `reclass()`, invert the area threshold check when `invert=True` so clumps between `lower` and `upper` are removed instead of kept.
- Add complete translatable messages for inverted filtering states.
- Disallow `-i` with `method='rmarea'` and add `excludes: -m, -i` to parser rules.
- Update documentation in `r.reclass.area.md` and `r.reclass.area.html`.
- Add test cases in `test_r_reclass_area.py`.

Fixes OSGeo#8097.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@github-actions github-actions Bot added raster Related to raster data processing Python Related code is in Python HTML Related code is in HTML module docs markdown Related to markdown, markdown files tests Related to Test Suite labels Oct 21, 2026
With -i, lower=20 and upper=4000, clumps larger than 4000 hectares are
kept as well, so the minimum category is below 900. Use the same bounds
as the non-inverted lower/upper test.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>

@petrasovaa petrasovaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! There are couple small improvements still needed.

Comment thread scripts/r.reclass.area/testsuite/test_r_reclass_area.py
Comment thread scripts/r.reclass.area/r.reclass.area.py Outdated
Comment thread scripts/r.reclass.area/r.reclass.area.py Outdated
- Improve condition readability in `reclass()` with `keep` variable.
- Document that `-i` is only supported with `method=reclass` and not with `method=rmarea`.
- Update test suite: add `test_reclass_upper_invert` with distinct min/max range and assert that normal and inverted outputs in `test_reclass_lower_upper_invert` have zero overlapping non-null cells.

Signed-off-by: bhuvan-somisetty <somisettybhuvan5@gmail.com>
@bhuvan-somisetty

Copy link
Copy Markdown
Contributor Author

Thanks for the review @petrasovaa! I've updated everything based on your feedback:

  • Simplified the condition readability using keep.
  • Documented that -i is only supported with the reclass method (and not rmarea).
  • Updated the tests with distinct ranges (test_reclass_upper_invert) and verified that normal and inverted outputs have zero overlapping cells (n=0).

@petrasovaa petrasovaa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@petrasovaa
petrasovaa merged commit 05e7f7f into OSGeo:main Oct 27, 2026
26 checks passed
@github-actions github-actions Bot added this to the 8.6.0 milestone Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs HTML Related code is in HTML markdown Related to markdown, markdown files module Python Related code is in Python raster Related to raster data processing tests Related to Test Suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] r.reclass.area: -i flag is ignored and has no effect on filtering

2 participants