Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gdalwarp overview selection issue due to numerical instability #10873

Closed
calogeromauceri opened this issue Sep 25, 2024 · 2 comments · Fixed by #10879
Closed

gdalwarp overview selection issue due to numerical instability #10873

calogeromauceri opened this issue Sep 25, 2024 · 2 comments · Fixed by #10879
Assignees

Comments

@calogeromauceri
Copy link

calogeromauceri commented Sep 25, 2024

What is the bug?

Summary
When reprojecting a file using gdalwarp, the overview selected can unexpectedly change by slightly shifting the output window. This results in visible differences in the output image, as lower resolution overviews might be chosen.

Example
Consider the following example, where the same input file produces different output with a small shift in the target extent

gdalwarp -of PNG -ts 256 256 -t_srs "+proj=eqc +a=2439700 +b=2439700 +lon_0=0.000000 +lat_0=0.000000 +no_defs +x_0=0 +y_0=0" -te -3481372.584110 1173100.739750 -2713372.607150 1941100.716710 -overwrite test.tif out1.png

This selects the second overview.
out1

shifting a little bit the extent
gdalwarp -of PNG -ts 256 256 -t_srs "+proj=eqc +a=2439700 +b=2439700 +lon_0=0.000000 +lat_0=0.000000 +no_defs +x_0=0 +y_0=0" -te -3484372.584010 1173100.739750 -2716372.607050 1941100.716710 -overwrite test.tif out2.png

This selects the first, higher resolution overview
out2

Source problem and suggested fix
Upon debugging, I found the problem is due to numeric instability when checking ratio between the desired resolution and the available overviews. Specifically in the GDALWarpDirect there is a comparison for selecting the right overview.

Relevant lines of code: GDALWarpDirect

if (dfOvrRatio < dfTargetRatio &&
    dfNextOvrRatio > dfTargetRatio)
    break;

Using an epsilon to stabilize the ratio checks seems to fix the issue. For example

constexpr double EPS = 1e-1;
if (dfOvrRatio - dfTargetRatio < EPS &&
   dfNextOvrRatio - dfTargetRatio > EPS )
   break;

You can reproduce the issue using the following test file: https://files.actgate.com/temp/test.tif

Steps to reproduce the issue

gdalwarp -of PNG -ts 256 256 -t_srs "+proj=eqc +a=2439700 +b=2439700 +lon_0=0.000000 +lat_0=0.000000 +no_defs +x_0=0 +y_0=0" -te -3481372.584110 1173100.739750 -2713372.607150 1941100.716710 -overwrite test.tif out1.png

gdalwarp -of PNG -ts 256 256 -t_srs "+proj=eqc +a=2439700 +b=2439700 +lon_0=0.000000 +lat_0=0.000000 +no_defs +x_0=0 +y_0=0" -te -3484372.584010 1173100.739750 -2716372.607050 1941100.716710 -overwrite test.tif out2.png

Versions and provenance

3.9.2

Additional context

No response

@jratike80
Copy link
Collaborator

I cannot judge if your suggestion is good, but if you want to be sure about what overview level gdalwarp takes it is best to define it with -ovrhttps://gdal.org/en/latest/programs/gdalwarp.html#cmdoption-gdalwarp-ovr.

@calogeromauceri
Copy link
Author

calogeromauceri commented Sep 26, 2024

I confirm I get a consistent result if I pass the overview to use with -ovr, but I would like to use the -ovr AUTO option and let gdalwarp select the right overview to use, otherwise I would need to implement/duplicate what is already done by gdalwarp for the automatic overview selection

@rouault rouault self-assigned this Sep 26, 2024
rouault added a commit to rouault/gdal that referenced this issue Sep 26, 2024
…views

Fixes OSGeo#10873

Uniformize logic with the one of GDALBandGetBestOverviewLevel2()
@rouault rouault closed this as completed in 160b1dc Oct 4, 2024
rouault added a commit that referenced this issue Oct 4, 2024
…views

Fixes #10873

Uniformize logic with the one of GDALBandGetBestOverviewLevel2()
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 a pull request may close this issue.

3 participants