MDAL
mdal_gdal.hpp
1 /*
2  MDAL - Mesh Data Abstraction Library (MIT License)
3  Copyright (C) 2018 Peter Petrik (zilolv at gmail dot com)
4 */
5 
6 #ifndef MDAL_GDAL_HPP
7 #define MDAL_GDAL_HPP
8 
9 #include "gdal.h"
10 
11 #include <string>
12 #include <vector>
13 #include <map>
14 
15 #include "mdal_data_model.hpp"
16 #include "mdal.h"
17 #include "mdal_utils.hpp"
18 #include "mdal_driver.hpp"
19 
20 namespace MDAL
21 {
22 
24  {
25  public:
26 
27  GdalDataset(): mHDataset( nullptr ) {}
28  ~GdalDataset()
29  {
30  if ( mHDataset ) GDALClose( mHDataset );
31  }
32 
33  void init( const std::string &dsName );
34 
35  std::string mDatasetName;
36  std::string mProj;
37  GDALDatasetH mHDataset;
38 
39  unsigned int mNBands; /* number of bands */
40  unsigned int mXSize; /* number of x pixels */
41  unsigned int mYSize; /* number of y pixels */
42  unsigned int mNPoints; /* nodes count */
43  unsigned int mNVolumes; /* Faces count */
44  double mGT[6]; /* affine transform matrix */
45 
46  private:
47  void parseParameters();
48  void parseProj();
49  };
50 
51  class DriverGdal: public Driver
52  {
53  public:
54  DriverGdal( const std::string &name,
55  const std::string &description,
56  const std::string &filter,
57  const std::string &gdalDriverName );
58 
59  virtual ~DriverGdal() override = default;
60  bool canReadMesh( const std::string &uri ) override;
61  std::unique_ptr< Mesh > load( const std::string &fileName, const std::string &meshName = "" ) override;
62 
63  protected:
64  typedef std::map<std::string, std::string> metadata_hash; // KEY, VALUE
65 
66  /* return true on failure */
67  virtual bool parseBandInfo( const GdalDataset *cfGDALDataset,
68  const metadata_hash &metadata, std::string &band_name, MDAL::RelativeTimestamp *time, bool *is_vector, bool *is_x ) = 0;
69  virtual double parseMetadataTime( const std::string &time_s );
70  virtual std::string GDALFileName( const std::string &fileName ); /* some formats require e.g. adding driver name at the beginning */
71  virtual std::vector<std::string> parseDatasetNames( const std::string &fileName );
72  virtual void parseGlobals( const metadata_hash &metadata ) {MDAL_UNUSED( metadata );}
73  virtual void parseBandIsVector( std::string &band_name, bool *is_vector, bool *is_x );
74 
75  private:
76  typedef std::map<MDAL::RelativeTimestamp, std::vector<GDALRasterBandH> > timestep_map; //TIME (sorted), [X, Y]
77  typedef std::map<std::string, timestep_map > data_hash; //Data Type, TIME (sorted), [X, Y]
78  typedef std::vector<std::shared_ptr<GdalDataset>> gdal_datasets_vector; //GDAL (Sub)Datasets,
79 
80  void registerDriver();
81 
82  void initFaces( Vertices &nodes, Faces &Faces, bool is_longitude_shifted );
83  bool initVertices( Vertices &vertices ); //returns is_longitude_shifted
84 
85  const GdalDataset *meshGDALDataset();
86 
87  bool meshes_equals( const GdalDataset *ds1, const GdalDataset *ds2 ) const;
88 
89  metadata_hash parseMetadata( GDALMajorObjectH gdalBand, const char *pszDomain = nullptr );
90  void addDataToOutput( GDALRasterBandH raster_band, std::shared_ptr<MemoryDataset2D> tos, bool is_vector, bool is_x );
91  bool addSrcProj();
92  void addDatasetGroups();
93  void createMesh();
94  void parseRasterBands( const GdalDataset *cfGDALDataset );
95  void fixRasterBands();
96 
97  virtual MDAL::DateTime referenceTime() const;
98 
99  std::string mFileName;
100  const std::string mGdalDriverName; /* GDAL driver name */
101  double *mPafScanline; /* temporary buffer for reading one raster line */
102  std::unique_ptr< MemoryMesh > mMesh;
103  gdal_datasets_vector gdal_datasets;
104  data_hash mBands; /* raster bands GDAL handle */
105  };
106 
107 } // namespace MDAL
108 #endif // MDAL_GDAL_HPP
Definition: mdal_datetime.hpp:48
Definition: mdal_gdal.hpp:52
Definition: mdal_driver.hpp:28
Definition: mdal_gdal.hpp:24
Definition: mdal_datetime.hpp:18