/
Stage.cpp
394 lines (325 loc) · 10.4 KB
/
Stage.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
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* 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 Hobu, Inc. or Flaxen Geo Consulting 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/GDALUtils.hpp>
#include <pdal/GEOSUtils.hpp>
#include <pdal/PipelineManager.hpp>
#include <pdal/Stage.hpp>
#include <pdal/SpatialReference.hpp>
#include <pdal/PDALUtils.hpp>
#include <pdal/util/Algorithm.hpp>
#include <pdal/util/ProgramArgs.hpp>
#include "private/StageRunner.hpp"
#include <iterator>
#include <memory>
namespace pdal
{
Stage::Stage() : m_progressFd(-1), m_verbose(0), m_pointCount(0),
m_faceCount(0)
{}
void Stage::addConditionalOptions(const Options& opts)
{
for (const auto& o : opts.getOptions())
m_options.addConditional(o);
}
void Stage::serialize(MetadataNode root, PipelineWriter::TagMap& tags) const
{
for (Stage *s : m_inputs)
s->serialize(root, tags);
auto tagname = [tags](const Stage *s)
{
const auto ti = tags.find(s);
return ti->second;
};
MetadataNode anon("pipeline");
anon.add("type", getName());
anon.add("tag", tagname(this));
m_options.toMetadata(anon);
for (Stage *s : m_inputs)
anon.addList("inputs", tagname(s));
root.addList(anon);
}
void Stage::addAllArgs(ProgramArgs& args)
{
try
{
l_addArgs(args);
addArgs(args);
}
catch (arg_error error)
{
throw pdal_error(getName() + ": " + error.m_error);
}
}
void Stage::handleOptions()
{
addAllArgs(*m_args);
StringList files = m_options.getValues("option_file");
for (std::string& file : files)
m_options.addConditional(Options::fromFile(file));
m_options.remove(Option("option_file", 0));
// Special stuff for GRiD so that no error is thrown when a file
// isn't found.
files = m_options.getValues("grid_option_file");
for (std::string& file : files)
m_options.addConditional(Options::fromFile(file, false));
m_options.remove(Option("grid_option_file", 0));
StringList cmdline = m_options.toCommandLine();
try
{
m_args->parse(cmdline);
}
catch (arg_error error)
{
throw pdal_error(getName() + ": " + error.m_error);
}
setupLog();
}
QuickInfo Stage::preview()
{
m_args.reset(new ProgramArgs);
handleOptions();
startLogging();
QuickInfo qi = inspect();
stopLogging();
return qi;
}
void Stage::prepare(PointTableRef table)
{
m_args.reset(new ProgramArgs);
for (size_t i = 0; i < m_inputs.size(); ++i)
{
Stage *prev = m_inputs[i];
prev->prepare(table);
}
handleOptions();
startLogging();
l_initialize(table);
initialize(table);
addDimensions(table.layout());
prepared(table);
stopLogging();
}
PointViewSet Stage::execute(PointTableRef table)
{
table.finalize();
std::stack<Stage *> stages;
std::stack<Stage *> pending;
std::map<Stage *, Stage *> children;
m_log->get(LogLevel::Debug) << "Executing pipeline in standard mode." <<
std::endl;
pending.push(this);
// There is no child of the terminal stage.
children[this] = nullptr;
// Linearize stage execution.
Stage *s;
while (pending.size())
{
s = pending.top();
pending.pop();
stages.push(s);
for (auto fi = s->m_inputs.begin(); fi != s->m_inputs.end(); ++fi)
{
children[*fi] = s;
pending.push(*fi);
}
}
// Go through the stages in order, executing
PointViewSet outViews;
std::map<Stage *, PointViewSet> sets;
while (stages.size())
{
s = stages.top();
stages.pop();
PointViewSet& inViews = sets[s];
if (s->m_inputs.empty())
inViews.insert(PointViewPtr(new PointView(table)));
outViews = s->execute(table, inViews);
Stage *child = children[s];
if (child)
sets[child].insert(outViews.begin(), outViews.end());
// Allow previous point views to be freed.
sets.erase(s);
}
return outViews;
}
PointViewSet Stage::execute(PointTableRef table, PointViewSet& views)
{
PointViewSet outViews;
std::vector<StageRunnerPtr> runners;
startLogging();
// Put the spatial references from the views onto the table.
// The table's spatial references are only valid as long as the stage
// is running.
// ABELL - Should we clear the references once the stage run has
// completed? Wondering if that would break something where a
// writer wants to check a table's SRS.
SpatialReference srs;
table.clearSpatialReferences();
// Iterating backwards will ensure that the SRS for the first view is
// first on the list for table.
for (auto it = views.rbegin(); it != views.rend(); it++)
table.addSpatialReference((*it)->spatialReference());
// Count the number of views and the number of points and faces so they're
// available to stages.
m_pointCount = 0;
m_faceCount = 0;
for (auto const& it : views)
{
m_pointCount += it->size();
auto m = it->mesh();
if (m)
m_faceCount += m->size();
}
// Do the ready operation and then start running all the views
// through the stage.
ready(table);
for (auto const& it : views)
{
StageRunnerPtr runner(new StageRunner(this, it));
runners.push_back(runner);
runner->run();
}
// As the stages complete (synchronously at this time), propagate the
// spatial reference and merge the output views.
srs = getSpatialReference();
for (auto const& it : runners)
{
StageRunnerPtr runner(it);
PointViewSet temp = runner->wait();
// If our stage has a spatial reference, the view takes it on once
// the stage has been run.
if (!srs.empty())
for (PointViewPtr v : temp)
v->setSpatialReference(srs);
outViews.insert(temp.begin(), temp.end());
}
done(table);
stopLogging();
m_pointCount = 0;
m_faceCount = 0;
return outViews;
}
void Stage::l_addArgs(ProgramArgs& args)
{
args.add("user_data", "User JSON", m_userDataJSON);
args.add("log", "Debug output filename", m_logname);
// We never really bind anything to this variable. We extract the option
// before parsing the command line. This entry allows a line in the
// help and options list.
args.add("option_file", "File from which to read additional options",
m_optionFile);
readerAddArgs(args);
}
void Stage::setupLog()
{
LogLevel l(LogLevel::Error);
if (m_log)
{
l = m_log->getLevel();
m_logLeader = m_log->leader();
}
if (!m_logname.empty())
m_log.reset(new Log("", m_logname));
else if (!m_log)
m_log.reset(new Log("", "stdlog"));
m_log->setLevel(l);
// Add the stage name to the existing leader.
if (m_logLeader.size())
m_logLeader += " ";
m_logLeader += getName();
gdal::ErrorHandler::getGlobalErrorHandler().set(m_log, isDebug());
}
void Stage::l_initialize(PointTableRef table)
{
m_metadata = table.metadata().add(getName());
readerInitialize(table);
writerInitialize(table);
}
const SpatialReference& Stage::getSpatialReference() const
{
return m_spatialReference;
}
void Stage::setSpatialReference(const SpatialReference& spatialRef)
{
setSpatialReference(m_metadata, spatialRef);
}
void Stage::setSpatialReference(MetadataNode& m,
const SpatialReference& spatialRef)
{
m_spatialReference = spatialRef;
auto pred = [](MetadataNode m)
{ return m.name() == "spatialreference"; };
MetadataNode spatialNode = m.findChild(pred);
if (spatialNode.empty())
{
m.add(spatialRef.toMetadata());
m.add("spatialreference", spatialRef.getWKT(), "SRS of this stage");
m.add("comp_spatialreference", spatialRef.getWKT(),
"SRS of this stage");
}
}
bool Stage::parseName(std::string o, std::string::size_type& pos)
{
auto isStageChar = [](char c)
{ return std::isalpha(c) || std::isdigit(c) || c == '_'; };
if (o.empty() || !std::isalpha(o[pos]))
return false;
pos++;
pos += Utils::extract(o, pos, isStageChar);
return true;
}
bool Stage::parseTagName(std::string o, std::string::size_type& pos)
{
auto isTagChar = [](char c)
{ return std::isalnum(c) || c == '_'; };
if (!std::isalpha(o[pos]))
return false;
pos++;
pos += Utils::extract(o, pos, isTagChar);
return true;
}
void Stage::throwError(const std::string& s) const
{
throw pdal_error(getName() + ": " + s);
}
void Stage::startLogging() const
{
m_log->pushLeader(m_logLeader);
gdal::ErrorHandler::getGlobalErrorHandler().set(m_log, isDebug());
}
void Stage::stopLogging() const
{
m_log->popLeader();
}
} // namespace pdal