[gdal-dev] Segmentation fault when using GDALReprojectImage

Gordon Farquharson gordonfarquharson at gmail.com
Sat May 24 15:53:35 PDT 2014


I am trying to warp a dataset read with the DTED driver by using
GDALReprojectImage. The output dataset is created with the MEM driver. The
program is failing with a segmentation fault. Below is a minimal working
program that shows the error, and the resulting output when run in GDB.

#include <iostream>
#include <string>

#include <gdal/gdal_priv.h>
#include <gdal/gdal_alg.h>
#include <gdal/gdalwarper.h>
#include <gdal/ogr_spatialref.h>
#include <gdal/cpl_conv.h>
#include <gdal/cpl_minixml.h>

namespace {
    const size_t SUCCESS = 0;
    const size_t ERROR_IN_COMMAND_LINE = 1;
    const size_t ERROR_IN_GDAL = 2;
    const size_t ERROR_UNHANDLED_EXCEPTION = 3;
}

void OGRCheckError(OGRErr errnum, const char *errstr)
{
    if (errnum != OGRERR_NONE)    {
    std::cout << errstr << " (" << errnum << ")" << std::endl;
    exit(ERROR_IN_GDAL);
    }
}

void CPLCheckError(CPLErr errnum)
{
    if (errnum == CE_Failure) {
    std::cerr << CPLGetLastErrorMsg() << std::endl;
    exit(ERROR_IN_GDAL);
    }
}

int main(int argc, char **argv)
{
    try {
    std::string pszFilename = "/mnt/raid/dted/w078/n34.dt2";

    OGRErr nOGRError;
    CPLErr eCPLError;

    GDALAllRegister();

    // GDAL: opening the input file

    GDALDataset *poSrcDataset;

    poSrcDataset = (GDALDataset *)GDALOpen(pszFilename.c_str(),
                           GA_ReadOnly);
    if (poSrcDataset == NULL) {
        std::cerr << CPLGetLastErrorMsg() << std::endl;
        return ERROR_IN_GDAL;
    }

    // GDAL: warp data

    GDALDatasetH hSrcDataset = poSrcDataset;
    GDALDatasetH hDstDataset;

    GDALDriverH hDstDriver;

    const char *pszDstProj4 = "+proj=utm +zone=11 +datum=WGS84";

    const char *pszSrcWKT = poSrcDataset->GetProjectionRef();
    char *pszDstWKT = NULL;

        OGRSpatialReference oSRS;

    double adfDstGeoTransform[6];
    int nPixels = 0, nLines = 0;

    nOGRError = oSRS.importFromProj4(pszDstProj4);
    OGRCheckError(nOGRError, "Error importing proj4 string");

    nOGRError = oSRS.exportToWkt(&pszDstWKT);
    OGRCheckError(nOGRError, "Error converting proj4 string to WKT");

    void *hTransformArg;

    hTransformArg =
        GDALCreateGenImgProjTransformer(hSrcDataset, pszSrcWKT,
                        NULL, pszDstWKT,
                        FALSE, 0, 1);
    if (hTransformArg == NULL) {
        std::cerr << CPLGetLastErrorMsg() << std::endl;
        return ERROR_IN_GDAL;
    }

    eCPLError = GDALSuggestedWarpOutput(hSrcDataset,
                        GDALGenImgProjTransform,
                        hTransformArg,
                        adfDstGeoTransform,
                        &nPixels, &nLines );
    CPLCheckError(eCPLError);

    // Construct destination Dataset

    std::cout << "Constructing output dataset" << std::endl;

    GDALDataset *poDstDataset;

    poDstDataset = GetGDALDriverManager()->
        GetDriverByName("MEM")->
        Create("", nPixels, nLines,
           poSrcDataset->GetRasterCount(),
           poSrcDataset->GetRasterBand(1)->GetRasterDataType(),
           NULL);
    if (poDstDataset == NULL) {
        std::cerr << CPLGetLastErrorMsg() << std::endl;
        return ERROR_IN_GDAL;
    }

    poDstDataset->SetProjection(pszDstWKT);
    poDstDataset->SetGeoTransform(adfDstGeoTransform);

    // Warp data

    std::cout << "Reprojecting image" << std::endl;

    eCPLError = GDALReprojectImage(hSrcDataset, pszSrcWKT,
                       hDstDataset, pszDstWKT,
                       GRA_Bilinear,
                       0.0, 0.0,
                       GDALTermProgress, NULL,
                       NULL);
    CPLCheckError(eCPLError);
    }

    catch(std::exception& e) {
    std::cerr << "Unhandled Exception reached the top of main: "
          << e.what() << ", application will now exit" << std::endl;
    return ERROR_UNHANDLED_EXCEPTION;
    }

    return SUCCESS;
}

The output in GDB is:

Starting program: /home/gordon/src/novcs/gdal-test/gdal-test-mwp
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Constructing output dataset
Reprojecting image

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7628a4c in GDALGetGeoTransform () from /usr/lib/libgdal.so.1

The program is compiled with

g++ -g gdal-test-mwp.cc -lgdal -Bshared -o gdal-test-mwp

I using the Debian Wheezy GDAL package:

Desired=Unknown/Install/Remove/Purge/Hold
|
Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name           Version      Architecture Description
+++-==============-============-============-=================================
ii  libgdal1       1.9.0-3.1    amd64        Geospatial Data Abstraction
Libra

Anybody got any ideas why this is failing?

Gordon

-- 
Gordon Farquharson
GnuPG Key ID: 32D6D676
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/gdal-dev/attachments/20140524/7c5012ea/attachment-0001.html>


More information about the gdal-dev mailing list