[Gdal-dev] how to use GDAL MEM driver
Zhaoqiang Bi
zhaobi at microsoft.com
Wed Nov 1 18:48:36 EST 2006
Hi,
I am trying to create an array in memory as a GDAL dataset. Below is the code snippet to do it, but it crashes at the functions which uses dataset as inputs. Any help is appreciated!
Bi
-------------------------------------------------------------
char gdal_driver[20] = "GTiff";
GDALAllRegister();
int lines = _lines;
int pixels = _pixels;
GDALDataType datatype = get_datatype();
pixelbytes = GDALGetDataTypeSize(datatype)/8;
mode = 2;
seedmode=1;
modified = 0;
/*reset datatype to float if not specified*/
if (datatype == GDT_Unknown)
datatype = GDT_Float32;
/* Allocate memory for block */
dat=(type *)malloc(block_lines*block_pixels*pixelbytes);
if(dat==NULL)
throw "No memory";
dat2=(type **)malloc(block_lines*sizeof(type *));
if(dat2==NULL)
{
free(dat);
throw "No memory";
}
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;
char* filename = (char*) malloc(1024);
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);
if (dataset == NULL)
printf("Failed to create file %s. Might exist already.\n",filename);
throw "fail";
}
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/gdal-dev/attachments/20061101/622a6799/attachment.html
More information about the Gdal-dev
mailing list