<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:courier new,monospace">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.<br>
<br>#include <iostream><br>#include <string><br><br>#include <gdal/gdal_priv.h><br>#include <gdal/gdal_alg.h><br>#include <gdal/gdalwarper.h><br>#include <gdal/ogr_spatialref.h><br>#include <gdal/cpl_conv.h><br>

#include <gdal/cpl_minixml.h><br><br>namespace {<br>    const size_t SUCCESS = 0;<br>    const size_t ERROR_IN_COMMAND_LINE = 1;<br>    const size_t ERROR_IN_GDAL = 2;<br>    const size_t ERROR_UNHANDLED_EXCEPTION = 3;<br>

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

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

}<br><br>int main(int argc, char **argv)<br>{<br>    try {<br>    std::string pszFilename = "/mnt/raid/dted/w078/n34.dt2";<br><br>    OGRErr nOGRError;<br>    CPLErr eCPLError;<br><br>    GDALAllRegister();<br>
<br>
    // GDAL: opening the input file<br><br>    GDALDataset *poSrcDataset;<br><br>    poSrcDataset = (GDALDataset *)GDALOpen(pszFilename.c_str(),<br>                           GA_ReadOnly);<br>    if (poSrcDataset == NULL) {<br>

        std::cerr << CPLGetLastErrorMsg() << std::endl;<br>        return ERROR_IN_GDAL;<br>    }<br><br>    // GDAL: warp data<br><br>    GDALDatasetH hSrcDataset = poSrcDataset;<br>    GDALDatasetH hDstDataset;<br>

<br>    GDALDriverH hDstDriver;<br><br>    const char *pszDstProj4 = "+proj=utm +zone=11 +datum=WGS84";<br><br>    const char *pszSrcWKT = poSrcDataset->GetProjectionRef();<br>    char *pszDstWKT = NULL;<br>
<br>
        OGRSpatialReference oSRS;<br><br>    double adfDstGeoTransform[6];<br>    int nPixels = 0, nLines = 0;<br><br>    nOGRError = oSRS.importFromProj4(pszDstProj4);<br>    OGRCheckError(nOGRError, "Error importing proj4 string");<br>

<br>    nOGRError = oSRS.exportToWkt(&pszDstWKT);<br>    OGRCheckError(nOGRError, "Error converting proj4 string to WKT");<br><br>    void *hTransformArg;<br><br>    hTransformArg =<br>        GDALCreateGenImgProjTransformer(hSrcDataset, pszSrcWKT,<br>

                        NULL, pszDstWKT,<br>                        FALSE, 0, 1);<br>    if (hTransformArg == NULL) {<br>        std::cerr << CPLGetLastErrorMsg() << std::endl;<br>        return ERROR_IN_GDAL;<br>

    }<br><br>    eCPLError = GDALSuggestedWarpOutput(hSrcDataset,<br>                        GDALGenImgProjTransform,<br>                        hTransformArg,<br>                        adfDstGeoTransform,<br>                        &nPixels, &nLines );<br>

    CPLCheckError(eCPLError);<br><br>    // Construct destination Dataset<br><br>    std::cout << "Constructing output dataset" << std::endl;<br><br>    GDALDataset *poDstDataset;<br><br>    poDstDataset = GetGDALDriverManager()-><br>

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

<br>    // Warp data<br><br>    std::cout << "Reprojecting image" << std::endl;<br><br>    eCPLError = GDALReprojectImage(hSrcDataset, pszSrcWKT,<br>                       hDstDataset, pszDstWKT,<br>

                       GRA_Bilinear,<br>                       0.0, 0.0,<br>                       GDALTermProgress, NULL,<br>                       NULL);<br>    CPLCheckError(eCPLError);<br>    }<br><br>    catch(std::exception& e) {<br>

    std::cerr << "Unhandled Exception reached the top of main: "<br>          << e.what() << ", application will now exit" << std::endl;<br>    return ERROR_UNHANDLED_EXCEPTION;<br>

    }<br><br>    return SUCCESS;<br>}<br><br>The output in GDB is:<br><br>Starting program: /home/gordon/src/novcs/gdal-test/gdal-test-mwp<br>[Thread debugging using libthread_db enabled]<br>Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".<br>

Constructing output dataset<br>Reprojecting image<br><br>Program received signal SIGSEGV, Segmentation fault.<br>0x00007ffff7628a4c in GDALGetGeoTransform () from /usr/lib/libgdal.so.1<br><br>The program is compiled with<br>

<br>g++ -g gdal-test-mwp.cc -lgdal -Bshared -o gdal-test-mwp<br><br>I using the Debian Wheezy GDAL package:<br><br>Desired=Unknown/Install/Remove/Purge/Hold<br>| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend<br>

|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)<br>||/ Name           Version      Architecture Description<br>+++-==============-============-============-=================================<br>ii  libgdal1       1.9.0-3.1    amd64        Geospatial Data Abstraction Libra<br>

<br>Anybody got any ideas why this is failing?<br><br>Gordon</span></div><br>-- <br>Gordon Farquharson<br>GnuPG Key ID: 32D6D676
</div>