/
PDALUtils.hpp
296 lines (247 loc) · 8.42 KB
/
PDALUtils.hpp
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
/******************************************************************************
* Copyright (c) 2014, Hobu Inc.
*
* 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.
****************************************************************************/
#pragma once
#include <boost/property_tree/json_parser.hpp>
#include <boost/algorithm/string.hpp>
#include <pdal/Dimension.hpp>
#include <pdal/pdal_defines.h>
#include <pdal/Bounds.hpp>
#include <pdal/PointContext.hpp>
#include <pdal/PointBuffer.hpp>
#include <pdal/Metadata.hpp>
#include <pdal/Options.hpp>
namespace pdal
{
namespace utils
{
using namespace boost::property_tree;
inline ptree toPTree(MetadataNode const& node)
{
typedef ptree::path_type path;
ptree tree;
tree.put("name", node.name());
tree.put("description", node.description());
tree.put("type", node.type());
tree.put("value", node.value());
for (auto n = node.children().begin(); n != node.children().end(); ++n)
{
ptree pnode = toPTree(*n);
if (boost::algorithm::iequals(node.kind(),"array"))
{
boost::optional<ptree&> opt =
tree.get_child_optional(path(node.name(), '/'));
if (opt)
opt->push_back(std::make_pair("", pnode));
else
{
tree.push_back(ptree::value_type(node.name(), ptree()));
auto& p = tree.get_child(path(node.name(), '/'));
p.push_back(std::make_pair("", pnode));
}
}
else if (node.name().size())
tree.push_back(std::make_pair(node.name(), pnode));
}
return tree;
}
inline ptree toPTree(PointContextRef ctx)
{
ptree tree;
ptree dimsTree;
for (const auto& id : ctx.dims())
{
ptree dim;
dim.put("name", ctx.dimName(id));
Dimension::Type::Enum t = ctx.dimType(id);
dim.put("type", Dimension::toName(Dimension::base(t)));
dim.put("size", ctx.dimSize(id));
dimsTree.push_back(std::make_pair("", dim));
}
tree.add_child("dimensions", dimsTree);
return tree;
}
/*! returns a boost::property_tree containing the point records, which is
useful for dumping and such.
\verbatim embed:rst
::
0:
X: 1.00
Y: 2.00
Z: 3.00
1:
X: 1.00
Y: 2.00
Z: 3.00
\endverbatim
*/
inline ptree toPTree(const PointBuffer& buffer)
{
ptree tree;
const Dimension::IdList& dims = buffer.dims();
for (PointId idx = 0; idx < buffer.size(); idx++)
{
std::string pointstring = boost::lexical_cast<std::string>(idx) + ".";
for (auto di = dims.begin(); di != dims.end(); ++di)
{
std::string key = pointstring + Dimension::name(*di);
double v = buffer.getFieldAs<double>(*di, idx);
std::string value = boost::lexical_cast<std::string>(v);
tree.add(key, value);
}
}
return tree;
}
template <typename T> ptree toPTree(const Range<T>& rng)
{
ptree tree;
tree.add("minimum", rng.getMinimum());
tree.add("maximum", rng.getMaximum());
return tree;
}
/// Outputs a string-based boost::property_tree::ptree representation
/// of the Bounds instance
template <typename T> ptree toPTree(const Bounds<T>& bounds)
{
ptree tree;
for (std::size_t i = 0; i < bounds.size(); ++i)
{
const Range<T>& r = bounds.dimensions()[i];
tree.add_child(boost::lexical_cast<std::string>(i), toPTree(r));
}
return tree;
}
ptree toPTree(const Options& options);
inline ptree toPTree(const Option& option)
{
ptree t;
t.put("Name", option.getName());
t.put("Value", option.getValue<std::string>());
if (option.getDescription() != "")
{
t.put("Description", option.getDescription());
}
boost::optional<Options const&> options = option.getOptions();
if (options != boost::none)
{
t.add_child("Options", toPTree(*options));
}
return t;
}
inline ptree toPTree(const Options& options)
{
ptree tree;
std::vector<Option> opts = options.getOptions();
for (auto citer = opts.begin(); citer != opts.end(); ++citer)
{
const Option& option = *citer;
ptree subtree = toPTree(option);
tree.add_child("Option", subtree);
}
return tree;
}
inline ptree toPTree(const SpatialReference& ref)
{
ptree srs;
#ifdef PDAL_SRS_ENABLED
srs.put("proj4", ref.getProj4());
srs.put("prettywkt", ref.getWKT(SpatialReference::eHorizontalOnly, true));
srs.put("wkt", ref.getWKT(SpatialReference::eHorizontalOnly, false));
srs.put("compoundwkt", ref.getWKT(SpatialReference::eCompoundOK, false));
srs.put("prettycompoundwkt", ref.getWKT(SpatialReference::eCompoundOK, true));
#else
std::string message;
std::string wkt = ref.getWKT();
if (wkt.size() == 0)
message = "Reference defined with VLR keys, but GeoTIFF and GDAL "
"support are not available to produce definition";
else if (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;
}
namespace reST
{
inline std::ostream& toRST(const PointBuffer& buffer, std::ostream& os)
{
const Dimension::IdList& dims = buffer.dims();
size_t name_column(20);
size_t value_column(40);
for (auto di = dims.begin(); di != dims.end(); ++di)
{
std::string name = Dimension::name(*di);
name_column = std::max(name_column, name.size());
}
std::ostringstream thdr;
for (unsigned i = 0; i < name_column - 1; ++i)
thdr << "=";
thdr << " ";
for (unsigned i = 0; i < value_column - 1; ++i)
thdr << "=";
thdr << " ";
thdr << " ";
name_column--;
unsigned step_back(3);
std::string hdr(80, '-');
for (PointId idx = 0; idx < buffer.size(); ++idx)
{
os << "Point " << idx << std::endl;
os << hdr << std::endl << std::endl;
os << thdr.str() << std::endl;
os << std::setw(name_column-step_back) << "Name" <<
std::setw(value_column-step_back) << "Value" << std::endl;
os << thdr.str() << std::endl;
for (auto di = dims.begin(); di != dims.end(); ++di)
{
double v = buffer.getFieldAs<double>(*di, idx);
std::string value = boost::lexical_cast<std::string>(v);
os << std::left << std::setw(name_column) << Dimension::name(*di) <<
std::right << std::setw(value_column) << value << std::endl;
}
os << thdr.str() << std::endl << std::endl;
}
os << std::endl << std::endl;
return os;
}
} // reST
} // utils
} // pdal