-
Notifications
You must be signed in to change notification settings - Fork 432
/
Schema.cpp
400 lines (299 loc) · 10.1 KB
/
Schema.cpp
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
390
391
392
393
394
395
396
397
398
399
400
/******************************************************************************
* $Id$
*
* Project: libLAS - http://liblas.org - A BSD library for LAS format data.
* Purpose: LAS Schema implementation for C++ libLAS
* Author: Howard Butler, hobu.inc@gmail.com
*
******************************************************************************
* Copyright (c) 2010, 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/Schema.hpp>
#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <pdal/external/boost/uuid/uuid_io.hpp>
#include <boost/concept_check.hpp> // ignore_unused_variable_warning
#ifdef PDAL_HAVE_LIBXML2
#include <pdal/XMLSchema.hpp>
#endif
namespace pdal
{
Schema::Schema()
: m_byteSize(0)
{
return;
}
Schema::Schema(std::vector<Dimension> const& dimensions)
: m_byteSize(0)
{
for ( std::vector<Dimension>::const_iterator i = dimensions.begin();
i != dimensions.end(); ++i)
{
appendDimension(*i);
}
}
/// copy constructor
Schema::Schema(Schema const& other)
: m_byteSize(other.m_byteSize)
, m_index(other.m_index)
{
}
// assignment constructor
Schema& Schema::operator=(Schema const& rhs)
{
if (&rhs != this)
{
m_byteSize = rhs.m_byteSize;
m_index = rhs.m_index;
}
return *this;
}
bool Schema::operator==(const Schema& other) const
{
if (m_byteSize != other.m_byteSize) return false;
if (m_index.size() != other.m_index.size()) return false;
schema::index_by_index const& idx = m_index.get<schema::index>();
schema::index_by_index const& idx2 = m_index.get<schema::index>();
schema::index_by_index::size_type i(0);
for (i = 0; i < idx.size(); ++i)
{
if (!(idx[i] == idx2[i]))
{
return false;
}
}
return true;
}
bool Schema::operator!=(const Schema& other) const
{
return !(*this==other);
}
void Schema::appendDimension(const Dimension& dim)
{
// m_dimensions.push_back(dim);
// std::pair<DimensionId::Id, std::size_t> p(dim.getId(), m_dimensions.size()-1);
// m_dimensions_map.insert(p);
// Add/reset the dimension ptr on the dimensions map
Dimension d(dim);
schema::index_by_position& position_index = m_index.get<schema::position>();
schema::index_by_position::const_reverse_iterator r = position_index.rbegin();
// If we do not have anything in the index, set our current values.
// Otherwise, use the values from the last entry in the index as our
// starting point
if (r != position_index.rend())
{
Dimension const& t = *r;
m_byteSize = m_byteSize + d.getByteSize();
d.setByteOffset( t.getByteOffset() + t.getByteSize());
}
else
{
m_byteSize = d.getByteSize();
d.setByteOffset(0);
}
d.setPosition(m_index.size());
std::pair<schema::index_by_position::iterator, bool> q = position_index.insert(d);
if (! q.second)
{
std::ostringstream oss;
oss << "Could not insert into schema index because of " << q.first->getName();
throw schema_error(oss.str());
}
return;
}
const Dimension& Schema::getDimension(std::size_t t) const
{
schema::index_by_index const& idx = m_index.get<schema::index>();
if (t >= idx.size())
throw schema_error("Index position is not valid");
return idx.at(t);
}
const Dimension& Schema::getDimension(std::string const& t) const
{
schema::index_by_name const& name_index = m_index.get<schema::name>();
schema::index_by_name::const_iterator it = name_index.find(t);
std::ostringstream oss;
oss << "Dimension with name '" << t << "' not found, unable to Schema::getDimension";
// FIXME: If there are two dimensions with the same name here, we're
// scrwed
if (it != name_index.end()) {
return *it;
} else {
pdal::external::boost::uuids::uuid ps1;
try
{
pdal::external::boost::uuids::string_generator gen;
ps1 = gen(t);
} catch (std::runtime_error&)
{
// invalid string for uuid
throw dimension_not_found(oss.str());
}
schema::index_by_uid::const_iterator i = m_index.get<schema::uid>().find(ps1);
if (i != m_index.get<schema::uid>().end())
{
return *i;
} else
{
oss.str("");
oss << "Dimension with name '" << t << "' not found, unable to Schema::getDimension";
throw dimension_not_found(oss.str());
}
}
throw dimension_not_found(oss.str());
}
bool Schema::setDimension(Dimension const& dim)
{
schema::index_by_name& name_index = m_index.get<schema::name>();
schema::index_by_name::iterator it = name_index.find(dim.getName());
// FIXME: If there are two dimensions with the same name here, we're
// scrwed
if (it != name_index.end()) {
name_index.replace(it, dim);
} else {
std::ostringstream oss;
oss << "Dimension with name '" << dim.getName() << "' not found, unable to Schema::setDimension";
throw dimension_not_found(oss.str());
}
return true;
}
int Schema::getDimensionIndex(const DimensionId::Id& t) const
{
schema::index_by_id::const_iterator it = m_index.get<schema::id>().find(t);
if (it != m_index.get<schema::id>().end())
{
return it->getPosition();
} else
{
return -1;
}
}
int Schema::getDimensionIndex(const Dimension& dim) const
{
schema::index_by_name::const_iterator it = m_index.get<schema::name>().find(dim.getName());
if (it != m_index.get<schema::name>().end())
{
return it->getPosition();
}
std::ostringstream oss;
oss << "getDimensionIndex: dimension not found with name " << dim.getName();
throw dimension_not_found(oss.str());
}
bool Schema::hasDimension(const DimensionId::Id& field) const
{
schema::index_by_id::const_iterator it = m_index.get<schema::id>().find(field);
if (it != m_index.get<schema::id>().end())
{
return true;
}
return false;
}
const Dimension& Schema::getDimension(const DimensionId::Id& field) const
{
schema::index_by_id::const_iterator it = m_index.get<schema::id>().find(field);
if (it != m_index.get<schema::id>().end())
{
return *it;
}
std::ostringstream oss;
oss << "getDimension: dimension not found with field " << field;
throw dimension_not_found(oss.str());
}
const Dimension& Schema::getDimension(dimension::id const& t) const
{
schema::index_by_uid::const_iterator it = m_index.get<schema::uid>().find(t);
if (it != m_index.get<schema::uid>().end())
{
return *it;
}
std::ostringstream oss;
oss << "getDimension: dimension not found with uuid '" << boost::lexical_cast<std::string>(t) << "'";
throw dimension_not_found(oss.str());
}
Schema Schema::from_xml(std::string const& xml, std::string const& xsd)
{
#ifdef PDAL_HAVE_LIBXML2
pdal::schema::Reader reader(xml, xsd);
pdal::Schema schema = reader.getSchema();
return schema;
#else
boost::ignore_unused_variable_warning(xml);
boost::ignore_unused_variable_warning(xsd);
return Schema();
#endif
}
Schema Schema::from_xml(std::string const& xml)
{
#ifdef PDAL_HAVE_LIBXML2
std::string xsd("");
pdal::schema::Reader reader(xml, xsd);
pdal::Schema schema = reader.getSchema();
return schema;
#else
boost::ignore_unused_variable_warning(xml);
return Schema();
#endif
}
std::string Schema::to_xml(Schema const& schema)
{
#ifdef PDAL_HAVE_LIBXML2
pdal::schema::Writer writer(schema);
return writer.getXML();
#else
boost::ignore_unused_variable_warning(schema);
return std::string("");
#endif
}
boost::property_tree::ptree Schema::toPTree() const
{
boost::property_tree::ptree tree;
schema::index_by_index const& idx = m_index.get<schema::index>();
for (schema::index_by_index::const_iterator iter = idx.begin(); iter != idx.end(); ++iter)
{
const Dimension& dim = *iter;
tree.add_child("dimension", dim.toPTree());
}
return tree;
}
void Schema::dump() const
{
std::cout << *this;
}
std::ostream& operator<<(std::ostream& os, pdal::Schema const& schema)
{
boost::property_tree::ptree tree = schema.toPTree();
boost::property_tree::write_json(os, tree);
return os;
}
} // namespace pdal