Even, <div><br></div><div>Thanks so much!  I will give this a try and get it implemented into my code.  This is also a wonderful primer on ctypes integration with existing Python scripts.</div><div><br></div><div>Thanks, </div>
<div>Jay<br><br><div class="gmail_quote">On Sat, Jul 28, 2012 at 2:17 AM, Even Rouault <span dir="ltr"><<a href="mailto:even.rouault@mines-paris.org" target="_blank">even.rouault@mines-paris.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Le samedi 28 juillet 2012 05:40:57, Jay L. a écrit :<br>
<div><div class="h5">> List,<br>
><br>
> I am working on a python script using the multiprocessing module with<br>
> shared memory ctype arrays.<br>
><br>
> Currently the workflow is to open the dataset and grab the first band, as<br>
> usual. I then read in the array, create an empty ctype array, and use<br>
> memmove to move the numpy array to my ctypes array.<br>
><br>
> Is it possible, via python, to read the array direct to a ctypes array and<br>
> bypass reading the array as an ndarray?<br>
<br>
</div></div>No, although it could certainly be extended to do so.<br>
<br>
But with ctypes, you are able to bind all C functions of the GDAL API, so you<br>
can try the following snippet. Of course, due to the nature of ctypes<br>
bindings, you must be extra carefull of the value/size of arguments you use,<br>
otherwise crashes can easily happen.<br>
<br>
from ctypes import *<br>
<br>
def getlibgdal():<br>
    import sys<br>
    # Might need name tweaking depending on platfrom & version<br>
    if sys.platform == 'win32':<br>
        libgdal = windll.LoadLibrary("gdal19.dll")<br>
    else:<br>
        libgdal = cdll.LoadLibrary("libgdal.so.1")<br>
<br>
    libgdal.GDALOpen.argtypes = [ c_char_p, c_int ]<br>
    libgdal.GDALOpen.restype = c_void_p<br>
<br>
    libgdal.GDALClose.argtypes = [ c_void_p ]<br>
<br>
    libgdal.GDALGetRasterCount.argtypes = [ c_void_p ]<br>
    libgdal.GDALGetRasterCount.res_type = c_int<br>
<br>
    libgdal.GDALGetRasterXSize.argtypes = [ c_void_p ]<br>
    libgdal.GDALGetRasterXSize.res_type = c_int<br>
<br>
    libgdal.GDALGetRasterYSize.argtypes = [ c_void_p ]<br>
    libgdal.GDALGetRasterYSize.res_type = c_int<br>
<br>
    libgdal.GDALGetRasterBand.argtypes = [ c_void_p, c_int ]<br>
    libgdal.GDALGetRasterBand.restype = c_void_p<br>
<br>
    libgdal.GA_ReadOnly = 0<br>
    libgdal.GA_Update = 1<br>
<br>
    libgdal.GF_Read = 0<br>
    libgdal.GF_Write = 1<br>
<br>
    libgdal.GDT_Unknown = 0<br>
    libgdal.GDT_Byte = 1<br>
    libgdal.GDT_UInt16 = 2<br>
    libgdal.GDT_Int16 = 3<br>
    libgdal.GDT_UInt32 = 4<br>
    libgdal.GDT_Int32 = 5<br>
    libgdal.GDT_Float32 = 6<br>
    libgdal.GDT_Float64 = 7<br>
    libgdal.GDT_CInt16 = 8<br>
    libgdal.GDT_CInt32 = 9<br>
    libgdal.GDT_CFloat32 = 10<br>
    libgdal.GDT_CFloat64 = 11<br>
<br>
    libgdal.GDALRasterIO.argtypes = [<br>
        c_void_p, # rasterband<br>
        c_int, # eRWFlag<br>
        c_int, # nXOff<br>
        c_int, # nYOff<br>
        c_int, # nXSize<br>
        c_int, # nYSize<br>
        c_void_p, # pData<br>
        c_int, # nBufXSize<br>
        c_int, # nBufYSize<br>
        c_int, # eBufType<br>
        c_int, # nPixelSpace<br>
        c_int ] # nLineSpace<br>
    libgdal.GDALRasterIO.res_type = c_int<br>
<br>
    # Don't forget to register all drivers<br>
    libgdal.GDALAllRegister()<br>
<br>
    return libgdal<br>
<br>
libgdal = getlibgdal()<br>
<br>
ds = libgdal.GDALOpen("byte.tif", libgdal.GA_ReadOnly)<br>
if ds is 0:<br>
    import sys<br>
    sys.exit(1)<br>
<br>
xsize = libgdal.GDALGetRasterXSize(ds)<br>
ysize = libgdal.GDALGetRasterYSize(ds)<br>
band = libgdal.GDALGetRasterBand(ds, 1)<br>
ar = (c_byte * (xsize * ysize))()<br>
<br>
ret = libgdal.GDALRasterIO(band, libgdal.GF_Read,<br>
                           0, 0, xsize, ysize,<br>
                           cast(ar, c_void_p),<br>
                           xsize, ysize, libgdal.GDT_Byte,<br>
                           0, 0)<br>
if ret == 0:<br>
    print('success')<br>
<br>
libgdal.GDALClose(ds)<br>
<br>
><br>
> Thanks,<br>
> J<br>
</blockquote></div><br></div>