[GIS] Reading 1/3 arcsecond DEM data using C++

cdemelevation

I am writing my own application to use and present elevation data. So far, I've been easily using the three arc second SRTM data presented in ArcInfo ASCII format. But now I'm looking to use the higher resolution 1/3 arc second data. And I find them downloadable in three formats: arcgrid, gridfloat, and img. No ASCII format.

I looked into the arcgrid format, and it looks utterly opaque and complicated. So here's the question if anyone knows: which of these three would be easiest to code for, to read the elevation data into a C or C++ array in my program. And if anyone has any code snippets to share, I'd love to see them.

Best Answer

This is very doable using C++. Some example code is below. You just need to use the correct driver. See here for driver formats. "HFA" is the code for the Erdas imagine (.img) format.

GDALDataset *pDS;
GDALDriver *pDriver;
pDriver = GDALGetDriverManager->getDriverByName("Name of you driver here");

//open the raster in read only mode   
pDS = (GDALDataset*) GDALOpen("Filepath here", GA_ReadOnly);

//create the array to hold the read data, I usually read rasters one row at at a time
float *rowData = (float*)CPLMalloc(sizeof(float)*pDS->GetRasterXSize);

//loop through all rows of the raster and read data into the array
for (int i=0; i<pDS->GetRasterYSize)
{
    pDS->GetRasterBand(1)->RasterIO(GF_Read, 0, i, pDS->GetRasterXSize, 1, rowData, pDS->GetRasterXSize, 1, 0, 0);
    //here's where you can do any operations on the row of data
}

//free the memory
CPLFree(rowData);

//close the raster
GDALClose(pDS);
Related Question