Question

I'm trying to convert a DTED file in another format so I can display it in an application, for now I'm targetting the JPEG format. What I need is basically the gdal_translate command but through the C++ API.

I'm using the GDAL C++ API and I can read the input DTED file and read it without problem :

GDALAllRegister();

GDALDataset  *poDataset;

poDataset = (GDALDataset *) GDALOpen( "n43.dt2", GA_ReadOnly );
if( poDataset == NULL )
{
   return 0;
}

I can also access the corresponding band without an issue.

GDALRasterBand  *poBand;
int             nBlockXSize, nBlockYSize;
int             bGotMin, bGotMax;
double          adfMinMax[2];

poBand = poDataset->GetRasterBand( 1 );
poBand->GetBlockSize( &nBlockXSize, &nBlockYSize );
printf( "Block=%dx%d Type=%s, ColorInterp=%s\n",
        nBlockXSize, nBlockYSize,
        GDALGetDataTypeName(poBand->GetRasterDataType()),
        GDALGetColorInterpretationName(
            poBand->GetColorInterpretation()) );

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

printf( "Min=%.3fd, Max=%.3f\n", adfMinMax[0], adfMinMax[1] );

if( poBand->GetOverviewCount() > 0 )
    printf( "Band has %d overviews.\n", poBand->GetOverviewCount() );

if( poBand->GetColorTable() != NULL )
    printf( "Band has a color table with %d entries.\n",
                     poBand->GetColorTable()->GetColorEntryCount() );

But I can't figure out how to add this band in another dataset using the wanted driver. My application crashes when I try to use the AddBand function.

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

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


GDALClose(poDataset);
GDALDataset  *resDataset;
GDALRasterBand  *resBand;
resDataset->AddBand (GDT_Float32, NULL);//<-application crashes here

/*resBand = resDataset->GetRasterBand(1);
resBand->RasterIO( GF_Write, 0, 0, nXSize, 1,
                  pafScanline, nXSize, 1, GDT_Float32,
                  0, 0 );*/

So I guess what I'm trying to do is not the proper way to do what I need. Could you explain to me what I am doing wrong ?

Was it helpful?

Solution

Okay I think I figured it out : unless going through a virtual raster, I shouldn't use raster bands but just CreatCopy.

Here's a working code for me :

#include <iostream>
#include "gdal_priv.h"
#include "cpl_conv.h" // for CPLMalloc()

using namespace std;

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

GDALAllRegister();
GDALDatasetH  poDataset;

poDataset = (GDALDataset *) GDALOpenShared( "n43.dt2", GA_ReadOnly );
if( poDataset == NULL )
{
   return 0;
}

const char *pszFormat = "PNG";

GDALDatasetH  resDataset;

GDALProgressFunc    pfnProgress = GDALTermProgress;
GDALDriverH hDriver = GDALGetDriverByName( pszFormat );
const char *pszDest = "n43.png";
char **papszCreateOptions = NULL;

resDataset = GDALCreateCopy( hDriver, pszDest, poDataset,
                                 FALSE, papszCreateOptions,
                                 pfnProgress, NULL );

if( resDataset != NULL )
{
    GDALClose( resDataset );
}
else
{
    printf("Error creating output dataset.");
}

GDALClose(poDataset);
CSLDestroy( papszCreateOptions );

return 1;
}

This gives a slightly brighter PNG image than the one I get from using gdal_translate, I still need to figure out what's causing this. This works with JPEG images but the result cannot be read (I think it's an application specific format as it is called "JPEG JTIF" by gdalinfo --formats).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top