vsgXchange  1.0.2
VulkanSceneGraph 3rd party data integration library
gdal.h
1 #pragma once
2 
3 /* <editor-fold desc="MIT License">
4 
5 Copyright(c) 2021 Robert Osfield
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy of
8 this software and associated documentation files (the "Software"), to deal in
9 the Software without restriction, including without limitation the rights to
10 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 the Software, and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
13 
14 The above copyright notice and this permission notice shimages be included in images
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 </editor-fold> */
25 
26 #include <vsg/io/ReaderWriter.h>
27 #include <vsgXchange/Version.h>
28 
29 #include <istream>
30 #include <memory>
31 #include <unordered_set>
32 
33 namespace vsgXchange
34 {
36  class VSGXCHANGE_DECLSPEC GDAL : public vsg::Inherit<vsg::ReaderWriter, GDAL>
37  {
38  public:
39  GDAL();
40  vsg::ref_ptr<vsg::Object> read(const vsg::Path& filename, vsg::ref_ptr<const vsg::Options> options) const override;
41 
42  bool getFeatures(Features& features) const override;
43 
44  protected:
45  ~GDAL();
46 
47  class Implementation;
48  Implementation* _implementation;
49  };
50 
51 } // namespace vsgXchange
52 
53 EVSG_type_name(vsgXchange::GDAL);
54 
55 #ifdef vsgXchange_GDAL
56 
57 # include "gdal_priv.h"
58 # include "ogr_spatialref.h"
59 
60 # include <vsg/core/Data.h>
61 # include <vsg/io/Path.h>
62 # include <vsg/maths/vec4.h>
63 
64 # include <memory>
65 # include <set>
66 
67 namespace vsgXchange
68 {
70  extern VSGXCHANGE_DECLSPEC bool initGDAL();
71 
73  inline std::shared_ptr<GDALDataset> openDataSet(const vsg::Path& filename, GDALAccess access)
74  {
75  // GDAL doesn't support wide string filenames so convert vsg::Path to std::string and then pass to GDALOpen
76  return std::shared_ptr<GDALDataset>(static_cast<GDALDataset*>(GDALOpen(filename.string().c_str(), access)), [](GDALDataset* dataset) { GDALClose(dataset); });
77  }
78 
80  inline std::shared_ptr<GDALDataset> openSharedDataSet(const vsg::Path& filename, GDALAccess access)
81  {
82  // GDAL doesn't support wide string filenames so convert vsg::Path to std::string and then pass to GDALOpenShared
83  return std::shared_ptr<GDALDataset>(static_cast<GDALDataset*>(GDALOpenShared(filename.string().c_str(), access)), [](GDALDataset* dataset) { GDALClose(dataset); });
84  }
85 
87  extern VSGXCHANGE_DECLSPEC bool compatibleDatasetProjections(const GDALDataset& lhs, const GDALDataset& rhs);
88 
90  extern VSGXCHANGE_DECLSPEC bool compatibleDatasetProjectionsTransformAndSizes(const GDALDataset& lhs, const GDALDataset& rhs);
91 
93  extern VSGXCHANGE_DECLSPEC vsg::ref_ptr<vsg::Data> createImage2D(int width, int height, int numComponents, GDALDataType dataType, vsg::dvec4 def = {0.0, 0.0, 0.0, 1.0});
94 
96  extern VSGXCHANGE_DECLSPEC bool copyRasterBandToImage(GDALRasterBand& band, vsg::Data& image, int component);
97 
99  extern VSGXCHANGE_DECLSPEC bool assignMetaData(GDALDataset& dataset, vsg::Object& object);
100 
102  template<class Iterator, class BinaryPredicate>
103  bool all_equal(Iterator first, Iterator last, BinaryPredicate compare)
104  {
105  if (first == last) return true;
106  Iterator itr = first;
107  ++itr;
108 
109  for (; itr != last; ++itr)
110  {
111  if (!compare(**first, **itr)) return false;
112  }
113 
114  return true;
115  }
116 
118  inline std::set<GDALDataType> dataTypes(GDALDataset& dataset)
119  {
120  std::set<GDALDataType> types;
121  for (int i = 1; i <= dataset.GetRasterCount(); ++i)
122  {
123  GDALRasterBand* band = dataset.GetRasterBand(i);
124  types.insert(band->GetRasterDataType());
125  }
126 
127  return types;
128  }
129 
131  template<class Iterator>
132  std::set<GDALDataType> dataTypes(Iterator first, Iterator last)
133  {
134  std::set<GDALDataType> types;
135  for (Iterator itr = first; itr != last; ++itr)
136  {
137  GDALDataset& dataset = **itr;
138  for (int i = 1; i <= dataset.GetRasterCount(); ++i)
139  {
140  GDALRasterBand* band = dataset.GetRasterBand(i);
141  types.insert(band->GetRasterDataType());
142  }
143  }
144  return types;
145  }
146 
148  extern VSGXCHANGE_DECLSPEC bool getEXIF_LatitudeLongitudeAlititude(GDALDataset& dataset, double& latitude, double& longitude, double& altitude);
149 
151  extern VSGXCHANGE_DECLSPEC bool getEXIF_LatitudeLongitudeAlititude(const vsg::Object& object, double& latitude, double& longitude, double& altitude);
152 
153  template<typename T>
154  struct in_brackets
155  {
156  in_brackets(T& v) :
157  value(v) {}
158  T& value;
159  };
160 
161  template<typename T>
162  std::istream& operator>>(std::istream& input, in_brackets<T> field)
163  {
164  while (input.peek() == ' ') input.get();
165 
166  std::string str;
167  if (input.peek() == '(')
168  {
169  input.ignore();
170 
171  input >> field.value;
172 
173  if constexpr (std::is_same_v<T, std::string>)
174  {
175  if (!field.value.empty() && field.value[field.value.size() - 1] == ')')
176  {
177  field.value.erase(field.value.size() - 1);
178  return input;
179  }
180  else
181  {
182  while (input.peek() != ')')
183  {
184  int c = input.get();
185  if (input.eof()) return input;
186 
187  field.value.push_back(c);
188  }
189  }
190  }
191 
192  if (input.peek() == ')')
193  {
194  input.ignore();
195  }
196  }
197  else
198  {
199  input >> field.value;
200  }
201 
202  return input;
203  }
204 
210  struct dms_in_brackets
211  {
212  dms_in_brackets(double& angle) :
213  value(angle) {}
214  double& value;
215  };
216 
217  inline std::istream& operator>>(std::istream& input, dms_in_brackets field)
218  {
219  double degrees = 0.0, minutes = 0.0, seconds = 0.0;
220  input >> in_brackets(degrees) >> in_brackets(minutes) >> in_brackets(seconds);
221  field.value = degrees + (minutes + seconds / 60.0) / 60.0;
222  return input;
223  }
224 
225 } // namespace vsgXchange
226 
227 #endif
optional GDAL ReaderWriter
Definition: gdal.h:37