[Gdal-dev] how to use GDAL MEM driver

Frank Warmerdam warmerdam at pobox.com
Sun Nov 5 15:18:23 EST 2006


Zhaoqiang Bi wrote:
 >     dat=(type *)malloc(block_lines*block_pixels*pixelbytes);
...
>     dat2=(type **)malloc(block_lines*sizeof(type *));
...
>     tmp=(type *)dat;
> 
>     for(i=0;i<block_lines;i++) dat2[i]=&tmp[i*block_pixels];
> 
>    
> 
>     /* Create a GDAL object */
> 
>     long int datpt =(long int) &dat2;
...
>     sprintf(filename,"MEM:::DATAPOINTER=%d,PIXELS=%d,LINES=%d,BANDS=1,DATATYPE=%d,PIXELOFFSET=0,LINEOFFSET=0,BANDOFFSET=0",datpt,pixels,lines,datatype);
> 
>     dataset = GDALOpen(filename,GA_Update);

Zhaoqiang Bi,

The buffer referenced by DATAPOINTER should be one continuous
buffer of the desired size.  In your case dat2 appears to be an
array of pointers to scanlines in the "dat" buffer.

It isn't completely clear to me, but I think you wanted to use the original
data as the memory array associated with the GDALDataset.  To do that
it should be sufficient to do something like:

   sprintf(filename,
         "MEM:::DATAPOINTER=%p,PIXELS=%d,LINES=%d,BANDS=1,DATATYPE=%d,"
         "PIXELOFFSET=%d,LINEOFFSET=%d,BANDOFFSET=0",
         dat, pixels, lines, datatype,
	pixelbytes, pixelbytes * block_pixels );

I have used the %p printf formatter for a somewhat more portable
handling of 64bit platforms.  Also, I have set the PIXELOFFSET,
LINEOFFSET and BANDOFFSET values more appropriately.

Later in the code you had this:
 >    band = GDALGetRasterBand(dataset,1);
 >    for (i = 0; i<lines; i++)
 >        GDALRasterIO(band, GF_Write,0,i,pixels,1,
 >                     dat, pixels,1, datatype,0,0);
 >
 >    GDALFlushCache(dataset); //crash here

This seems a bit off base to me.  You are essentially writing the
contents of the "dat" array into the band.  But the band is in
fact attached to the dat buffer already, so it is really writing
over itself. So this part should not be necessary.

Best regards,
-- 
---------------------------------------+--------------------------------------
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    | President OSGeo, http://osgeo.org




More information about the Gdal-dev mailing list