[Gdal-dev] how to write block by block to file

Frank Warmerdam warmerdam at pobox.com
Thu Jan 15 00:58:25 EST 2004


Pushkar Pradhan wrote:
> I would like to do a block by block read from one file, process the buffer
> and write it to output file. However, my code fails with seg. fault, at the
> end of this email is my code.
> The offending line is poDstDS->poDriver->Create(...);
> 
> I'm not using CreateCopy at this moment because I don't want to do a plain
> copy. Can someone tell me the correct way to do this? Thanks,
> 
>   poDataset = (GDALDataset *) GDALOpen(inFile, GA_ReadOnly);
> 
>   imgXSize = poDataset->GetRasterXSize();
>   imgYSize = poDataset->GetRasterYSize();
>   numBands = poDataset->GetRasterCount();
> 
>   poBand = poDataset->GetRasterBand(dwtBand);
>   poBand->GetBlockSize(&blockXSize, &blockYSize);
> 
>   pData = (float *) CPLMalloc(SZFLOAT*blockXSize*blockYSize);
> 
>   poDstDS = poDriver->Create(dwtFile, imgXSize, imgYSize, 1, GDT_Byte,
> NULL);
> 
>   poBand2 = poDstDS->GetRasterBand(dwtBand);
> 
>   for(row = 0; row < imgYSize; row += blockYSize) {
>     for(col = 0; col < imgXSize; col += blockXSize) {
>       rc = poBand->RasterIO(GF_Read, col, row, blockXSize, blockYSize,
>                             pData, blockXSize, blockYSize, GDT_Float32, 0,
> 0);
> 
> 	/* I will be putting code here to manipulate pData */
> 
>       rc = poBand2->RasterIO(GF_Write, col, row, blockXSize, blockYSize,
>                             pData, blockXSize, blockYSize, GDT_Float32, 0,
> 0);

Pushkar,

Well, it is hard to be sure why it is seg faulting.  I would suggest
running it in the debugger to at least determine where it faults!

However, your above code is likely to fail on partial right side, or
bottom edge tiles/blocks if the file is actually tiled, rather than
being using scanline blocks.

Unless maximum performance is of the utmost importance, I would suggest
simplifying this to read and write the data one scanline at a time.

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    | Geospatial Programmer for Rent





More information about the Gdal-dev mailing list