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

Logic errors in GeoTIFF validation in rio_cogeo/cogeo.py #182

Closed
mplough-kobold opened this issue Mar 23, 2021 · 4 comments · Fixed by #183
Closed

Logic errors in GeoTIFF validation in rio_cogeo/cogeo.py #182

mplough-kobold opened this issue Mar 23, 2021 · 4 comments · Fixed by #183
Assignees
Labels
bug Something isn't working

Comments

@mplough-kobold
Copy link
Contributor

Lines 456-461 of rio_cogeo/cogeo.py read:

            block_offset = int(src.get_tag_item("BLOCK_OFFSET_0_0", "TIFF", bidx=1))
            if not block_offset:
                errors.append("Missing BLOCK_OFFSET_0_0")

            data_offset = int(block_offset) if block_offset else None
            data_offsets = [data_offset]

If BLOCK_OFFSET_0_0 is missing / None, the cast to int will raise an exception and the error string will not be appended.

Later, on line 472:

            if data_offsets[-1] < ifd_offsets[-1]:

The value of data_offsets[-1] may be None. If it is in fact None, a TypeError will be raised.

I don't currently understand the TIFF file format well enough to understand how to handle the missing offset correctly so I don't want to open a PR with a potentially wrong fix, but I'd be glad to get a discussion started on what to do here.

Thanks!

@vincentsarago
Copy link
Member

@mplough-kobold would you be able to share a tiff where BLOCK_OFFSET_0_0 is missing ?

FYI looking at https://github.com/OSGeo/gdal/blob/master/gdal/swig/python/samples/validate_cloud_optimized_geotiff.py#L72-L73 it seems that you are indeed right 😉

@vincentsarago vincentsarago self-assigned this Mar 23, 2021
@vincentsarago vincentsarago added the bug Something isn't working label Mar 23, 2021
@mplough-kobold
Copy link
Contributor Author

mplough-kobold commented Mar 23, 2021

Certainly!

The code at https://github.com/OSGeo/gdal/blob/master/autotest/gcore/cog.py#L761 will generate a TIFF file with a missing block offset. (it's currently HEAD; for those looking at this in the future, see the specific commit -- https://github.com/OSGeo/gdal/blob/a9e3b302963f1b560502fea8760807b5506fdfb4/autotest/gcore/cog.py#L761)

I sliced and diced it a bit and ran this snippet in the osgeo/gdal Docker image; the resulting TIFF file is attached here -
cog.tif.zip.

from osgeo import gdal

filename = 'cog.tif'
src_ds = gdal.GetDriverByName('MEM').Create('', 512, 512)
src_ds.GetRasterBand(1).Fill(255)
src_ds.WriteRaster(0, 0, 256, 256, '\x00' * 256 * 256)
src_ds.WriteRaster(256, 256, 128, 128, '\x00' * 128 * 128)
src_ds.BuildOverviews('NEAREST', [2])
gdal.GetDriverByName('COG').CreateCopy(filename, src_ds, options=['BLOCKSIZE=128', 'SPARSE_OK=YES', 'COMPRESS=LZW'])

@mplough-kobold
Copy link
Contributor Author

🎉 🎉 Thanks @vincentsarago for taking care of this!

@vincentsarago
Copy link
Member

@mplough-kobold thanks for reporting it :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants