Skip to content

Fix to prevent negative size values in gdal2tiles.py#14101

Closed
antonmyagotin wants to merge 1 commit intoOSGeo:masterfrom
antonmyagotin:patch-1
Closed

Fix to prevent negative size values in gdal2tiles.py#14101
antonmyagotin wants to merge 1 commit intoOSGeo:masterfrom
antonmyagotin:patch-1

Conversation

@antonmyagotin
Copy link
Copy Markdown

Problem:
Gdal2tiles crashes with the message "Illegal values for buffer size". The problem was reported in multiple source and exists already for years. Examples:
https://gis.stackexchange.com/questions/249809/gdal2tiles-error-5-illegal-values-for-buffer-size-error-for-a-single-zoom-lev

Solution:
We just skip the tile with negative extent values.

for big geotiff files the tiling craches due to negative values in rxsize, rysize, wxsize, wysize. We just skip the inopropriate tile sizes.
@rouault
Copy link
Copy Markdown
Member

rouault commented Mar 10, 2026

@antonmyagotin
Copy link
Copy Markdown
Author

yes, for several big geotiffs I can reproduce the crash

@rouault
Copy link
Copy Markdown
Member

rouault commented Mar 10, 2026

yes, for several big geotiffs I can reproduce the crash

Please use "gdal_create" with options "-co TILED=YES -co SPARSE_OK=YES" to create an empty file with the same characteristics, and attach it there (zipped), and also the exact gdal2tiles command line you use

@antonmyagotin
Copy link
Copy Markdown
Author

Sorry for the delay, it took some time to generate smallest reproducable example

small_sample.zip
I use following sequence of command

create original vrt file

gdalbuildvrt -input_file_list filelist.txt original.vrt

create scaled vrt

gdal_translate -of VRT -ot Byte -a_nodata 0 -scale -15 15 1 255 original.vrt scaled.vrt

and finally generate tiles

gdal2tiles --processes 8 --tilesize=256 --tiledrive=PNG  scaled.vrt

it gives an error:
The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/usr/bin/gdal2tiles", line 33, in
sys.exit(load_entry_point('GDAL==3.9.3', 'console_scripts', 'gdal2tiles')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/osgeo_utils/gdal2tiles.py", line 4693, in main
return submain(argv, called_from_main=called_from_main)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/osgeo_utils/auxiliary/util.py", line 62, in enable_exceptions_wrapper
return fun(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/osgeo_utils/gdal2tiles.py", line 4723, in submain
multi_threaded_tiling(input_file, output_folder, options, pool)
File "/usr/lib/python3/dist-packages/osgeo_utils/auxiliary/util.py", line 62, in enable_exceptions_wrapper
return fun(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/osgeo_utils/gdal2tiles.py", line 4617, in multi_threaded_tiling
for _ in pool.imap_unordered(
File "/usr/lib/python3.12/multiprocessing/pool.py", line 451, in
return (item for chunk in result for item in chunk)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/multiprocessing/pool.py", line 873, in next
raise value
RuntimeError: Illegal values for buffer size

Checking out actual values for rxsize, rysize, wxsize, wysize I see that they are negative.

@jratike80
Copy link
Copy Markdown
Collaborator

It seems to work well with the new alternative https://gdal.org/en/stable/programs/gdal_raster_tile.html
Perhaps there is no need to touch the old python utility any more.

gdal raster tile scaled.vrt output
0...10...20...30...40...50...60...70...80...90...100 - done.

rouault added a commit to rouault/gdal that referenced this pull request Mar 12, 2026
…uare pixels

that can cause exceptions due to empty tiles being attempted to be
generated, or tiles that should be generated not being generated

Fixes OSGeo#14101
@rouault
Copy link
Copy Markdown
Member

rouault commented Mar 12, 2026

closing this PR as it only fixes the symptom and not the root cause. Proper fix in #14115

@rouault rouault closed this Mar 12, 2026
rouault added a commit to rouault/gdal that referenced this pull request Mar 12, 2026
…uare pixels

that can cause exceptions due to empty tiles being attempted to be
generated, or tiles that should be generated not being generated

Fixes OSGeo#14101
rouault added a commit that referenced this pull request Mar 15, 2026
…uare pixels

that can cause exceptions due to empty tiles being attempted to be
generated, or tiles that should be generated not being generated

Fixes #14101
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.

3 participants