Skip to content

open_geotiff overview_level accepts bool and non-int types, returning wrong resolution silently #2074

@brendancol

Description

@brendancol

Description

open_geotiff(..., overview_level=...) does not validate the type of overview_level. In xrspatial/geotiff/_header.py:794-808, the selector assigns level = overview_level and compares it numerically. Because bool is a subclass of int in Python, overview_level=True is coerced to 1 and the function returns overview level 1 with no error. Non-int types (str, float) leak raw TypeError messages from the internal comparison or list indexing.

Downstream code then gets a wrong-resolution array with no signal that anything was off.

Reproducer

import numpy as np
import tifffile
from xrspatial.geotiff import open_geotiff

arr = np.random.randint(0, 256, size=(64, 64), dtype=np.uint8)
half = arr[::2, ::2]
with tifffile.TiffWriter("cog.tif") as tw:
    tw.write(arr, tile=(32, 32), photometric="minisblack")
    tw.write(half, tile=(32, 32), photometric="minisblack", subfiletype=1)

# Bug 1: bool is silently coerced to an int level
open_geotiff("cog.tif", overview_level=True).shape   # (32, 32)
open_geotiff("cog.tif", overview_level=False).shape  # (64, 64)

# Bug 2: raw TypeErrors from internal comparison/indexing
open_geotiff("cog.tif", overview_level="0")
# TypeError: '<' not supported between instances of 'str' and 'int'

open_geotiff("cog.tif", overview_level=1.0)
# TypeError: list indices must be integers or slices, not float

Expected behavior

overview_level should be an int (>= 0) or None. Any other type should raise a clear TypeError that names the rejected type and value.

Source location

xrspatial/geotiff/_header.py:794-808 in select_overview_ifd. The public entrypoint is open_geotiff in xrspatial/geotiff/__init__.py:243.

Environment

xarray-spatial-contrib main, Python 3.12.

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiAPI design and consistencybugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions