49
49
namespace pdal
50
50
{
51
51
52
- PipelineReaderJSON::PipelineReaderJSON (PipelineManager& manager) :
53
- m_manager (manager)
54
- {}
52
+ using TagMap = std::map<std::string, Stage *>;
53
+
54
+ namespace
55
+ {
55
56
57
+ std::string extractType (NL::json& node);
58
+ std::string extractTag (NL::json& node, TagMap& tags);
59
+ FileSpec extractFilename (NL::json& node);
60
+ std::vector<Stage *> extractInputs (NL::json& node, TagMap& tags);
61
+ Options extractOptions (NL::json& node);
62
+ bool extractOption (Options& options, const std::string& name, const NL::json& node);
63
+ void handleInputTag (const std::string& tag, const TagMap& tags, std::vector<Stage *>& inputs);
56
64
57
- void PipelineReaderJSON:: parsePipeline (NL::json& tree)
65
+ void parsePipeline (NL::json& tree, PipelineManager& manager )
58
66
{
59
67
TagMap tags;
60
68
std::vector<Stage*> inputs;
@@ -101,7 +109,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
101
109
{
102
110
spec.m_path = path;
103
111
ReaderCreationOptions ops { spec, type, nullptr , options, tag };
104
- s = &m_manager .makeReader (ops);
112
+ s = &manager .makeReader (ops);
105
113
106
114
if (specifiedInputs.size ())
107
115
throw pdal_error (" JSON pipeline: Inputs not permitted for "
@@ -112,7 +120,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
112
120
else if (type.empty () || Utils::startsWith (type, " writers." ))
113
121
{
114
122
StageCreationOptions ops { spec.m_path .string (), type, nullptr , options, tag };
115
- s = &m_manager .makeWriter (ops);
123
+ s = &manager .makeWriter (ops);
116
124
for (Stage *ts : inputs)
117
125
s->setInput (*ts);
118
126
inputs.clear ();
@@ -123,7 +131,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
123
131
if (spec.valid ())
124
132
options.add (" filename" , spec.m_path .string ());
125
133
StageCreationOptions ops { " " , type, nullptr , options, tag };
126
- s = &m_manager .makeFilter (ops);
134
+ s = &manager .makeFilter (ops);
127
135
for (Stage *ts : inputs)
128
136
s->setInput (*ts);
129
137
inputs.clear ();
@@ -136,10 +144,10 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
136
144
}
137
145
138
146
// Tell user if the pipeline seems wacky.
139
- const std::vector<Stage *> llist = m_manager .leaves ();
147
+ const std::vector<Stage *> llist = manager .leaves ();
140
148
if (llist.size () > 1 )
141
149
{
142
- const LogPtr& log = m_manager .log ();
150
+ const LogPtr& log = manager .log ();
143
151
log->get (LogLevel::Error) << " Pipeline has multiple leaf nodes.\n " ;
144
152
log->get (LogLevel::Error) << " Only the first of the following leaf nodes will be run.\n " ;
145
153
for (Stage *s : llist)
@@ -150,99 +158,7 @@ void PipelineReaderJSON::parsePipeline(NL::json& tree)
150
158
}
151
159
}
152
160
153
-
154
- void PipelineReaderJSON::readPipeline (std::istream& input)
155
- {
156
- NL::json root;
157
-
158
- try
159
- {
160
- root = NL::json::parse (input, /* callback */ nullptr ,
161
- /* allow exceptions */ true ,
162
- /* ignore_comments */ true );
163
- }
164
- catch (NL::json::parse_error& err)
165
- {
166
- // Look for a right bracket -- this indicates the start of the
167
- // actual message from the parse error.
168
- std::string s (err.what ());
169
- auto pos = s.find (" ]" );
170
- if (pos != std::string::npos)
171
- s = s.substr (pos + 1 );
172
- throw pdal_error (" Pipeline:" + s);
173
- }
174
-
175
- auto it = root.find (" pipeline" );
176
- if (root.is_object () && it != root.end ())
177
- parsePipeline (*it);
178
- else if (root.is_array ())
179
- parsePipeline (root);
180
- else
181
- throw pdal_error (" Pipeline: root element is not a pipeline." );
182
- }
183
-
184
-
185
- void PipelineReaderJSON::readPipeline (const std::string& filename)
186
- {
187
- std::istream* input = Utils::openFile (filename);
188
- if (!input)
189
- {
190
- throw pdal_error (" Pipeline: Unable to open stream for "
191
- " file \" " + filename + " \" " );
192
- }
193
-
194
- try
195
- {
196
- readPipeline (*input);
197
- }
198
- catch (...)
199
- {
200
- Utils::closeFile (input);
201
- throw ;
202
- }
203
-
204
- Utils::closeFile (input);
205
- }
206
-
207
-
208
- std::string PipelineReaderJSON::extractType (NL::json& node)
209
- {
210
- std::string type;
211
-
212
- auto it = node.find (" type" );
213
- if (it != node.end ())
214
- {
215
- NL::json& val = *it;
216
- if (!val.is_null ())
217
- {
218
- if (val.is_string ())
219
- type = val.get <std::string>();
220
- else
221
- throw pdal_error (" JSON pipeline: 'type' must be specified as "
222
- " a string." );
223
- }
224
- node.erase (it);
225
- }
226
- return type;
227
- }
228
-
229
-
230
- FileSpec PipelineReaderJSON::extractFilename (NL::json& node)
231
- {
232
- FileSpec spec;
233
-
234
- auto it = node.find (" filename" );
235
- if (it == node.end ())
236
- return spec;
237
-
238
- Utils::StatusWithReason status = spec.parse (*it);
239
- if (!status)
240
- throw pdal_error (status.what ());
241
- node.erase (it);
242
- return spec;
243
- }
244
-
245
- std::string PipelineReaderJSON::extractTag (NL::json& node, TagMap& tags)
161
+ std::string extractTag (NL::json& node, TagMap& tags)
246
162
{
247
163
std::string tag;
248
164
@@ -273,21 +189,22 @@ std::string PipelineReaderJSON::extractTag(NL::json& node, TagMap& tags)
273
189
return tag;
274
190
}
275
191
276
-
277
- void PipelineReaderJSON::handleInputTag (const std::string& tag,
278
- const TagMap& tags, std::vector<Stage *>& inputs)
192
+ FileSpec extractFilename (NL::json& node)
279
193
{
280
- auto ii = tags.find (tag);
281
- if (ii == tags.end ())
282
- throw pdal_error (" JSON pipeline: Invalid pipeline: "
283
- " undefined stage tag '" + tag + " '." );
284
- else
285
- inputs.push_back (ii->second );
286
- }
194
+ FileSpec spec;
195
+
196
+ auto it = node.find (" filename" );
197
+ if (it == node.end ())
198
+ return spec;
287
199
200
+ Utils::StatusWithReason status = spec.parse (*it);
201
+ if (!status)
202
+ throw pdal_error (status.what ());
203
+ node.erase (it);
204
+ return spec;
205
+ }
288
206
289
- std::vector<Stage *> PipelineReaderJSON::extractInputs (NL::json& node,
290
- TagMap& tags)
207
+ std::vector<Stage *> extractInputs (NL::json& node, TagMap& tags)
291
208
{
292
209
std::vector<Stage *> inputs;
293
210
std::string filename;
@@ -316,34 +233,7 @@ std::vector<Stage *> PipelineReaderJSON::extractInputs(NL::json& node,
316
233
return inputs;
317
234
}
318
235
319
- namespace
320
- {
321
-
322
- bool extractOption (Options& options, const std::string& name,
323
- const NL::json& node)
324
- {
325
- if (node.is_string ())
326
- options.add (name, node.get <std::string>());
327
- else if (node.is_number_unsigned ())
328
- options.add (name, node.get <uint64_t >());
329
- else if (node.is_number_integer ())
330
- options.add (name, node.get <int64_t >());
331
- else if (node.is_number_float ())
332
- options.add (name, node.get <double >());
333
- else if (node.is_boolean ())
334
- options.add (name, node.get <bool >());
335
- else if (node.is_array ())
336
- options.add (name, node.get <NL::json::array_t >());
337
- else if (node.is_null ())
338
- options.add (name, " " );
339
- else
340
- return false ;
341
- return true ;
342
- }
343
-
344
- } // unnamed namespace
345
-
346
- Options PipelineReaderJSON::extractOptions (NL::json& node)
236
+ Options extractOptions (NL::json& node)
347
237
{
348
238
Options options;
349
239
@@ -380,4 +270,115 @@ Options PipelineReaderJSON::extractOptions(NL::json& node)
380
270
return options;
381
271
}
382
272
273
+ std::string extractType (NL::json& node)
274
+ {
275
+ std::string type;
276
+
277
+ auto it = node.find (" type" );
278
+ if (it != node.end ())
279
+ {
280
+ NL::json& val = *it;
281
+ if (!val.is_null ())
282
+ {
283
+ if (val.is_string ())
284
+ type = val.get <std::string>();
285
+ else
286
+ throw pdal_error (" JSON pipeline: 'type' must be specified as "
287
+ " a string." );
288
+ }
289
+ node.erase (it);
290
+ }
291
+ return type;
292
+ }
293
+
294
+ bool extractOption (Options& options, const std::string& name, const NL::json& node)
295
+ {
296
+ if (node.is_string ())
297
+ options.add (name, node.get <std::string>());
298
+ else if (node.is_number_unsigned ())
299
+ options.add (name, node.get <uint64_t >());
300
+ else if (node.is_number_integer ())
301
+ options.add (name, node.get <int64_t >());
302
+ else if (node.is_number_float ())
303
+ options.add (name, node.get <double >());
304
+ else if (node.is_boolean ())
305
+ options.add (name, node.get <bool >());
306
+ else if (node.is_array ())
307
+ options.add (name, node.get <NL::json::array_t >());
308
+ else if (node.is_null ())
309
+ options.add (name, " " );
310
+ else
311
+ return false ;
312
+ return true ;
313
+ }
314
+
315
+ void handleInputTag (const std::string& tag, const TagMap& tags, std::vector<Stage *>& inputs)
316
+ {
317
+ auto ii = tags.find (tag);
318
+ if (ii == tags.end ())
319
+ throw pdal_error (" JSON pipeline: Invalid pipeline: "
320
+ " undefined stage tag '" + tag + " '." );
321
+ else
322
+ inputs.push_back (ii->second );
323
+ }
324
+
325
+
326
+ } // unnamed namespace
327
+
328
+ PipelineReaderJSON::PipelineReaderJSON (PipelineManager& manager) :
329
+ m_manager (manager)
330
+ {}
331
+
332
+ void PipelineReaderJSON::readPipeline (const std::string& filename)
333
+ {
334
+ std::istream* input = Utils::openFile (filename);
335
+ if (!input)
336
+ {
337
+ throw pdal_error (" Pipeline: Unable to open stream for "
338
+ " file \" " + filename + " \" " );
339
+ }
340
+
341
+ try
342
+ {
343
+ readPipeline (*input);
344
+ }
345
+ catch (...)
346
+ {
347
+ Utils::closeFile (input);
348
+ throw ;
349
+ }
350
+
351
+ Utils::closeFile (input);
352
+ }
353
+
354
+ void PipelineReaderJSON::readPipeline (std::istream& input)
355
+ {
356
+ NL::json root;
357
+
358
+ try
359
+ {
360
+ root = NL::json::parse (input, /* callback */ nullptr ,
361
+ /* allow exceptions */ true ,
362
+ /* ignore_comments */ true );
363
+ }
364
+ catch (NL::json::parse_error& err)
365
+ {
366
+ // Look for a right bracket -- this indicates the start of the
367
+ // actual message from the parse error.
368
+ std::string s (err.what ());
369
+ auto pos = s.find (" ]" );
370
+ if (pos != std::string::npos)
371
+ s = s.substr (pos + 1 );
372
+ throw pdal_error (" Pipeline:" + s);
373
+ }
374
+
375
+ auto it = root.find (" pipeline" );
376
+ if (root.is_object () && it != root.end ())
377
+ parsePipeline (*it, m_manager);
378
+ else if (root.is_array ())
379
+ parsePipeline (root, m_manager);
380
+ else
381
+ throw pdal_error (" Pipeline: root element is not a pipeline." );
382
+ }
383
+
383
384
} // namespace pdal
0 commit comments