Skip to content

r.lake: Reject seed coordinates north or west of the region - #7899

Open
Ady0333 wants to merge 1 commit into
OSGeo:mainfrom
Ady0333:r.lake-seed-floor
Open

r.lake: Reject seed coordinates north or west of the region#7899
Ady0333 wants to merge 1 commit into
OSGeo:mainfrom
Ady0333:r.lake-seed-floor

Conversation

@Ady0333

@Ady0333 Ady0333 commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Follow up to #7877, which covers the southern and eastern bounds. This one covers the northern and western bounds, which have a different problem.

Problem

r.lake converts the seed coordinate to a cell index with a plain cast:

start_col = (int)Rast_easting_to_col(east, &window);
start_row = (int)Rast_northing_to_row(north, &window);

if (start_row < 0 || start_row > rows || start_col < 0 ||
    start_col > cols)
    G_fatal_error(_("Seed point outside the current region"));

The cast truncates towards zero, so every value between -1 and 0 becomes 0. A coordinate less than one cell north or west of the region therefore never reaches the check below zero. It is accepted and quietly used as row 0 or column 0, which is a cell the user did not ask for.

Going further out works, because at -1 and beyond the cast produces a negative value and the check catches it.

Reproduction

g.region rows=50 cols=50 w=0 e=50 s=0 n=50 res=1
r.mapcalc "terrain = 5"

r.lake elevation=terrain water_level=10 lake=lk coordinates=25.5,50     # on the north edge, row 0, correct
r.lake elevation=terrain water_level=10 lake=lk coordinates=25.5,50.5   # outside, accepted as row 0
r.lake elevation=terrain water_level=10 lake=lk coordinates=-0.5,25.5   # outside, accepted as column 0
r.lake elevation=terrain water_level=10 lake=lk coordinates=25.5,51.5   # outside, correctly rejected

The second and third commands should print Seed point outside the current region and instead they fill the map.

Fix

Floor the converted value so the interval between -1 and 0 stays negative and the existing check sees it. The northern and western edges themselves still map to row 0 and column 0, so seeds there keep working.

Tests

Four cases just outside the northern and western edges, which fail without the fix, and two cases on the edges themselves, which confirm valid seeds are still accepted.

Note that this touches raster/r.lake/tests/r_lake_test.py in the same place as #7877, so whichever merges second will need a small rebase.

The conversion to a cell index truncated towards zero, so a coordinate
less than one cell north or west of the region gave index 0 instead of a
negative value and passed the check below zero. Such a seed was silently
moved to the first row or column instead of being rejected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant