Search code examples
c++gdalgeotiff

GDALOpen return null on GEOTiff file


I'm trying to open GEOTiff image with gdal library. My code:

GDALDatasetH hSrcDS = GDALOpen("/home/gamma/srtm_55_01.tif", GA_ReadOnly);
if (hSrcDS == nullptr)
    printf("failure");
else
    printf("success");

And this is output:

ERROR 4: `/home/gamma/srtm_55_01.tif' not recognized as a supported file format.
failure

Also I tried using relative path but it didn't work too.

File "/home/gamma/srtm_55_01.tif" exists and was download from here (srtm_55_01).

I thought maybe I'd made a mistake after all. I looked at the sources gdal_contour and there the code is EXACTLY the same.

Moreover, I tried to use gdal_contour on my file - everything was successful.

gamma@gamma:~$ gdalinfo --version
GDAL 2.2.3, released 2017/11/20

Solution

  • Try to follow the following order for opening the raster files

      /* register all known GDAL drivers.
       * attempt to suppress GDAL warnings.
       */
    
      GDALAllRegister();
      CPLPushErrorHandler(CPLQuietErrorHandler);
     /* -------------------------------------------------------------------- */
     /*      Open source raster file.                                        */
     /* -------------------------------------------------------------------- */
        GDALDatasetH hSrcDS = GDALOpen(pszSrcFilename, GA_ReadOnly);
        if( hSrcDS == nullptr )
            exit( 2 );
    
        GDALRasterBandH hBand = GDALGetRasterBand( hSrcDS, nBandIn );
        if( hBand == nullptr )
        {
            CPLError( CE_Failure, CPLE_AppDefined,
                      "Band %d does not exist on dataset.",
                      nBandIn );
            exit(2);
        }
    
        if( !bNoDataSet && !bIgnoreNoData )
            dfNoData = GDALGetRasterNoDataValue( hBand, &bNoDataSet );