c++gdalelevationgeotiffaltitude

GDAL GeoTiff get elevation at a specified pixel help C++


I am understanding English not so well and I am not the best C++ Programmer so I decided to ask a question here to compensate this.

I am trying to get the elevation out of an GeoTiff image I downloaded from this page: http://terracolor.net/sample_imagery.html

I get some information out of the Image via GDAL lib but I am not sure what this exactly is. Is it the colour? Are this coordinates? What unity is that? I am also confused by the different bands I can read in. Maybe someone who used GDAL in the past can explain me what this is all about. What I like to have in the end is the altitude in for example meters at a given Pixel.

Here is my Code:

int ofApp::getAlt(int x,int y){ 
GDALDataset  *poDataset;

GDALAllRegister();

poDataset = (GDALDataset *) GDALOpen( "data/test.tif", GA_ReadOnly );
if( poDataset == NULL )
{
    cout << "no" << endl;
}else{
    GDALRasterBand  *poBand;
    int             nBlockXSize, nBlockYSize;
    int             bGotMin, bGotMax;
    double          adfMinMax[2];

    //printf( "Size is %dx%dx%d\n", poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), poDataset->GetRasterCount() );

    poBand = poDataset->GetRasterBand( 3 );
    poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );

    adfMinMax[0] = poBand->GetMinimum( &bGotMin );
    adfMinMax[1] = poBand->GetMaximum( &bGotMax );
    if( ! (bGotMin && bGotMax) )
        GDALComputeRasterMinMax((GDALRasterBandH)poBand, TRUE, adfMinMax);

    float *pafScanline;
    int   nXSize = poBand->GetXSize();

    pafScanline = (float *) CPLMalloc(sizeof(float)*nXSize);
    poBand->RasterIO( GF_Read, x, y, 1, 1, 
                      pafScanline, nXSize, 1, GDT_Float32, 
                      0, 0 );

    //cout << "vvv" << pafScanline[0] << endl;
    //printf( "value %f \n", pafScanline[0]);
    return pafScanline[0];
 }
 }

Solution

  • You are right. What simple mistake. These imagery i used had no such elevation data as a band in in it. I now used images from http://srtm.csi.cgiar.org/SELECTION/inputCoord.asp which serve the needed data with in a band how I needed it. I can then use my function to read it out.