Skip to content

ds.ReadAsArray errors when xoff, yoff are numpy.int64 #8026

@scottstanie

Description

@scottstanie

Expected behavior and actual behavior.

I can read chunks of rasters in python using integer start/offsets using

ds.ReadAsArray(0, 0, 64, 64)

but this errors when 64 is actually a numpy integer.

Steps to reproduce the problem.

testing on gdal installed from conda, version 3.7.0

conda create -n gdal-bug python=3.11 gdal
conda activate gdal-bug
$ python
Python 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:41) [Clang 15.0.7 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from osgeo import gdal
>>> ds = gdal.Open("test.tif")
/Users/staniewi/miniconda3/envs/gdal-bug/lib/python3.11/site-packages/osgeo/gdal.py:287: FutureWarning: Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.
  warnings.warn(
>>> ds.ReadAsArray().shape
(128, 128)
>>> ds.ReadAsArray(0, 0, 64, 64).shape
(64, 64)

# But when these are numpy integers, this fails
>>> import numpy as np
>>> ds.ReadAsArray(0, 0, np.int64(64), np.int64(64))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/staniewi/miniconda3/envs/gdal-bug/lib/python3.11/site-packages/osgeo/gdal.py", line 3317, in ReadAsArray
    return gdal_array.DatasetReadAsArray(self, xoff, yoff, xsize, ysize, buf_obj,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/staniewi/miniconda3/envs/gdal-bug/lib/python3.11/site-packages/osgeo/gdal_array.py", line 260, in DatasetReadAsArray
    return BandReadAsArray(ds.GetRasterBand(band_list[0]), xoff, yoff, win_xsize, win_ysize,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/staniewi/miniconda3/envs/gdal-bug/lib/python3.11/site-packages/osgeo/gdal_array.py", line 451, in BandReadAsArray
    if BandRasterIONumPy(band, 0, xoff, yoff, win_xsize, win_ysize,
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/staniewi/miniconda3/envs/gdal-bug/lib/python3.11/site-packages/osgeo/gdal_array.py", line 103, in BandRasterIONumPy
    return _gdal_array.BandRasterIONumPy(band, bWrite, xoff, yoff, xsize, ysize, psArray, buf_type, resample_alg, callback, callback_data)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: in method 'BandRasterIONumPy', argument 5 of type 'double'

(I created the test file with drv.Create("test.tif", 128, 128, 1, gdal.GDT_Float32)

Operating system

Tested on both OSX and linux.

GDAL version and provenance

Tested on gdal 3.6.3, 3.6.4, 3.7.0 from conda forge.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions