Skip to content

r.lake: Only set seed point when coordinates are given - #7874

Merged
echoix merged 1 commit into
OSGeo:mainfrom
Ady0333:r.lake-seed-point-fix
Sep 1, 2026
Merged

echoix merged 1 commit into
OSGeo:mainfrom
Ady0333:r.lake-seed-point-fix

Conversation

@Ady0333

@Ady0333 Ady0333 commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In raster/r.lake/main.c the statement that plants the seed point sits outside the braceless if (sdxy_opt->answer) block that is meant to guard it, so it runs on every invocation:

if (sdxy_opt->answer)
    /* Check is water level higher than seed point */
    if (in_terran[start_row][start_col] >= water_level)
        G_fatal_error(...);
out_water[start_row][start_col] = 1;

start_row and start_col are initialised to 0 and are only assigned in the coordinates= branch. The parser makes seed= and coordinates= mutually exclusive, so on every seed=<raster> run the line above sets out_water[0][0] = 1, adding a seed at the north-west corner cell of the computational region that the user never asked for.

The stray seed is not inert. If the terrain at that corner is below water_level, the flood fill spreads from it exactly as it would from a real seed, producing a second water body unconnected to the seed map. If the corner is above water_level it is cleared on the first sweep and nothing happens, which is why the defect is easy to miss.

Impact

The tool exits 0 and emits no warning, so the result looks correct. The reported Lake area and Lake volume include the spurious body, and the documented workflow of feeding a lake map from one run back in as the seed for the next carries the contamination forward.

Reproduction

A 20x20 region with a basin in the south-east at elevation 5, a separate low area in the north-west at elevation 8, a ridge at elevation 50 between them, one seed cell inside the south-east basin, and water_level=10. Only the south-east basin is reachable from the seed, so the expected result is 36 wet cells in a single water body.

before after
wet cells 72 36
distinct water bodies 2 1
north-west cell (2.5, 17.5) 2 NULL
reported lake area 72.000000 36.000000
reported lake volume 252.000000 180.000000

Fix

Add the braces so the seed point is set only when coordinates are given. With seed=, out_water is already fully populated from the seed raster, so nothing else is needed.

Tests

r.lake had no tests. This adds raster/r.lake/tests/r_lake_test.py with three cases covering the seed map path: total wet cells, the unseeded low area staying dry, and the number of distinct water bodies. All three fail on main and pass with the fix.

The coordinates= path is unchanged. Seeding inside the basin still yields 36 cells, and seeding on the dry ridge still fails with Given water level at seed point is below earth surface.

Missing braces made the seed assignment run on every invocation, adding
an unintended seed at the north-west corner of the computational region
when seed= is used.
@github-actions github-actions Bot added raster Related to raster data processing Python Related code is in Python C Related code is in C module tests Related to Test Suite labels Sep 1, 2026
@echoix

echoix commented Sep 1, 2026

Copy link
Copy Markdown
Member

I'm waiting for the CI results, the description and changes seem fine

@nilason
nilason requested a review from marisn September 1, 2026 11:57

@marisn marisn 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.

Looks good to me, but I can not judge Python code in tests, as I am not that familiar with latest trends in GRASS Python code writing.

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

Labels

C Related code is in C 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.

3 participants