[gdal-dev] Issue with ReadBlock

Even Rouault even.rouault at spatialys.com
Wed Jan 20 01:06:34 PST 2016


Le mercredi 20 janvier 2016 08:26:51, trewdbal a écrit :
> 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#ad80cecc562fd48f5783677de6253
> 60ac> ), I just change the type of the pabyData array (I need float32).
> I tested with several raster formats, without result.

You can only use a destination array of Float32 if the natural data type 
reported by GetDataType() is GDT_Float32. IReadBlock() has no way to know the 
data type you expect.
If you need to read in a particular data type, which is not necessarily the 
natural data type of the band, you must use RasterIO(GF_Read, ... , 
GDT_Float32, ....) instead

> 
> 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));

sizeof(GByte) == sizeof(unsigned char) == 1 per the C standard

> 
> 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.
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/gdal-dev

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list