[Gdal-dev] reading hdf4 file

Frank Warmerdam fwarmerdam at gmail.com
Mon Jul 25 19:14:39 EDT 2005


On 7/25/05, Sabine Grabner <csab7771 at uibk.ac.at> wrote:
> hello,
> 
> i try to read a hdf4 file but poDataset->GetRasterCount() returns 0 bands even
> though gdalinfo lists all bands of the file.
> 
> i work with GDAL 1.2.6.0 from 03/13/2005.
> 
> can anybody help out please?

Sabine, 

It would be helpful to show the gdalinfo report.  Most likely the gdalinfo
report is listing a set of subdatasets but not any bands.  Generally for
HDF you need to open the file, get a list of subdatasets (from metadata)
and then open one or more of those subdatasets in order to get the 
accesss to the imagery.

The following code from gdal_translate might give an idea how to get
the list of subdatasets, and use it. 

/* -------------------------------------------------------------------- */
/*      Handle subdatasets.                                             */
/* -------------------------------------------------------------------- */
    if( CSLCount(GDALGetMetadata( hDataset, "SUBDATASETS" )) > 0 )
    {
        if( !bCopySubDatasets )
        {
            fprintf( stderr,
                     "Input file contains subdatasets. Please, select
one of them for reading.\n" );
        }
        else
        {
            char **papszSubdatasets = GDALGetMetadata(hDataset,"SUBDATASETS");
            char *pszSubDest = (char *) CPLMalloc(strlen(pszDest)+32);
            int i;
            int bOldSubCall = bSubCall;

            argv[iDstFileArg] = pszSubDest;
            bSubCall = TRUE;
            for( i = 0; papszSubdatasets[i] != NULL; i += 2 )
            {
                argv[iSrcFileArg] = strstr(papszSubdatasets[i],"=")+1;
                sprintf( pszSubDest, "%s%d", pszDest, i/2 + 1 );
                if( ProxyMain( argc, argv ) != 0 )
                    break;
            }

            bSubCall = bOldSubCall;
            CPLFree( pszSubDest );
        }

        GDALClose( hDataset );

        if( !bSubCall )
        {
            GDALDumpOpenDatasets( stderr );
            GDALDestroyDriverManager();
        }
        return 1;
    }

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent




More information about the Gdal-dev mailing list