0

I have a .dem-file with elevation data of an area and I am looking to programmatically convert this data to coordinates and elevation in C++. After a lot of searching, it looks like GDALTranslate might do the trick.

Is .xyz a good format when coordinates (northing, easting) and elevation is what I'm looking for?

Also where can I find C++ example for that scenario ?

1

This is easy to do using GDAL. Here is function I regularly use to convert raster data to XYZ format. Note: .xyz is just a space (or tab) delimited text file.

#include "gdal_priv.h"

void Raster::toXYZ(const char *rasterPath, const char *xyzPath)
{
    setProperties(rasterPath);

    GDALDataset *pRaster;
    pRaster = (GDALDataset*) GDALOpen(rasterPath, GA_ReadOnly);

    int nRows = pRaster.RasterYSize()
    int nCols = pRaster.RasterXSize()
    double noData = -9999.0
    double xtlCenter, ytlCenter, xCenter, yCenter;
    double transform[6];
    transform = pRaster->GetGeoTransform(transform)

    xtlCenter = transform[0] + (transform[1]/2.0);
    ytlCenter = transform[3] + (fabs(transform[5])/2.0);

    float *read = (float*) CPLMalloc(sizeof(float)*1);

    QFile fout(xyzPath);
    fout.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&fout);
    out.setRealNumberNotation(QTextStream::FixedNotation);

    for (int i=0; i<nRows; i++)
    {
        yCenter = ytlCenter + (i*fabs(transform[5]));
        for (int j=0; j<nCols; j++)
        {
            pRaster->GetRasterBand(1)->RasterIO(GF_Read,j,i,1,1,read,1,1,GDT_Float32,0,0);
            if (*read != noData)
            {
                xCenter = xtlCenter + (j*transform[1]);
                out.setRealNumberPrecision(5);
                out << xCenter << "\t" <<yCenter << "\t" << *read <<"\n";
            }
        }
    }
    fout.close();
    CPLFree(read);
    GDALClose(pRaster);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.