LCOV - code coverage report
Current view: directory - frmts/hdf5 - hdf5imagedataset.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 239 144 60.3 %
Date: 2012-04-28 Functions: 21 10 47.6 %

       1                 : /******************************************************************************
       2                 :  * $Id: hdf5imagedataset.cpp 24082 2012-03-06 00:57:22Z warmerdam $
       3                 :  *
       4                 :  * Project:  Hierarchical Data Format Release 5 (HDF5)
       5                 :  * Purpose:  Read subdatasets of HDF5 file.
       6                 :  * Author:   Denis Nadeau <denis.nadeau@gmail.com>
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2005, Frank Warmerdam <warmerdam@pobox.com>
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      12                 :  * copy of this software and associated documentation files (the "Software"),
      13                 :  * to deal in the Software without restriction, including without limitation
      14                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15                 :  * and/or sell copies of the Software, and to permit persons to whom the
      16                 :  * Software is furnished to do so, subject to the following conditions:
      17                 :  *
      18                 :  * The above copyright notice and this permission notice shall be included
      19                 :  * in all copies or substantial portions of the Software.
      20                 :  *
      21                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27                 :  * DEALINGS IN THE SOFTWARE.
      28                 :  ****************************************************************************/
      29                 : 
      30                 : #define H5_USE_16_API
      31                 : 
      32                 : #include "hdf5.h"
      33                 : 
      34                 : #include "gdal_pam.h"
      35                 : #include "gdal_priv.h"
      36                 : #include "cpl_string.h"
      37                 : #include "hdf5dataset.h"
      38                 : #include "ogr_spatialref.h"
      39                 : 
      40                 : CPL_CVSID("$Id: hdf5imagedataset.cpp 24082 2012-03-06 00:57:22Z warmerdam $");
      41                 : 
      42                 : CPL_C_START
      43                 : void GDALRegister_HDF5Image(void);
      44                 : CPL_C_END
      45                 : 
      46                 : /* release 1.6.3 or 1.6.4 changed the type of count in some api functions */
      47                 : 
      48                 : #if H5_VERS_MAJOR == 1 && H5_VERS_MINOR <= 6 \
      49                 :        && (H5_VERS_MINOR < 6 || H5_VERS_RELEASE < 3)
      50                 : #  define H5OFFSET_TYPE hssize_t
      51                 : #else
      52                 : #  define H5OFFSET_TYPE  hsize_t
      53                 : #endif
      54                 : 
      55                 : class HDF5ImageDataset : public HDF5Dataset
      56                 : {
      57                 : 
      58                 :     friend class HDF5ImageRasterBand;
      59                 : 
      60                 :     char        *pszProjection;
      61                 :     char        *pszGCPProjection;
      62                 :     GDAL_GCP    *pasGCPList;
      63                 :     int         nGCPCount;
      64                 :     OGRSpatialReference oSRS;
      65                 : 
      66                 :     hsize_t      *dims,*maxdims;
      67                 :     HDF5GroupObjects *poH5Objects;
      68                 :     int          ndims,dimensions;
      69                 :     hid_t        dataset_id;
      70                 :     hid_t        dataspace_id;
      71                 :     hsize_t      size;
      72                 :     haddr_t      address;
      73                 :     hid_t        datatype;
      74                 :     hid_t        native;
      75                 :     H5T_class_t  clas;
      76                 : 
      77                 : public:
      78                 :     HDF5ImageDataset();
      79                 :     ~HDF5ImageDataset();
      80                 : 
      81                 :     CPLErr CreateProjections( );
      82                 :     static GDALDataset  *Open( GDALOpenInfo * );
      83                 :     static int           Identify( GDALOpenInfo * );
      84                 : 
      85                 :     const char          *GetProjectionRef();
      86                 :     virtual int         GetGCPCount( );
      87                 :     virtual const char  *GetGCPProjection();
      88                 :     virtual const GDAL_GCP *GetGCPs( );
      89                 : 
      90                 : };
      91                 : 
      92                 : /************************************************************************/
      93                 : /* ==================================================================== */
      94                 : /*                              HDF5ImageDataset                        */
      95                 : /* ==================================================================== */
      96                 : /************************************************************************/
      97                 : 
      98                 : /************************************************************************/
      99                 : /*                           HDF5ImageDataset()                         */
     100                 : /************************************************************************/
     101              20 : HDF5ImageDataset::HDF5ImageDataset()
     102                 : {
     103              20 :     nGCPCount       = 0;
     104              20 :     pszProjection   = NULL;
     105              20 :     pszGCPProjection= NULL;
     106              20 :     pasGCPList      = NULL;
     107              20 :     poH5Objects     = NULL;
     108              20 :     poH5RootGroup   = NULL;
     109              20 :     dims            = NULL;
     110              20 :     maxdims         = NULL;
     111              20 :     papszMetadata   = NULL;
     112              20 : }
     113                 : 
     114                 : /************************************************************************/
     115                 : /*                            ~HDF5ImageDataset()                       */
     116                 : /************************************************************************/
     117              20 : HDF5ImageDataset::~HDF5ImageDataset( )
     118                 : {
     119              20 :     FlushCache();
     120                 : 
     121              20 :     CPLFree(pszProjection);
     122              20 :     CPLFree(pszGCPProjection);
     123                 : 
     124              20 :     if( dims )
     125              20 :         CPLFree( dims );
     126                 : 
     127              20 :     if( maxdims )
     128              20 :         CPLFree( maxdims );
     129                 : 
     130              20 :     if( nGCPCount > 0 )
     131                 :     {
     132               0 :         for( int i = 0; i < nGCPCount; i++ )
     133                 :         {
     134               0 :             if( pasGCPList[i].pszId )
     135               0 :                 CPLFree( pasGCPList[i].pszId );
     136               0 :             if( pasGCPList[i].pszInfo )
     137               0 :                 CPLFree( pasGCPList[i].pszInfo );
     138                 :         }
     139                 : 
     140               0 :         CPLFree( pasGCPList );
     141                 :     }
     142              20 : }
     143                 : 
     144                 : /************************************************************************/
     145                 : /* ==================================================================== */
     146                 : /*                            Hdf5imagerasterband                       */
     147                 : /* ==================================================================== */
     148                 : /************************************************************************/
     149                 : class HDF5ImageRasterBand : public GDALPamRasterBand
     150                 : {
     151                 :     friend class HDF5ImageDataset;
     152                 : 
     153                 :     int         bNoDataSet;
     154                 :     double      dfNoDataValue;
     155                 :     char        *pszFilename;
     156                 : 
     157                 : public:
     158                 : 
     159                 :     HDF5ImageRasterBand( HDF5ImageDataset *, int, GDALDataType );
     160                 :     ~HDF5ImageRasterBand();
     161                 : 
     162                 :     virtual CPLErr      IReadBlock( int, int, void * );
     163                 :     virtual double      GetNoDataValue( int * );
     164                 :     virtual CPLErr      SetNoDataValue( double );
     165                 :     /*  virtual CPLErr          IWriteBlock( int, int, void * ); */
     166                 : };
     167                 : 
     168                 : /************************************************************************/
     169                 : /*                        ~HDF5ImageRasterBand()                        */
     170                 : /************************************************************************/
     171                 : 
     172              20 : HDF5ImageRasterBand::~HDF5ImageRasterBand()
     173                 : {
     174                 : 
     175              20 : }
     176                 : /************************************************************************/
     177                 : /*                           HDF5ImageRasterBand()                      */
     178                 : /************************************************************************/
     179              20 : HDF5ImageRasterBand::HDF5ImageRasterBand( HDF5ImageDataset *poDS, int nBand,
     180              20 :                                           GDALDataType eType )
     181                 : 
     182                 : {
     183                 :     char          **papszMetaGlobal;
     184              20 :     this->poDS    = poDS;
     185              20 :     this->nBand   = nBand;
     186              20 :     eDataType     = eType;
     187              20 :     bNoDataSet    = FALSE;
     188              20 :     dfNoDataValue = -9999;
     189              20 :     nBlockXSize   = poDS->GetRasterXSize( );
     190              20 :     nBlockYSize   = 1;
     191                 : 
     192                 : /* -------------------------------------------------------------------- */
     193                 : /*      Take a copy of Global Metadata since  I can't pass Raster       */
     194                 : /*      variable to Iterate function.                                   */
     195                 : /* -------------------------------------------------------------------- */
     196              20 :     papszMetaGlobal = CSLDuplicate( poDS->papszMetadata );
     197              20 :     CSLDestroy( poDS->papszMetadata );
     198              20 :     poDS->papszMetadata = NULL;
     199                 : 
     200              20 :     if( poDS->poH5Objects->nType == H5G_DATASET ) {
     201              20 :         poDS->CreateMetadata( poDS->poH5Objects, H5G_DATASET );
     202                 :     }
     203                 : 
     204                 : /* -------------------------------------------------------------------- */
     205                 : /*      Recover Global Metadat and set Band Metadata                    */
     206                 : /* -------------------------------------------------------------------- */
     207                 : 
     208              20 :     SetMetadata( poDS->papszMetadata );
     209                 : 
     210              20 :     CSLDestroy( poDS->papszMetadata );
     211              20 :     poDS->papszMetadata = CSLDuplicate( papszMetaGlobal );
     212              20 :     CSLDestroy( papszMetaGlobal );
     213                 : 
     214                 :     /* check for chunksize and set it as the blocksize (optimizes read) */
     215              20 :     hid_t listid = H5Dget_create_plist(((HDF5ImageDataset * )poDS)->dataset_id);
     216              20 :     if (listid>0)
     217                 :     {
     218              20 :         if(H5Pget_layout(listid) == H5D_CHUNKED)
     219                 :         {
     220                 :             hsize_t panChunkDims[3];
     221               0 :             int nDimSize = H5Pget_chunk(listid, 3, panChunkDims);
     222               0 :             nBlockXSize   = (int) panChunkDims[nDimSize-1];
     223               0 :             nBlockYSize   = (int) panChunkDims[nDimSize-2];
     224                 :         }
     225              20 :         H5Pclose(listid);
     226                 :     }
     227                 : 
     228              20 : }
     229                 : 
     230                 : /************************************************************************/
     231                 : /*                           GetNoDataValue()                           */
     232                 : /************************************************************************/
     233               4 : double HDF5ImageRasterBand::GetNoDataValue( int * pbSuccess )
     234                 : 
     235                 : {
     236               4 :     if( bNoDataSet )
     237                 :     {
     238               0 :         if( pbSuccess )
     239               0 :             *pbSuccess = bNoDataSet;
     240                 : 
     241               0 :         return dfNoDataValue;
     242                 :     }
     243                 :     else
     244               4 :         return GDALPamRasterBand::GetNoDataValue( pbSuccess );
     245                 : }
     246                 : 
     247                 : /************************************************************************/
     248                 : /*                           SetNoDataValue()                           */
     249                 : /************************************************************************/
     250               0 : CPLErr HDF5ImageRasterBand::SetNoDataValue( double dfNoData )
     251                 : 
     252                 : {
     253               0 :     bNoDataSet = TRUE;
     254               0 :     dfNoDataValue = dfNoData;
     255                 : 
     256               0 :     return CE_None;
     257                 : }
     258                 : 
     259                 : /************************************************************************/
     260                 : /*                             IReadBlock()                             */
     261                 : /************************************************************************/
     262            1838 : CPLErr HDF5ImageRasterBand::IReadBlock( int nBlockXOff, int nBlockYOff,
     263                 :                                         void * pImage )
     264                 : {
     265                 :     herr_t      status;
     266                 :     hsize_t     count[3];
     267                 :     H5OFFSET_TYPE offset[3];
     268                 :     int         nSizeOfData;
     269                 :     hid_t       memspace;
     270                 :     hsize_t     col_dims[3];
     271                 :     hsize_t     rank;
     272                 : 
     273            1838 :     HDF5ImageDataset    *poGDS = ( HDF5ImageDataset * ) poDS;
     274                 : 
     275            1838 :     if( poGDS->eAccess == GA_Update ) {
     276                 :         memset( pImage, 0,
     277                 :             nBlockXSize * nBlockYSize *
     278               0 :             GDALGetDataTypeSize( eDataType )/8 );
     279               0 :         return CE_None;
     280                 :     }
     281                 : 
     282            1838 :     rank=2;
     283                 : 
     284            1838 :     if( poGDS->ndims == 3 ){
     285               0 :         rank=3;
     286               0 :         offset[0]   = nBand-1;
     287               0 :         count[0]    = 1;
     288               0 :         col_dims[0] = 1;
     289                 :     }
     290                 : 
     291            1838 :     offset[poGDS->ndims - 2] = nBlockYOff*nBlockYSize;
     292            1838 :     offset[poGDS->ndims - 1] = nBlockXOff*nBlockXSize;
     293            1838 :     count[poGDS->ndims - 2]  = nBlockYSize;
     294            1838 :     count[poGDS->ndims - 1]  = nBlockXSize;
     295                 : 
     296            1838 :     nSizeOfData = H5Tget_size( poGDS->native );
     297            1838 :     memset( pImage,0,nBlockXSize*nBlockYSize*nSizeOfData );
     298                 : 
     299                 : /*  blocksize may not be a multiple of imagesize */
     300            3690 :     count[poGDS->ndims - 2]  = MIN( size_t(nBlockYSize),
     301                 :                                     poDS->GetRasterYSize() -
     302            3690 :                                             offset[poGDS->ndims - 2]);
     303            5514 :     count[poGDS->ndims - 1]  = MIN( size_t(nBlockXSize),
     304                 :                                     poDS->GetRasterXSize()-
     305            5514 :                                             offset[poGDS->ndims - 1]);
     306                 : 
     307                 : /* -------------------------------------------------------------------- */
     308                 : /*      Select block from file space                                    */
     309                 : /* -------------------------------------------------------------------- */
     310                 :     status =  H5Sselect_hyperslab( poGDS->dataspace_id,
     311                 :                                    H5S_SELECT_SET,
     312                 :                                    offset, NULL,
     313            1838 :                                    count, NULL );
     314                 : 
     315                 : /* -------------------------------------------------------------------- */
     316                 : /*      Create memory space to receive the data                         */
     317                 : /* -------------------------------------------------------------------- */
     318            1838 :     col_dims[poGDS->ndims-2]=nBlockYSize;
     319            1838 :     col_dims[poGDS->ndims-1]=nBlockXSize;
     320            1838 :     memspace = H5Screate_simple( (int) rank, col_dims, NULL );
     321            1838 :     H5OFFSET_TYPE mem_offset[3] = {0, 0, 0};
     322                 :     status =  H5Sselect_hyperslab(memspace,
     323                 :                                   H5S_SELECT_SET,
     324                 :                                   mem_offset, NULL,
     325            1838 :                                   count, NULL);
     326                 : 
     327                 :     status = H5Dread ( poGDS->dataset_id,
     328                 :                        poGDS->native,
     329                 :                        memspace,
     330                 :                        poGDS->dataspace_id,
     331                 :                        H5P_DEFAULT,
     332            1838 :                        pImage );
     333                 : 
     334            1838 :     H5Sclose( memspace );
     335                 : 
     336            1838 :     if( status < 0 )
     337                 :     {
     338                 :         CPLError( CE_Failure, CPLE_AppDefined,
     339               0 :                   "H5Dread() failed for block." );
     340               0 :         return CE_Failure;
     341                 :     }
     342                 :     else
     343            1838 :         return CE_None;
     344                 : }
     345                 : 
     346                 : /************************************************************************/
     347                 : /*                              Identify()                              */
     348                 : /************************************************************************/
     349                 : 
     350           18908 : int HDF5ImageDataset::Identify( GDALOpenInfo *poOpenInfo )
     351                 : 
     352                 : {
     353           18908 :     if(!EQUALN( poOpenInfo->pszFilename, "HDF5:", 5 ) )
     354           18908 :         return FALSE;
     355                 :     else
     356               0 :         return TRUE;
     357                 : }
     358                 : 
     359                 : /************************************************************************/
     360                 : /*                                Open()                                */
     361                 : /************************************************************************/
     362            3352 : GDALDataset *HDF5ImageDataset::Open( GDALOpenInfo * poOpenInfo )
     363                 : {
     364                 :     int i;
     365                 :     HDF5ImageDataset    *poDS;
     366                 :     char szFilename[2048];
     367                 : 
     368            3352 :     if(!EQUALN( poOpenInfo->pszFilename, "HDF5:", 5 ) ||
     369                 :         strlen(poOpenInfo->pszFilename) > sizeof(szFilename) - 3 )
     370            3332 :         return NULL;
     371                 : 
     372                 : /* -------------------------------------------------------------------- */
     373                 : /*      Confirm the requested access is supported.                      */
     374                 : /* -------------------------------------------------------------------- */
     375              20 :     if( poOpenInfo->eAccess == GA_Update )
     376                 :     {
     377                 :         CPLError( CE_Failure, CPLE_NotSupported,
     378                 :                   "The HDF5ImageDataset driver does not support update access to existing"
     379               0 :                   " datasets.\n" );
     380               0 :         return NULL;
     381                 :     }
     382                 : 
     383              20 :     poDS = new HDF5ImageDataset();
     384                 : 
     385                 :     /* -------------------------------------------------------------------- */
     386                 :     /*      Create a corresponding GDALDataset.                             */
     387                 :     /* -------------------------------------------------------------------- */
     388                 :     /* printf("poOpenInfo->pszFilename %s\n",poOpenInfo->pszFilename); */
     389                 :     char **papszName =
     390                 :         CSLTokenizeString2(  poOpenInfo->pszFilename,
     391              20 :                              ":", CSLT_HONOURSTRINGS|CSLT_PRESERVEESCAPES );
     392                 : 
     393              20 :     if( !((CSLCount(papszName) == 3) || (CSLCount(papszName) == 4)) )
     394                 :     {
     395               0 :         CSLDestroy(papszName);
     396               0 :         delete poDS;
     397               0 :         return NULL;
     398                 :     }
     399                 : 
     400              20 :     poDS->SetDescription( poOpenInfo->pszFilename );
     401                 : 
     402                 :     /* -------------------------------------------------------------------- */
     403                 :     /*    Check for drive name in windows HDF5:"D:\...                      */
     404                 :     /* -------------------------------------------------------------------- */
     405              20 :     strcpy(szFilename, papszName[1]);
     406                 : 
     407              20 :     if( strlen(papszName[1]) == 1 && papszName[3] != NULL )
     408                 :     {
     409               0 :         strcat(szFilename, ":");
     410               0 :         strcat(szFilename, papszName[2]);
     411                 : 
     412               0 :         poDS->SetSubdatasetName( papszName[3] );
     413                 :     }
     414                 :     else
     415              20 :         poDS->SetSubdatasetName( papszName[2] );
     416                 : 
     417              20 :     CSLDestroy(papszName);
     418              20 :     papszName = NULL;
     419                 : 
     420              20 :     if( !H5Fis_hdf5(szFilename) ) {
     421               0 :         delete poDS;
     422               0 :         return NULL;
     423                 :     }
     424                 : 
     425              20 :     poDS->SetPhysicalFilename( szFilename );
     426                 : 
     427                 :     /* -------------------------------------------------------------------- */
     428                 :     /*      Try opening the dataset.                                        */
     429                 :     /* -------------------------------------------------------------------- */
     430                 :     poDS->hHDF5 = H5Fopen(szFilename,
     431                 :                           H5F_ACC_RDONLY,
     432              20 :                           H5P_DEFAULT );
     433                 : 
     434              20 :     if( poDS->hHDF5 < 0 )
     435                 :     {
     436               0 :         delete poDS;
     437               0 :         return NULL;
     438                 :     }
     439                 : 
     440              20 :     poDS->hGroupID = H5Gopen( poDS->hHDF5, "/" );
     441              20 :     if( poDS->hGroupID < 0 )
     442                 :     {
     443               0 :         poDS->bIsHDFEOS=false;
     444               0 :         delete poDS;
     445               0 :         return NULL;
     446                 :     }
     447                 : 
     448                 : /* -------------------------------------------------------------------- */
     449                 : /*      THIS IS AN HDF5 FILE                                            */
     450                 : /* -------------------------------------------------------------------- */
     451              20 :     poDS->bIsHDFEOS=TRUE;
     452              20 :     poDS->ReadGlobalAttributes( FALSE );
     453                 : 
     454                 : /* -------------------------------------------------------------------- */
     455                 : /*      Create HDF5 Data Hierarchy in a link list                       */
     456                 : /* -------------------------------------------------------------------- */
     457                 :     poDS->poH5Objects =
     458                 :         poDS->HDF5FindDatasetObjectsbyPath( poDS->poH5RootGroup,
     459              20 :                                             (char *)poDS->GetSubdatasetName() );
     460                 : 
     461              20 :     if( poDS->poH5Objects == NULL ) {
     462               0 :         delete poDS;
     463               0 :         return NULL;
     464                 :     }
     465                 : /* -------------------------------------------------------------------- */
     466                 : /*      Retrieve HDF5 data information                                  */
     467                 : /* -------------------------------------------------------------------- */
     468              20 :     poDS->dataset_id   = H5Dopen( poDS->hHDF5,poDS->poH5Objects->pszPath );
     469              20 :     poDS->dataspace_id = H5Dget_space( poDS->dataset_id );
     470              20 :     poDS->ndims        = H5Sget_simple_extent_ndims( poDS->dataspace_id );
     471              20 :     poDS->dims         = (hsize_t*)CPLCalloc( poDS->ndims, sizeof(hsize_t) );
     472              20 :     poDS->maxdims      = (hsize_t*)CPLCalloc( poDS->ndims, sizeof(hsize_t) );
     473                 :     poDS->dimensions   = H5Sget_simple_extent_dims( poDS->dataspace_id,
     474                 :                                                     poDS->dims,
     475              20 :                                                     poDS->maxdims );
     476              20 :     poDS->datatype = H5Dget_type( poDS->dataset_id );
     477              20 :     poDS->clas     = H5Tget_class( poDS->datatype );
     478              20 :     poDS->size     = H5Tget_size( poDS->datatype );
     479              20 :     poDS->address = H5Dget_offset( poDS->dataset_id );
     480              20 :     poDS->native  = H5Tget_native_type( poDS->datatype, H5T_DIR_ASCEND );
     481                 : 
     482              20 :     poDS->nRasterYSize=(int)poDS->dims[poDS->ndims-2];   // Y
     483              20 :     poDS->nRasterXSize=(int)poDS->dims[poDS->ndims-1];   // X alway last
     484                 : 
     485              20 :     poDS->nBands=1;
     486                 : 
     487              20 :     if( poDS->ndims == 3 ) poDS->nBands=(int) poDS->dims[0];
     488                 : 
     489                 : 
     490              40 :     for(  i = 1; i <= poDS->nBands; i++ ) {
     491                 :         HDF5ImageRasterBand *poBand =
     492                 :             new HDF5ImageRasterBand( poDS, i,
     493              20 :                             poDS->GetDataType( poDS->native ) );
     494                 : 
     495              20 :         poDS->SetBand( i, poBand );
     496              20 :         if( poBand->bNoDataSet )
     497               0 :             poBand->SetNoDataValue( 255 );
     498                 :     }
     499                 : 
     500              20 :     poDS->CreateProjections( );
     501                 : 
     502              20 :     poDS->SetMetadata( poDS->papszMetadata );
     503                 : 
     504                 : /* -------------------------------------------------------------------- */
     505                 : /*      Setup/check for pam .aux.xml.                                   */
     506                 : /* -------------------------------------------------------------------- */
     507              20 :     poDS->TryLoadXML();
     508                 : 
     509                 : /* -------------------------------------------------------------------- */
     510                 : /*      Setup overviews.                                                */
     511                 : /* -------------------------------------------------------------------- */
     512              20 :     poDS->oOvManager.Initialize( poDS, ":::VIRTUAL:::" );
     513                 : 
     514              20 :     return( poDS );
     515                 : }
     516                 : 
     517                 : 
     518                 : /************************************************************************/
     519                 : /*                        GDALRegister_HDF5Image()                      */
     520                 : /************************************************************************/
     521            1135 : void GDALRegister_HDF5Image( )
     522                 : 
     523                 : {
     524                 :     GDALDriver  *poDriver;
     525                 : 
     526            1135 :     if (! GDAL_CHECK_VERSION("HDF5Image driver"))
     527               0 :         return;
     528                 : 
     529            1135 :     if(  GDALGetDriverByName( "HDF5Image" ) == NULL )
     530                 :     {
     531            1093 :         poDriver = new GDALDriver( );
     532                 : 
     533            1093 :         poDriver->SetDescription( "HDF5Image" );
     534                 :         poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
     535            1093 :                                    "HDF5 Dataset" );
     536                 :         poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
     537            1093 :                                    "frmt_hdf5.html" );
     538            1093 :         poDriver->pfnOpen = HDF5ImageDataset::Open;
     539            1093 :         poDriver->pfnIdentify = HDF5ImageDataset::Identify;
     540                 : 
     541            1093 :         GetGDALDriverManager( )->RegisterDriver( poDriver );
     542                 :     }
     543                 : }
     544                 : 
     545                 : /************************************************************************/
     546                 : /*                         CreateProjections()                          */
     547                 : /************************************************************************/
     548              20 : CPLErr HDF5ImageDataset::CreateProjections()
     549                 : {
     550                 : #define NBGCPLAT 100
     551                 : #define NBGCPLON 30
     552                 : 
     553              20 :     hid_t LatitudeDatasetID  = -1;
     554              20 :     hid_t LongitudeDatasetID = -1;
     555                 :     hid_t LatitudeDataspaceID;
     556                 :     hid_t LongitudeDataspaceID;
     557                 :     float* Latitude;
     558                 :     float* Longitude;
     559                 :     int    i,j;
     560                 :     int   nDeltaLat;
     561                 :     int   nDeltaLon;
     562                 : 
     563              20 :     nDeltaLat = nRasterYSize / NBGCPLAT;
     564              20 :     nDeltaLon = nRasterXSize / NBGCPLON;
     565                 : 
     566              20 :     if( nDeltaLat == 0 || nDeltaLon == 0 )
     567              16 :         return CE_None;
     568                 : 
     569                 : /* -------------------------------------------------------------------- */
     570                 : /*      Create HDF5 Data Hierarchy in a link list                       */
     571                 : /* -------------------------------------------------------------------- */
     572               4 :     poH5Objects=HDF5FindDatasetObjects( poH5RootGroup,  "Latitude" );
     573               4 :     if( !poH5Objects ) {
     574               4 :         return CE_None;
     575                 :     }
     576                 : /* -------------------------------------------------------------------- */
     577                 : /*      The Lattitude and Longitude arrays must have a rank of 2 to     */
     578                 : /*      retrieve GCPs.                                                  */
     579                 : /* -------------------------------------------------------------------- */
     580               0 :     if( poH5Objects->nRank != 2 ) {
     581               0 :         return CE_None;
     582                 :     }
     583                 : 
     584                 : /* -------------------------------------------------------------------- */
     585                 : /*      Retrieve HDF5 data information                                  */
     586                 : /* -------------------------------------------------------------------- */
     587               0 :     LatitudeDatasetID   = H5Dopen( hHDF5,poH5Objects->pszPath );
     588               0 :     LatitudeDataspaceID = H5Dget_space( dataset_id );
     589                 : 
     590               0 :     poH5Objects=HDF5FindDatasetObjects( poH5RootGroup, "Longitude" );
     591               0 :     LongitudeDatasetID   = H5Dopen( hHDF5,poH5Objects->pszPath );
     592               0 :     LongitudeDataspaceID = H5Dget_space( dataset_id );
     593                 : 
     594               0 :     if( ( LatitudeDatasetID > 0 ) && ( LongitudeDatasetID > 0) ) {
     595                 : 
     596                 :         Latitude         = ( float * ) CPLCalloc(  nRasterYSize*nRasterXSize,
     597               0 :                             sizeof( float ) );
     598                 :         Longitude         = ( float * ) CPLCalloc( nRasterYSize*nRasterXSize,
     599               0 :                             sizeof( float ) );
     600               0 :         memset( Latitude, 0, nRasterXSize*nRasterYSize*sizeof(  float ) );
     601               0 :         memset( Longitude, 0, nRasterXSize*nRasterYSize*sizeof( float ) );
     602                 : 
     603                 :         H5Dread ( LatitudeDatasetID,
     604                 :             H5T_NATIVE_FLOAT,
     605                 :             H5S_ALL,
     606                 :             H5S_ALL,
     607                 :             H5P_DEFAULT,
     608               0 :             Latitude );
     609                 : 
     610                 :         H5Dread ( LongitudeDatasetID,
     611                 :             H5T_NATIVE_FLOAT,
     612                 :             H5S_ALL,
     613                 :             H5S_ALL,
     614                 :             H5P_DEFAULT,
     615               0 :             Longitude );
     616                 : 
     617               0 :         oSRS.SetWellKnownGeogCS( "WGS84" );
     618               0 :         CPLFree(pszProjection);
     619               0 :         CPLFree(pszGCPProjection);
     620               0 :         oSRS.exportToWkt( &pszProjection );
     621               0 :         oSRS.exportToWkt( &pszGCPProjection );
     622                 : 
     623                 :     /* -------------------------------------------------------------------- */
     624                 :     /*  Fill the GCPs list.                                                 */
     625                 :     /* -------------------------------------------------------------------- */
     626               0 :         nGCPCount = nRasterYSize/nDeltaLat * nRasterXSize/nDeltaLon;
     627                 : 
     628                 :         pasGCPList = ( GDAL_GCP * )
     629               0 :             CPLCalloc( nGCPCount, sizeof( GDAL_GCP ) );
     630                 : 
     631               0 :         GDALInitGCPs( nGCPCount, pasGCPList );
     632               0 :         int k=0;
     633                 : 
     634               0 :         int nYLimit = ((int)nRasterYSize/nDeltaLat) * nDeltaLat;
     635               0 :         int nXLimit = ((int)nRasterXSize/nDeltaLon) * nDeltaLon;
     636               0 :         for( j = 0; j < nYLimit; j+=nDeltaLat )
     637                 :         {
     638               0 :             for( i = 0; i < nXLimit; i+=nDeltaLon )
     639                 :             {
     640               0 :                 int iGCP =  j * nRasterXSize + i;
     641               0 :                 pasGCPList[k].dfGCPX = ( double ) Longitude[iGCP]+180.0;
     642               0 :                 pasGCPList[k].dfGCPY = ( double ) Latitude[iGCP];
     643                 : 
     644               0 :                 pasGCPList[k].dfGCPPixel = i + 0.5;
     645               0 :                 pasGCPList[k++].dfGCPLine =  j + 0.5;
     646                 : 
     647                 :             }
     648                 :         }
     649                 : 
     650               0 :         CPLFree( Latitude );
     651               0 :         CPLFree( Longitude );
     652                 :     }
     653               0 :     return CE_None;
     654                 : 
     655                 : }
     656                 : /************************************************************************/
     657                 : /*                          GetProjectionRef()                          */
     658                 : /************************************************************************/
     659                 : 
     660               0 : const char *HDF5ImageDataset::GetProjectionRef( )
     661                 : 
     662                 : {
     663               0 :     if( pszProjection )
     664               0 :         return pszProjection;
     665                 :     else
     666               0 :         return GDALPamDataset::GetProjectionRef();
     667                 : }
     668                 : 
     669                 : /************************************************************************/
     670                 : /*                            GetGCPCount()                             */
     671                 : /************************************************************************/
     672                 : 
     673               0 : int HDF5ImageDataset::GetGCPCount( )
     674                 : 
     675                 : {
     676               0 :     if( nGCPCount > 0 )
     677               0 :         return nGCPCount;
     678                 :     else
     679               0 :         return GDALPamDataset::GetGCPCount();
     680                 : }
     681                 : 
     682                 : /************************************************************************/
     683                 : /*                          GetGCPProjection()                          */
     684                 : /************************************************************************/
     685                 : 
     686               0 : const char *HDF5ImageDataset::GetGCPProjection( )
     687                 : 
     688                 : {
     689               0 :     if( nGCPCount > 0 )
     690               0 :         return pszGCPProjection;
     691                 :     else
     692               0 :         return GDALPamDataset::GetGCPProjection();
     693                 : }
     694                 : 
     695                 : /************************************************************************/
     696                 : /*                               GetGCPs()                              */
     697                 : /************************************************************************/
     698                 : 
     699               0 : const GDAL_GCP *HDF5ImageDataset::GetGCPs( )
     700                 : {
     701               0 :     if( nGCPCount > 0 )
     702               0 :         return pasGCPList;
     703                 :     else
     704               0 :         return GDALPamDataset::GetGCPs();
     705                 : }

Generated by: LCOV version 1.7