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
|
/******************************************************************************
* 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/pdal_features.hpp"
#include "PipelineKernel.hpp"
#ifdef PDAL_HAVE_LIBXML2
#include <pdal/XMLSchema.hpp>
#endif
#include <pdal/PDALUtils.hpp>
#include <nlohmann/json.hpp>
namespace pdal
{
static StaticPluginInfo const s_info
{
"kernels.pipeline",
"Pipeline Kernel",
"http://pdal.io/apps/pipeline.html"
};
CREATE_STATIC_KERNEL(PipelineKernel, s_info)
std::string PipelineKernel::getName() const { return s_info.name; }
PipelineKernel::PipelineKernel() : m_validate(false), m_progressFd(-1)
{}
void PipelineKernel::validateSwitches(ProgramArgs& args)
{
if (m_usestdin)
m_inputFile = "STDIN";
if (m_inputFile.empty())
throw pdal_error("Input filename required.");
if (m_stream && m_noStream)
throw pdal_error("Can't execute with 'stream' and 'nostream' options");
if (m_stream)
m_mode = ExecMode::Stream;
else if (m_noStream)
m_mode = ExecMode::Standard;
else
m_mode = ExecMode::PreferStream;
}
bool PipelineKernel::isStagePrefix(const std::string& stage)
{
return Kernel::isStagePrefix(stage) || stage == "stage";
}
void PipelineKernel::addSwitches(ProgramArgs& args)
{
args.add("input,i", "Input filename", m_inputFile).setOptionalPositional();
args.add("pipeline-serialization", "Output file for pipeline serialization",
m_pipelineFile);
args.add("validate", "Validate the pipeline (including serialization), "
"but do not write points", m_validate);
args.add("progress",
"Name of file or FIFO to which stages should write progress "
"information. The file/FIFO must exist. PDAL will not create "
"the progress file.",
m_progressFile);
args.add("pointcloudschema", "dump PointCloudSchema XML output",
m_PointCloudSchemaOutput).setHidden();
args.add("stdin,s", "Read pipeline from standard input", m_usestdin);
args.add("stream", "Run in stream mode. Error if not streamable.",
m_stream);
args.add("nostream", "Run in standard mode.", m_noStream);
args.add("metadata", "Metadata filename", m_metadataFile);
}
int PipelineKernel::execute()
{
if (!Utils::fileExists(m_inputFile))
throw pdal_error("file not found: " + m_inputFile);
if (m_progressFile.size())
{
m_progressFd = Utils::openProgress(m_progressFile);
m_manager.setProgressFd(m_progressFd);
}
if (m_validate)
{
NL::json root;
// Validate the options of the pipeline we were
// given, and once we succeed, we're done
try
{
m_manager.readPipeline(m_inputFile);
m_manager.prepare();
root["valid"] = true;
root["error_detail"] = "";
root["streamable"] = m_manager.pipelineStreamable();
}
catch (pdal::pdal_error const& e)
{
root["valid"] = false;
root["error_detail"] = e.what();
root["streamable"] = false;
}
Utils::closeProgress(m_progressFd);
std::cout << root.dump(4) << "\n";
return 0;
}
m_manager.readPipeline(m_inputFile);
if (m_manager.execute(m_mode).m_mode == ExecMode::None)
throw pdal_error("Couldn't run pipeline in requested execution mode.");
if (m_metadataFile.size())
{
std::ostream *out = Utils::createFile(m_metadataFile, false);
if (!out)
throw pdal_error("Can't open file '" + m_metadataFile +
"' for metadata output.");
Utils::toJSON(m_manager.getMetadata(), *out);
Utils::closeFile(out);
}
if (m_pipelineFile.size())
PipelineWriter::writePipeline(m_manager.getStage(), m_pipelineFile);
if (m_PointCloudSchemaOutput.size() > 0)
{
#ifdef PDAL_HAVE_LIBXML2
XMLSchema schema(m_manager.pointTable().layout());
std::ostream *out = Utils::createFile(m_PointCloudSchemaOutput);
std::string xml(schema.xml());
out->write(xml.c_str(), xml.size());
Utils::closeFile(out);
#else
std::cerr << "libxml2 support not available, no schema is produced" <<
std::endl;
#endif
}
Utils::closeProgress(m_progressFd);
return 0;
}
} // pdal
|