GDALDataset *BAGDataset::Open( GDALOpenInfo * poOpenInfo ) { /* -------------------------------------------------------------------- */ /* Confirm that this appears to be a BAG file. */ /* -------------------------------------------------------------------- */ if( !Identify( poOpenInfo ) ) return NULL; /* -------------------------------------------------------------------- */ /* Confirm the requested access is supported. */ /* -------------------------------------------------------------------- */ if( poOpenInfo->eAccess == GA_Update ) { CPLError( CE_Failure, CPLE_NotSupported, "The BAG driver does not support update access." ); return NULL; } /* -------------------------------------------------------------------- */ /* Open the file as an HDF5 file. */ /* -------------------------------------------------------------------- */ hid_t hHDF5 = H5Fopen( poOpenInfo->pszFilename, H5F_ACC_RDONLY, H5P_DEFAULT ); if( hHDF5 < 0 ) return NULL; /* -------------------------------------------------------------------- */ /* Confirm it is a BAG dataset by checking for the */ /* BAG_Root/Bag Version attribute. */ /* -------------------------------------------------------------------- */ hid_t hBagRoot = H5Gopen( hHDF5, "/BAG_root" ); hid_t hVersion = -1; if( hBagRoot >= 0 ) hVersion = H5Aopen_name( hBagRoot, "Bag Version" ); if( hVersion < 0 ) { H5Fclose( hHDF5 ); return NULL; } H5Aclose( hVersion ); /* -------------------------------------------------------------------- */ /* Create a corresponding dataset. */ /* -------------------------------------------------------------------- */ BAGDataset *poDS = new BAGDataset(); poDS->hHDF5 = hHDF5; /* -------------------------------------------------------------------- */ /* Extract version as metadata. */ /* -------------------------------------------------------------------- */ CPLString osVersion; if( GH5_FetchAttribute( hBagRoot, "Bag Version", osVersion ) ) poDS->SetMetadataItem( "BagVersion", osVersion ); H5Gclose( hBagRoot ); /* -------------------------------------------------------------------- */ /* Fetch the elevation dataset and attach as a band. */ /* -------------------------------------------------------------------- */ int nNextBand = 1; hid_t hElevation = H5Dopen( hHDF5, "/BAG_root/elevation" ); if( hElevation < 0 ) { delete poDS; return NULL; } BAGRasterBand *poElevBand = new BAGRasterBand( poDS, nNextBand ); if( !poElevBand->Initialize( hElevation, "elevation" ) ) { delete poElevBand; delete poDS; return NULL; } poDS->nRasterXSize = poElevBand->nRasterXSize; poDS->nRasterYSize = poElevBand->nRasterYSize; poDS->SetBand( nNextBand++, poElevBand ); /* -------------------------------------------------------------------- */ /* Try to do the same for the uncertainty band. */ /* -------------------------------------------------------------------- */ hid_t hUncertainty = H5Dopen( hHDF5, "/BAG_root/uncertainty" ); BAGRasterBand *poUBand = new BAGRasterBand( poDS, nNextBand ); if( hUncertainty >= 0 && poUBand->Initialize( hUncertainty, "uncertainty") ) { poDS->SetBand( nNextBand++, poUBand ); } else delete poUBand; /* -------------------------------------------------------------------- */ /* Try to do the same for the uncertainty band. */ /* -------------------------------------------------------------------- */ hid_t hNominal = -1; H5E_BEGIN_TRY { hNominal = H5Dopen( hHDF5, "/BAG_root/nominal_elevation" ); } H5E_END_TRY; BAGRasterBand *poNBand = new BAGRasterBand( poDS, nNextBand ); if( hNominal >= 0 && poNBand->Initialize( hNominal, "nominal_elevation" ) ) { poDS->SetBand( nNextBand++, poNBand ); } else delete poNBand; /* -------------------------------------------------------------------- */ /* Load the XML metadata. */ /* -------------------------------------------------------------------- */ poDS->LoadMetadata(); /* -------------------------------------------------------------------- */ /* Setup/check for pam .aux.xml. */ /* -------------------------------------------------------------------- */ poDS->SetDescription( poOpenInfo->pszFilename ); poDS->TryLoadXML(); /* -------------------------------------------------------------------- */ /* Setup overviews. */ /* -------------------------------------------------------------------- */ poDS->oOvManager.Initialize( poDS, poOpenInfo->pszFilename ); return( poDS ); }
GDALDataset *BAGDataset::Open( GDALOpenInfo *poOpenInfo ) { // Confirm that this appears to be a BAG file. if( !Identify(poOpenInfo) ) return nullptr; // Confirm the requested access is supported. if( poOpenInfo->eAccess == GA_Update ) { CPLError(CE_Failure, CPLE_NotSupported, "The BAG driver does not support update access."); return nullptr; } // Open the file as an HDF5 file. hid_t hHDF5 = H5Fopen(poOpenInfo->pszFilename, H5F_ACC_RDONLY, H5P_DEFAULT); if( hHDF5 < 0 ) return nullptr; // Confirm it is a BAG dataset by checking for the // BAG_Root/Bag Version attribute. const hid_t hBagRoot = H5Gopen(hHDF5, "/BAG_root"); const hid_t hVersion = hBagRoot >= 0 ? H5Aopen_name(hBagRoot, "Bag Version") : -1; if( hVersion < 0 ) { if( hBagRoot >= 0 ) H5Gclose(hBagRoot); H5Fclose(hHDF5); return nullptr; } H5Aclose(hVersion); // Create a corresponding dataset. BAGDataset *const poDS = new BAGDataset(); poDS->hHDF5 = hHDF5; // Extract version as metadata. CPLString osVersion; if( GH5_FetchAttribute(hBagRoot, "Bag Version", osVersion) ) poDS->SetMetadataItem("BagVersion", osVersion); H5Gclose(hBagRoot); // Fetch the elevation dataset and attach as a band. int nNextBand = 1; const hid_t hElevation = H5Dopen(hHDF5, "/BAG_root/elevation"); if( hElevation < 0 ) { delete poDS; return nullptr; } BAGRasterBand *poElevBand = new BAGRasterBand(poDS, nNextBand); if( !poElevBand->Initialize(hElevation, "elevation") ) { delete poElevBand; delete poDS; return nullptr; } poDS->nRasterXSize = poElevBand->nRasterXSize; poDS->nRasterYSize = poElevBand->nRasterYSize; poDS->SetBand(nNextBand++, poElevBand); // Try to do the same for the uncertainty band. const hid_t hUncertainty = H5Dopen(hHDF5, "/BAG_root/uncertainty"); BAGRasterBand *poUBand = new BAGRasterBand(poDS, nNextBand); if( hUncertainty >= 0 && poUBand->Initialize(hUncertainty, "uncertainty") ) { poDS->SetBand(nNextBand++, poUBand); } else { delete poUBand; } // Try to do the same for the nominal_elevation band. hid_t hNominal = -1; #ifdef HAVE_GCC_WARNING_ZERO_AS_NULL_POINTER_CONSTANT #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant" #endif H5E_BEGIN_TRY { hNominal = H5Dopen(hHDF5, "/BAG_root/nominal_elevation"); } H5E_END_TRY; #ifdef HAVE_GCC_WARNING_ZERO_AS_NULL_POINTER_CONSTANT #pragma GCC diagnostic pop #endif BAGRasterBand *const poNBand = new BAGRasterBand(poDS, nNextBand); if( hNominal >= 0 && poNBand->Initialize(hNominal, "nominal_elevation") ) { poDS->SetBand(nNextBand++, poNBand); } else { delete poNBand; } // Load the XML metadata. poDS->LoadMetadata(); // Setup/check for pam .aux.xml. poDS->SetDescription(poOpenInfo->pszFilename); poDS->TryLoadXML(); // Setup overviews. poDS->oOvManager.Initialize(poDS, poOpenInfo->pszFilename); return poDS; }