[gdal-dev] Python Bindings - ReadArray direct to ctypes array?

Chris Barker chris.barker at noaa.gov
Mon Jul 30 21:36:10 PDT 2012

On Mon, Jul 30, 2012 at 8:19 AM, Jay L. <jzl5325 at psu.edu> wrote:
> Chris,
> Thanks for the link / info.  My issue is not working with the ctypes array,
> but the fact that, I believe, GDAL returns a numpy array using
> gdal.ReadAsArray().  Perhaps it is possible to use the GDAL python bindings
> ReadAsArray() to go directly into a ctypes array (as opposed to the
> wonderful code post earlier in this thread), but I have missed it.
> Is it possible, not using the previously posted code, to do something like:
> empty_ctypes_array = multiprocessing.RawArray(data_type, size)
> empty_ctypes_array.ReadAsArray()

what I was getting at is that you should be able to use ReadAsArray to
get a bumpy array, then crate a types array from that without any data
copying -- I"m pretty sure. So while you would still have the
intermediate step of the numpy array, that overhead would be minor if
your raster bands are not tiny.

Ideally, GDAL would produce PEP 3118 compatible buffers, and then you
could create either bumpy arrays or ctypes arrays from that -- not
sure how the code is written now, though.

But in fact, numpy arrays are really two things:

1) full-featured n-d arrays for python

2) nifty wrappers around a c data pointer

If you think them as (2) then there are exactly the right thing to use!

> I will definitely check out the SciPy mailing list to see what the current
> trends in multiprocessing are on large arrays.

I'd probably go to Cython and forget about ctypes anyway ;-)


Christopher Barker, Ph.D.

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov

More information about the gdal-dev mailing list