[gdal-dev] Can GDAL open binary buffer contents as input for MEM format?

Frank Warmerdam warmerdam at pobox.com
Sat Jan 16 16:26:44 EST 2010


Roger André wrote:
> Hi All,
> 
> I'd like to know if I can take the results of a MapServer Python 
> mapscript 'mapImage.getBytes()' operation, and feed it into GDAL as a 
> data source for an in-memory raster?  The MapServer docs state that the 
> results of 'getBytes' in Python is a "string of binary data".  I'm not 
> sure if GDAL can open this sort of object though.
> 
> For background purposes, the reason I would like to do this is so that I 
> can try replacing PIL with GDAL in TileCache for the cutting of 
> metatiles.  PIL isn't properly saving the image formats generated by 
> MapServer, whereas GDAL replicates them perfectly.

Roger,

I am not aware of a way to utilize a raw buffer as a mem dataset from
Python (though this is doable from C++).  However, you can just write
the buffer to a mem dataset.  This code snippet from the autotest suite
shows something like this:

     #######################################################
     # Setup dataset
     drv = gdal.GetDriverByName('MEM')
     gdaltest.mem_ds = drv.Create( 'mem_1.mem', 50, 3, gdal.GDT_Int32, 1 )
     ds = gdaltest.mem_ds

     raw_data = array.array('f',list(range(150))).tostring()
     ds.WriteRaster( 0, 0, 50, 3, raw_data,
                     buf_type = gdal.GDT_Float32,
                     band_list = [1] )

It is slightly more tricky with what I assume is a pixel interleaved 3
band image you would have.  Without having tested it, something like this
might work:

   mem_ds = gdal.GetDriverByName('MEM').Create('x',200,300,gdal.GDT_Byte,3)
   mem_ds.WriteRaster( 0, 0, 200, 300,
                       mapImage.getBytes(), 200, 300, gdal.GDT_Byte,
                       band_list=[1,2,3],
                       buf_pixel_space=3,
                       buf_line_space=3*200,
                       buf_band_space=1)

Good luck,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent



More information about the gdal-dev mailing list