forked from PDAL/PDAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialReference.cpp
More file actions
389 lines (287 loc) · 9.16 KB
/
Copy pathSpatialReference.cpp
File metadata and controls
389 lines (287 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/******************************************************************************
* Copyright (c) 2009, Howard Butler
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the Martin Isenburg or Iowa Department
* of Natural Resources nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include <pdal/SpatialReference.hpp>
#include <boost/concept_check.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/concept_check.hpp> // ignore_unused_variable_warning
#include <boost/algorithm/string/trim.hpp>
// gdal
#ifdef PDAL_HAVE_GDAL
#include <ogr_spatialref.h>
#include <cpl_conv.h>
#endif
#include <pdal/Utils.hpp>
namespace pdal
{
SpatialReference::SpatialReference()
: m_wkt("")
{
return;
}
SpatialReference::SpatialReference(const std::string& s)
: m_wkt("")
{
this->setFromUserInput(s);
return;
}
SpatialReference::SpatialReference(SpatialReference const& rhs)
: m_wkt(rhs.m_wkt)
{
return;
}
SpatialReference& SpatialReference::operator=(SpatialReference const& rhs)
{
if (&rhs != this)
{
m_wkt = rhs.m_wkt;
}
return *this;
}
SpatialReference::~SpatialReference()
{
return;
}
bool SpatialReference::empty() const
{
return (getWKT() == "");
}
std::string SpatialReference::getWKT(WKTModeFlag mode_flag) const
{
return getWKT(mode_flag, false);
}
/// Fetch the SRS as WKT
std::string SpatialReference::getWKT(WKTModeFlag mode_flag , bool pretty) const
{
#ifndef PDAL_SRS_ENABLED
boost::ignore_unused_variable_warning(mode_flag);
boost::ignore_unused_variable_warning(pretty);
// we don't have a way of making this pretty, or of stripping the compound wrapper.
return m_wkt;
#else
std::string result_wkt = m_wkt;
if ((mode_flag == eHorizontalOnly
&& strstr(result_wkt.c_str(),"COMPD_CS") != NULL)
|| pretty)
{
OGRSpatialReference* poSRS = (OGRSpatialReference*) OSRNewSpatialReference(result_wkt.c_str());
char *pszWKT = NULL;
if (mode_flag == eHorizontalOnly)
poSRS->StripVertical();
if (pretty)
poSRS->exportToPrettyWkt(&pszWKT, FALSE);
else
poSRS->exportToWkt(&pszWKT);
OSRDestroySpatialReference(poSRS);
result_wkt = pszWKT;
CPLFree(pszWKT);
}
return result_wkt;
#endif
}
void SpatialReference::setFromUserInput(std::string const& v)
{
#ifdef PDAL_SRS_ENABLED
char* poWKT = 0;
const char* input = v.c_str();
// OGRSpatialReference* poSRS = (OGRSpatialReference*) OSRNewSpatialReference(NULL);
OGRSpatialReference srs(NULL);
OGRErr err = srs.SetFromUserInput(const_cast<char *>(input));
if (err != OGRERR_NONE)
{
throw std::invalid_argument("could not import coordinate system into OGRSpatialReference SetFromUserInput");
}
srs.exportToWkt(&poWKT);
std::string tmp(poWKT);
CPLFree(poWKT);
setWKT(tmp);
#else
boost::ignore_unused_variable_warning(v);
throw std::runtime_error("GDAL is not available, SpatialReference could not be set from WKT");
#endif
}
void SpatialReference::setWKT(std::string const& v)
{
m_wkt = v;
return;
}
std::string SpatialReference::getProj4() const
{
#ifdef PDAL_SRS_ENABLED
std::string wkt = getWKT(eCompoundOK);
const char* poWKT = wkt.c_str();
OGRSpatialReference srs(NULL);
if (OGRERR_NONE != srs.importFromWkt(const_cast<char **>(&poWKT)))
{
return std::string();
}
char* proj4 = 0;
srs.exportToProj4(&proj4);
std::string tmp(proj4);
CPLFree(proj4);
boost::algorithm::trim(tmp);
return tmp;
#else
// By default or if we have neither GDAL nor proj.4, we can't do squat
return std::string();
#endif
}
void SpatialReference::setProj4(std::string const& v)
{
#ifdef PDAL_SRS_ENABLED
char* poWKT = 0;
const char* poProj4 = v.c_str();
OGRSpatialReference srs(NULL);
if (OGRERR_NONE != srs.importFromProj4(const_cast<char *>(poProj4)))
{
throw std::invalid_argument("could not import proj4 into OSRSpatialReference SetProj4");
}
srs.exportToWkt(&poWKT);
std::string tmp(poWKT);
CPLFree(poWKT);
m_wkt = tmp;
#else
boost::ignore_unused_variable_warning(v);
#endif
return;
}
bool SpatialReference::equals(const SpatialReference& input) const
{
#ifdef PDAL_SRS_ENABLED
OGRSpatialReferenceH current = OSRNewSpatialReference(getWKT(eCompoundOK, false).c_str());
OGRSpatialReferenceH other = OSRNewSpatialReference(input.getWKT(eCompoundOK, false).c_str());
int output = OSRIsSame(current, other);
OSRDestroySpatialReference(current);
OSRDestroySpatialReference(other);
return (output==1);
#else
boost::ignore_unused_variable_warning(input);
throw pdal_error("SpatialReference equality testing not available without GDAL+libgeotiff support");
#endif
}
bool SpatialReference::operator==(const SpatialReference& input) const
{
return this->equals(input);
}
bool SpatialReference::operator!=(const SpatialReference& input) const
{
return !(this->equals(input));
}
const std::string& SpatialReference::getName() const
{
static std::string name("pdal.spatialreference");
return name;
}
bool SpatialReference::isGeographic() const
{
#ifdef PDAL_SRS_ENABLED
OGRSpatialReferenceH current = OSRNewSpatialReference(getWKT(eCompoundOK, false).c_str());
int isGeog = OSRIsGeographic(current);
bool output(false);
if (isGeog == 0)
output = false;
else
output = true;
OSRDestroySpatialReference(current);
return output;
#else
throw std::runtime_error("GDAL is not available, SpatialReference could not determine if isGeographic");
#endif
}
boost::property_tree::ptree SpatialReference::toPTree() const
{
using boost::property_tree::ptree;
ptree srs;
#ifdef PDAL_SRS_ENABLED
srs.put("proj4", getProj4());
srs.put("prettywkt", getWKT(SpatialReference::eHorizontalOnly, true));
srs.put("wkt", getWKT(SpatialReference::eHorizontalOnly, false));
srs.put("compoundwkt", getWKT(eCompoundOK, false));
srs.put("prettycompoundwkt", getWKT(eCompoundOK, true));
#else
std::string message;
if (m_wkt.size() == 0)
{
message = "Reference defined with VLR keys, but GeoTIFF and GDAL support are not available to produce definition";
}
else if (m_wkt.size() > 0)
{
message = "Reference defined with WKT, but GeoTIFF and GDAL support are not available to produce definition";
}
else
{
message = "None";
}
srs.put("proj4", message);
srs.put("prettywkt", message);
srs.put("wkt", message);
srs.put("compoundwkt", message);
srs.put("prettycompoundwkt", message);
srs.put("gtiff", message);
#endif
return srs;
}
void SpatialReference::dump() const
{
std::cout << *this;
}
std::ostream& operator<<(std::ostream& ostr, const SpatialReference& srs)
{
#ifdef PDAL_SRS_ENABLED
std::string wkt = srs.toPTree().get<std::string>("prettycompoundwkt");
ostr << wkt;
return ostr;
#else
boost::ignore_unused_variable_warning(ostr);
boost::ignore_unused_variable_warning(srs);
ostr << "SpatialReference data is not available without GDAL+libgeotiff support";
return ostr;
#endif
}
std::istream& operator>>(std::istream& istr, SpatialReference& srs)
{
#ifdef PDAL_SRS_ENABLED
SpatialReference ref;
std::ostringstream oss;
oss << istr.rdbuf();
std::string wkt = oss.str();
ref.setFromUserInput(wkt.c_str());
srs = ref;
return istr;
#else
boost::ignore_unused_variable_warning(istr);
boost::ignore_unused_variable_warning(srs);
throw pdal_error("SpatialReference io operator>> is not available without GDAL+libgeotiff support");
#endif
}
} // namespace pdal