Skip to content

geotiff: to_geotiff trusts coord regularity, silently writes wrong transform for non-uniform coords #1720

@brendancol

Description

@brendancol

Describe the bug

_coords_to_transform (xrspatial/geotiff/init.py:296-303) reads x[1] - x[0] and y[1] - y[0] as the pixel sizes for the affine transform. There is no check that the rest of the spacing matches.

GeoTIFF (in this writer) only supports an affine transform, so non-uniform coords cannot be expressed faithfully. Today the writer silently picks the spacing between the first two samples and lets the resulting file claim that spacing for every pixel, which produces a wrong georeference downstream.

Expected behavior

_coords_to_transform validates that np.diff(x) and np.diff(y) are constant within a small relative tolerance (e.g. 1e-6 of the median step). If not, raise a ValueError with a clear message rather than emitting a silently-wrong transform.

Proposed fix

Add the regularity check to _coords_to_transform. Failing the check raises ValueError. Add a test that covers a DataArray with non-uniform x and y coords and asserts the writer raises.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinginput-validationInput validation and error messages

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions