-
Notifications
You must be signed in to change notification settings - Fork 433
/
pdal.cpp
364 lines (312 loc) · 11.3 KB
/
pdal.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
/******************************************************************************
* Copyright (c) 2013, Howard Butler (hobu.inc@gmail.com)
* Copyright (c) 2014-2015, Bradley J Chambers (brad.chambers@gmail.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/KernelFactory.hpp>
#include <pdal/StageFactory.hpp>
#include <pdal/pdal_config.hpp>
#include <boost/algorithm/string.hpp>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
using namespace pdal;
std::string headline("------------------------------------------------------------------------------------------");
std::string splitDriverName(std::string const& name)
{
std::vector<std::string> driverVec;
boost::algorithm::split(driverVec, name,
boost::algorithm::is_any_of("."), boost::algorithm::token_compress_on);
return driverVec[1];
}
void outputVersion()
{
std::cout << headline << std::endl;
std::cout << "pdal " << "(" << GetFullVersionString() << ")" << std::endl;
std::cout << headline << std::endl;
std::cout << std::endl;
}
void outputHelp()
{
std::cerr << "Usage: pdal <command> [--debug] [--drivers] [--help] [--options[=<driver name>]] [--version]\n";
std::cerr << " --debug Show debug information\n";
std::cerr << " --drivers Show drivers\n";
std::cerr << " -h [ --help ] Print help message\n";
std::cerr << " --options [=arg(=all)] Show driver options\n";
std::cerr << " --version Show version info\n";
std::cerr << "\n";
std::cerr << "The most commonly used pdal commands are:\n";
KernelFactory f(false);
std::vector<std::string> loaded_kernels = f.getKernelNames();
for (auto name : loaded_kernels)
std::cout << " - " << splitDriverName(name) << std::endl;
std::cout << "See http://pdal.io/apps.html for more detail";
std::cout << std::endl;
}
void outputDrivers()
{
StageFactory f(false);
std::map<std::string, std::string> sm = f.getStageMap();
std::ostringstream strm;
std::string tablehead("================================ ==========================================================");
std::string headings ("Name Description");
strm << std::endl;
strm << tablehead << std::endl;
strm << headings << std::endl;
strm << tablehead << std::endl;
uint32_t name_column(32);
uint32_t description_column(57);
strm << std::left;
for (auto s : sm)
{
std::vector<std::string> lines;
std::string description(s.second);
description = boost::algorithm::erase_all_copy(description, "\n");
Utils::wordWrap(description, lines, description_column-1);
if (lines.size() == 1)
{
strm << std::setw(name_column) << s.first << " "
<< std::setw(description_column) << description << std::endl;
}
else
{
strm << std::setw(name_column) << s.first << " "
<< lines[0] << std::endl;
}
std::stringstream blank;
size_t blanks(33);
for (size_t i = 0; i < blanks; ++i)
blank << " ";
for (size_t i = 1; i < lines.size(); ++i)
strm << blank.str() << lines[i] << std::endl;
}
strm << tablehead << std::endl;
std::cout << strm.str() << std::endl;
}
void outputOptions(std::string const& n)
{
StageFactory f(false);
std::unique_ptr<Stage> s = f.createStage(n);
if (!s)
{
std::cerr << "Unable to create stage " << n << "\n";
return;
}
std::vector<Option> options = s->getDefaultOptions().getOptions();
if (options.size())
{
std::ostringstream strm;
strm << n << std::endl;
strm << headline << std::endl;
std::string tablehead("================================ =============== =========================================");
std::string headings ("Name Default Description");
strm << std::endl;
strm << tablehead << std::endl;
strm << headings << std::endl;
strm << tablehead << std::endl;
uint32_t default_column(15);
uint32_t name_column(32);
uint32_t description_column(40);
strm << std::left;
for (auto const& opt : options)
{
std::string default_value(opt.getValue<std::string>() );
default_value = boost::algorithm::erase_all_copy(default_value, "\n");
if (default_value.size() > default_column -1 )
{
default_value = default_value.substr(0, default_column-3);
default_value = default_value + "...";
}
std::vector<std::string> lines;
std::string description(opt.getDescription());
description = boost::algorithm::erase_all_copy(description, "\n");
Utils::wordWrap(description, lines, description_column-1);
if (lines.size() == 1)
{
strm << std::setw(name_column) << opt.getName() << " "
<< std::setw(default_column) << default_value << " "
<< std::setw(description_column) << description << std::endl;
} else
strm << std::setw(name_column) << opt.getName() << " "
<< std::setw(default_column) << default_value << " "
<< lines[0] << std::endl;
std::stringstream blank;
size_t blanks(49);
for (size_t i = 0; i < blanks; ++i)
blank << " ";
for (size_t i = 1; i < lines.size(); ++i)
{
strm << blank.str() <<lines[i] << std::endl;
}
}
strm << tablehead << std::endl;
strm << std::endl;
std::cout << strm.str() << std::endl;
}
else
{
std::cerr << n << " has no options\n";
}
}
void outputOptions()
{
StageFactory f(false);
std::vector<std::string> nv = f.getStageNames();
for (auto const& n : nv)
outputOptions(n);
}
int main(int argc, char* argv[])
{
// No arguments, print basic usage, plugins will be loaded
if (argc < 2)
{
outputHelp(/*false*/);
return 1;
}
// Discover available kernels without plugins, and test to see if
// the positional option 'command' is a valid kernel
KernelFactory f(true);
std::vector<std::string> loaded_kernels;
loaded_kernels = f.getKernelNames();
bool isValidKernel = false;
std::string command(argv[1]);
std::string fullname;
for (auto name : loaded_kernels)
{
if (boost::iequals(argv[1], splitDriverName(name)))
{
fullname = name;
isValidKernel = true;
break;
}
}
// If the kernel was not available, then light up the plugins and retry
if (!isValidKernel)
{
KernelFactory f(false);
loaded_kernels.clear();
loaded_kernels = f.getKernelNames();
for (auto name : loaded_kernels)
{
if (boost::iequals(argv[1], splitDriverName(name)))
{
fullname = name;
isValidKernel = true;
break;
}
}
}
// Dispatch execution to the kernel, passing all remaining args
if (isValidKernel)
{
int count(argc - 1); // remove the 1st argument
const char** args = const_cast<const char**>(&argv[1]);
std::unique_ptr<Kernel> app = f.createKernel(fullname);
return app->run(count, args, command);
}
// Otherwise, process the remaining args to see if they are supported
bool debug = false;
bool drivers = false;
bool help = false;
bool options = false;
bool version = false;
std::string optString("all"); // --options will default to displaying information on all available stages
for (int i = 1; i < argc; ++i)
{
if (boost::iequals(argv[i], "--debug"))
{
debug = true;
}
else if (boost::iequals(argv[i], "--drivers"))
{
drivers = true;
}
else if (boost::iequals(argv[i], "--help") || boost::iequals(argv[i], "-h"))
{
help = true;
}
else if (boost::algorithm::istarts_with(argv[i], "--options"))
{
std::vector<std::string> optionsVec;
// we are rather unsophisticated for now, only splitting on '=', no spaces allowed
boost::algorithm::split(optionsVec, argv[i],
boost::algorithm::is_any_of("="), boost::algorithm::token_compress_on);
options = true;
if (optionsVec.size() == 2)
optString = optionsVec[1];
}
else if (boost::iequals(argv[i], "--version"))
{
version = true;
}
else
{
if (boost::algorithm::istarts_with(argv[i], "--"))
std::cerr << "Unknown option '" << argv[i] <<"' not recognized" << std::endl << std::endl;
}
}
if (version)
{
outputVersion();
return 0;
}
if (drivers)
{
outputDrivers();
return 0;
}
if (options)
{
if (boost::iequals(optString, "all"))
outputOptions();
else
outputOptions(optString);
return 0;
}
if (debug)
{
std::cerr << getPDALDebugInformation() << std::endl;
return 0;
}
if (help)
{
outputHelp();
return 0;
}
if (!isValidKernel)
std::cerr << "Command '" << command <<"' not recognized" << std::endl << std::endl;
outputHelp();
return 1;
}