1 /******************************************************************************
2 * Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 *     * Redistributions of source code must retain the above copyright
11 *       notice, this list of conditions and the following disclaimer.
12 *     * Redistributions in binary form must reproduce the above copyright
13 *       notice, this list of conditions and the following disclaimer in
14 *       the documentation and/or other materials provided
15 *       with the distribution.
16 *     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 *       names of its contributors may be used to endorse or promote
18 *       products derived from this software without specific prior
19 *       written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34 
35 #include <pdal/PipelineWriter.hpp>
36 
37 #include <pdal/Metadata.hpp>
38 #include <pdal/PDALUtils.hpp>
39 #include <pdal/Stage.hpp>
40 
41 namespace pdal
42 {
43 
44 namespace
45 {
46 
generateTag(Stage * stage,PipelineWriter::TagMap & tags)47 std::string generateTag(Stage *stage, PipelineWriter::TagMap& tags)
48 {
49     auto tagExists = [tags](const std::string& tag)
50     {
51         for (auto& t : tags)
52         {
53             if (t.second == tag)
54                 return true;
55         }
56         return false;
57     };
58 
59     std::string tag = stage->tag();
60     if (tag.empty())
61     {
62         for (size_t i = 1; ; ++i)
63         {
64             tag = stage->getName() + std::to_string(i);
65             tag = Utils::replaceAll(tag, ".", "_");
66             if (!tagExists(tag))
67                 break;
68         }
69     }
70     return tag;
71 }
72 
generateTags(Stage * stage,PipelineWriter::TagMap & tags)73 void generateTags(Stage *stage, PipelineWriter::TagMap& tags)
74 {
75     for (Stage *s : stage->getInputs())
76         generateTags(s, tags);
77     tags[stage] = generateTag(stage, tags);
78 }
79 
80 } // anonymous namespace
81 
82 namespace PipelineWriter
83 {
84 
writePipeline(Stage * stage,const std::string & filename)85 PDAL_DLL void writePipeline(Stage *stage, const std::string& filename)
86 {
87     std::ostream *out = Utils::createFile(filename, false);
88     writePipeline(stage, *out);
89     Utils::closeFile(out);
90 }
91 
writePipeline(Stage * stage,std::ostream & strm)92 PDAL_DLL void writePipeline(Stage *stage, std::ostream& strm)
93 {
94     TagMap tags;
95     generateTags(stage, tags);
96 
97     MetadataNode root;
98     stage->serialize(root, tags);
99     Utils::toJSON(root, strm);
100 }
101 
102 } // namespace PipelineWriter
103 
104 } // namespace pdal
105 
106