Skip to content

Fix geotiff export geotransform values for numpy >= 2.4#7140

Open
PraneelBhatia wants to merge 2 commits into
SciTools:mainfrom
PraneelBhatia:fix-7133-geotiff-numpy24
Open

Fix geotiff export geotransform values for numpy >= 2.4#7140
PraneelBhatia wants to merge 2 commits into
SciTools:mainfrom
PraneelBhatia:fix-7133-geotiff-numpy24

Conversation

@PraneelBhatia

Copy link
Copy Markdown

🚀 Pull Request

Description

Closes #7133

iris.experimental.raster.export_geotiff fails with TypeError: not a number from GDAL's SetGeoTransform under numpy >= 2.4: the x/y step values were the 1-element arrays produced by np.diff(coord.bounds[0]), and numpy >= 2.4 removed the implicit 1-element-array-to-scalar conversion which previously made this work.

This PR coerces all four geotransform inputs (x_min, x_step, y_max, y_step) to plain Python floats via .item(), as suggested in the issue — .item() is safe on scalars and size-1 arrays, and would raise on anything longer.

Also:

  • Added a regression test (test_scalar_transform_values) which asserts the values passed to _gdal_write_array are Python floats, so the regression is guarded even on numpy < 2.4 installs (the geotiff tests need GDAL, which is not in the CI environment).
  • Added a whatsnew entry.

Tested locally with python 3.13 / numpy 2.4.6 / GDAL 3.13.0: the issue's reproduction snippet now succeeds (GetGeoTransform() == (-0.5, 1.0, 0.0, 4.5, 0.0, -1.0)), and pytest lib/iris/tests/unit/experimental/raster/test_export_geotiff.py goes from 17 failed / 1 passed on main to 19 passed. The new test fails without the fix.


Consult Iris pull request check list


Add any of the below labels to trigger actions on this PR:

  • benchmark_this Request that this pull request be benchmarked to check if it introduces performance shifts

Copilot AI review requested due to automatic review settings June 5, 2026 18:40
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

GDAL's SetGeoTransform requires plain numbers, and numpy >= 2.4 no
longer implicitly converts the 1-element arrays which np.diff produces
for the x/y step values, causing export_geotiff to fail with
"TypeError: not a number". Take scalar ".item()" values for all four
geotransform inputs instead.

Closes SciTools#7133
@PraneelBhatia PraneelBhatia force-pushed the fix-7133-geotiff-numpy24 branch from 8a9e3ab to 0bb3f5d Compare June 5, 2026 18:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Fixes iris.experimental.raster.export_geotiff failing under newer NumPy by ensuring GDAL geotransform parameters are passed as Python scalars rather than NumPy 1-element arrays.

Changes:

  • Convert np.diff(...), np.min(...), and np.max(...) results to Python scalars via .item() before calling GDAL.
  • Add a unit test asserting _gdal_write_array receives Python scalar geotransform values.
  • Document the bugfix in the latest “What’s New”.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
lib/iris/experimental/raster.py Casts geotransform components to Python scalars to avoid TypeError with newer NumPy.
lib/iris/tests/unit/experimental/raster/test_export_geotiff.py Adds regression test validating scalar arguments passed into GDAL writer.
docs/src/whatsnew/latest.rst Notes the fix and references the related issue/user link.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

cube.add_dim_coord(coord, 1)
return cube

def test_(self, tmp_path):

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to test_geotransform in b88badd.

Comment on lines +189 to +192
x_min, x_step, y_max, y_step = write_array.call_args.args[:4]
assert (x_min, x_step, y_max, y_step) == (-12.5, 5.0, 55.0, -10.0)
for value in (x_min, x_step, y_max, y_step):
assert isinstance(value, float)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in b88badd — accepts (int, float) and asserts not isinstance(value, np.generic). This is also a stronger check where it matters: np.float64 is a subclass of float, so the previous assertion wouldn't have caught a NumPy scalar leaking through.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Geotiff raster saving broken with numpy >= 2.4.0

3 participants