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

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

Closed
scottstanie opened this issue Jun 28, 2023 · 0 comments · Fixed by #8054
Closed

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

scottstanie opened this issue Jun 28, 2023 · 0 comments · Fixed by #8054
Assignees

Comments

@scottstanie
Copy link
Contributor

scottstanie commented Jun 28, 2023

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.

@rouault rouault self-assigned this Jul 4, 2023
rouault added a commit to rouault/gdal that referenced this issue Jul 4, 2023
… win_xsize, win_ysize, buf_xsize, buf_ysize arguments of ReadAsArray() (fixes OSGeo#8026)
rouault added a commit to rouault/gdal that referenced this issue Jul 4, 2023
… win_xsize, win_ysize, buf_xsize, buf_ysize arguments of ReadAsArray() (fixes OSGeo#8026)
rouault added a commit to rouault/gdal that referenced this issue Jul 5, 2023
… win_xsize, win_ysize, buf_xsize, buf_ysize arguments of ReadAsArray() (fixes OSGeo#8026)
jeffypooo pushed a commit to IgnisTechnologies/gdal that referenced this issue Aug 9, 2023
… win_xsize, win_ysize, buf_xsize, buf_ysize arguments of ReadAsArray() (fixes OSGeo#8026)
scottstanie added a commit to scottstanie/MintPy that referenced this issue Aug 15, 2023
This is fixed in gdal versions >3.7.0 OSGeo/gdal#8026

The issue came from passing `np.int64` objects to GDAL, which it wasnt handling properly. Regular python `int`s work find
scottstanie added a commit to insarlab/MintPy that referenced this issue Aug 19, 2023
…pability (#1072)

* utils0: fix typos

* utils0: simplify look of `update_attribute_or_not`

The bare `next` was acting as a `pass` or `continue`

* fix more typos caught by vscode

* use `ReadAsArray` after casting box to int

This is fixed in gdal versions >3.7.0 OSGeo/gdal#8026

The issue came from passing `np.int64` objects to GDAL, which it wasnt handling properly. Regular python `int`s work find

* remove unused import

* gps: print the filename, instead of (possibly blank) dirname

* convert `Path`s to str for readfile

also fix grammar for file not found

* remove 2nd version of wkt function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants