[gdal-dev] Get pixel values from all bands

Eli Adam EAdam at co.lincoln.or.us
Wed Mar 16 16:41:35 EDT 2011


Alexander,

    Not sure if it meets you situation, but there is a command line utility with similar functionality, gdallocationinfo, http://gdal.org/gdallocationinfo.html 

HTH, Eli


>>> On 3/16/2011 at 1:21 PM, in message
<20110316222128.b943f4e3.alexander.bruy at gmail.com>, Alexander Bruy
<alexander.bruy at gmail.com> wrote:
> Hi all
> 
> I work on raster processing application (developed with C++ and Qt).
> For reading rasters we use GDAL and it's C++ API.
> 
> For one task we need to get pixel values from all raster bands. For
> this I get all GDALRasterBands and store them in list. Then in loop
> read image row by row and store row from each band in another list.
> 
> Here is simplified code
> 
> // iterate over image row by row
> for ( int row = 0; row < ySize; ++row )
> {
>   // read one row from each band and store it in list
>   for ( int b = 0; b < bandCount; ++b )
>   {
>     float *scanline;
>     scanline = (float *) CPLMalloc( sizeof( float ) * xSize );
>     bands[ i ]->RasterIO( GF_Read, 0, row, xSize, 1, scanline, xSize,
>                           1, GDT_Float32, 0, 0 );
>     rows[ i ] = scanline;
>   }
> 
>   // iterate on pixels in row
>   for (int col = 0; col < ySize; ++col)
>   {
>     // get pixel value from bands
>     float pixel[bandCount];
>     for ( int r = 0; r < bandCount; ++r )
>     {
>        // get value in (row, col) from band r
>        double pixel[r] = rows[ r ][ col ];
>     }
>     // do something with this pixel
>   }
> }
> 
> Is this correct approach? Or maybe there is another more correct way
> to do this?
> 
> Also I'm not sure where I should free memory allocated with CPLMalloc().
> After reading pixels and before reading next rows or at end when
> all rows processed?
> 
> I'm not a big C++ programmer, so any help is very appreciated
> 
> Thanks



More information about the gdal-dev mailing list