[gdal-dev] Issue with ReadBlock

trewdbal arnaud.durand at gmail.com
Tue Jan 19 23:26:51 PST 2016


Hi Folks,

I'm trying to read an image using ReadBlock, but the array is not filled
with raster values.
It works with RasterIO.

The code below is copied from the GDAL doc ( page
<http://www.gdal.org/classGDALRasterBand.html#ad80cecc562fd48f5783677de625360ac> 
), I just change the type of the pabyData array (I need float32).
I tested with several raster formats, without result.

Another point, maybe linked with the previous, in the doc at the line :
GByte *pabyData = (GByte *) CPLMalloc(nXBlockSize * nYBlockSize);
I'm confused not reading 
GByte *pabyData = (GByte *) CPLMalloc(nXBlockSize * nYBlockSize *
sizeof(GByte));

Thanks for any help.
Trewdbal

int main()
{
	std::cout.precision(12);

	GDALAllRegister();
	const char* PANFile = "/home/ficus/Code/Covariance/rasters/pan2048.tif";

	GDALDataset  *poDataset;
	poDataset = (GDALDataset *) GDALOpen( PANFile, GA_ReadOnly );

	GDALRasterBand  *poBand;
	int nXBlockSize, nYBlockSize;

	poBand = poDataset->GetRasterBand( 1 );
	poBand->GetBlockSize( &nXBlockSize, &nYBlockSize );

	unsigned int SIZEX = poBand->GetXSize();
	unsigned int SIZEY = poBand->GetYSize();

	int nXBlocks = (SIZEX + nXBlockSize - 1) / nXBlockSize;
	int nYBlocks = (SIZEY + nYBlockSize - 1) / nYBlockSize;

	std::cout << " NBlocks = " << nXBlocks << " " << nYBlocks << std::endl;
	std::cout << " Block Size = " << nXBlockSize << " " << nYBlockSize <<
std::endl;

	float* pabyData = (float*)malloc(nXBlockSize * nYBlockSize *
sizeof(float));

	int nXValid = 0;
	int nYValid = 0;
	
	for( int iYBlock = 0; iYBlock < nYBlocks; iYBlock++ )
	{
		for( int iXBlock = 0; iXBlock < nXBlocks; iXBlock++ )
		{
		poBand->ReadBlock( iXBlock, iYBlock, pabyData );

			std::cout << "test " << pabyData[25] << std::endl; // Where we see that
the array is full of 0 values...

			// Compute the portion of the block that is valid
			// for partial edge blocks.
			if( (iXBlock+1) * nXBlockSize > poBand->GetXSize() )
				nXValid = poBand->GetXSize() - iXBlock * nXBlockSize;
			else
				nXValid = nXBlockSize;

			if( (iYBlock+1) * nYBlockSize > poBand->GetYSize() )
				nYValid = poBand->GetYSize() - iYBlock * nYBlockSize;
			else
				nYValid = nYBlockSize;
		}
	}

	free(pabyData);

	GDALClose( poDataset );

	GDALDestroyDriverManager();
	return 0;
}




--
View this message in context: http://osgeo-org.1560.x6.nabble.com/Issue-with-ReadBlock-tp5246218.html
Sent from the GDAL - Dev mailing list archive at Nabble.com.


More information about the gdal-dev mailing list