vsgXchange: include/vsgXchange/gdal.h Source File

vsgXchange 1.1.9 VulkanSceneGraph 3rd party data integration library

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