Geoscience Reference
In-Depth Information
double uly=gt[3];
double lrx=ulx+ncol*gt[1]+nrow*gt[2];
double lry=uly+ncol*gt[4]+nrow*gt[5];
cout <<"Upper left x: "<< ulx << endl;
cout <<"Upper left y: "<< uly << endl;
cout <<"Lower right x: "<< lrx << endl;
cout <<"Lower right y: "<< lry << endl;
//calculate center location
double centerX=ulx+(ncol/2.0)*gt[1]+(nrow/2.0)*gt[2];
double centerY=gt[3]+(ncol/2.0)*gt[4]+(nrow/2.0)*gt[5];
cout <<"Center coordinate x: "<< centerX << endl;
cout <<"Center coordinate y: "<< centerY << endl;
In the next part, we access the raster values by reading the first line of each band of
the Landsat image. We iterate through the number of bands in a for loop and access
the raster information band per band.
GDALRasterBand *poBand;//the raster band corresponding to the
individual Landsat spectral bands
unsigned short *pnbuffer;//a buffer that will hold data for an entire
image line
//allocate memory to read an entire image line
pnbuffer=( unsigned short *) CPLMalloc( sizeof ( unsigned
short )*ncol);
//define arguments for rasterIO function
int nXOff=0;//start reading at first column (index is 0 based)
int nYOff=0;//start reading at first line (index is 0 based)
int nXSize=ncol;//read all columns
int nYSize=1;//read single line
int nBufXSize=nXSize;//width of the buffer in which to read
int nBufYSize=1;//height of the buffer in which to read
int nPixelSpace=0;//use the default: do not use byte offset
for pixels
int nLineSpace=0;//use the default: do not use byte offset for lines
//loop over all available bands
int nband=poDataset->GetRasterCount();//number of bands in our Landsat
raster
for ( int iband=0;iband<nband;++iband){
//fetch a raster band in GDAL (index is 1 based: start counting from
1)
poBand = poDataset->GetRasterBand(iband+1);
//read the first line into pnbuffer
//raster data is of type GDT_UInt16
poBand->RasterIO(GF_Read, nXOff, nYOff, nXSize, nYSize, pnbuffer,
nBufXSize, nBufYSize, GDT_UInt16, nPixelSpace, nLineSpace);
}
//release the memory taken by pnbuffer
CPLFree(pnbuffer);
 
Search WWH ::




Custom Search