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
|
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
* Copyright (c) 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 "TranslateKernel.hpp"
#include <pdal/PipelineWriter.hpp>
#include <pdal/PointTable.hpp>
#include <pdal/PointView.hpp>
#include <pdal/Reader.hpp>
#include <pdal/StageFactory.hpp>
#include <pdal/PipelineReaderJSON.hpp>
#include <pdal/Writer.hpp>
#include <pdal/util/FileUtils.hpp>
#include <memory>
#include <string>
#include <vector>
namespace pdal
{
static StaticPluginInfo const s_info
{
"kernels.translate",
"The Translate kernel allows users to construct a pipeline " \
"consisting of a reader, a writer, and N filter stages. " \
"Any supported stage type can be specified from the command " \
"line, reducing the need to create custom kernels for every " \
"combination.",
"http://pdal.io/apps/translate.html"
};
CREATE_STATIC_KERNEL(TranslateKernel, s_info)
std::string TranslateKernel::getName() const
{
return s_info.name;
}
TranslateKernel::TranslateKernel()
{}
void TranslateKernel::addSwitches(ProgramArgs& args)
{
args.add("input,i", "Input filename", m_inputFile).
setPositional();
args.add("output,o", "Output filename", m_outputFile).
setPositional();
args.add("filter,f", "Filter type", m_filterType).
setOptionalPositional();
args.add("json", "PDAL pipeline from which to extract filters.",
m_filterJSON);
args.add("pipeline,p", "Pipeline output", m_pipelineOutputFile);
args.add("metadata,m", "Dump metadata output to the specified file",
m_metadataFile);
args.add("reader,r", "Reader type", m_readerType);
args.add("writer,w", "Writer type", m_writerType);
args.add("nostream", "Run in standard mode", m_noStream);
args.add("stream", "Run in stream mode. Error if not possible.", m_stream);
}
void TranslateKernel::validateSwitches(ProgramArgs&)
{
if (m_stream && m_noStream)
throw pdal_error("Can't specify both 'stream' and 'nostream' options.");
if (m_stream)
m_mode = ExecMode::Stream;
else if (m_noStream)
m_mode = ExecMode::Standard;
else
m_mode = ExecMode::PreferStream;
}
/*
Build a pipeline from a JSON filter specification.
*/
void TranslateKernel::makeJSONPipeline()
{
std::string json;
if (pdal::FileUtils::fileExists(m_filterJSON))
json = pdal::FileUtils::readFileIntoString(m_filterJSON);
if (json.empty())
json = m_filterJSON;
std::stringstream in(json);
m_manager.readPipeline(in);
std::vector<Stage *> roots = m_manager.roots();
if (roots.size() > 1)
throw pdal_error("Can't process pipeline with more than one root.");
Stage *r(nullptr);
if (roots.size())
r = dynamic_cast<Reader *>(roots[0]);
if (r)
{
StageCreationOptions ops { m_inputFile, m_readerType, nullptr,
Options(), r->tag() };
m_manager.replace(r, &m_manager.makeReader(ops));
}
else
{
r = &m_manager.makeReader(m_inputFile, m_readerType);
if (roots.size())
roots[0]->setInput(*r);
}
std::vector<Stage *> leaves = m_manager.leaves();
if (leaves.size() != 1)
throw pdal_error("Can't process pipeline with more than one "
"terminal stage.");
Stage *w = dynamic_cast<Writer *>(leaves[0]);
if (w)
m_manager.replace(w, &m_manager.makeWriter(m_outputFile, m_writerType));
else
{
// We know we have a leaf because we added a reader.
StageCreationOptions ops { m_outputFile, m_writerType, leaves[0],
Options(), "" }; // These last two args just keep compiler quiet.
m_manager.makeWriter(ops);
}
}
/*
Build a pipeline from filters specified as command-line arguments.
*/
void TranslateKernel::makeArgPipeline()
{
std::string readerType(m_readerType);
if (!readerType.empty() && !Utils::startsWith(readerType, "readers."))
readerType.insert(0, "readers.");
Stage& reader = m_manager.makeReader(m_inputFile, readerType);
Stage* stage = &reader;
// add each filter provided on the command-line,
// updating the stage pointer
for (auto const& f : m_filterType)
{
std::string filter_name(f);
if (!Utils::startsWith(f, "filters."))
filter_name.insert(0, "filters.");
Stage& filter = m_manager.makeFilter(filter_name, *stage);
stage = &filter;
}
std::string writerType(m_writerType);
if (!writerType.empty() && !Utils::startsWith(writerType, "writers."))
writerType.insert(0, "writers.");
m_manager.makeWriter(m_outputFile, writerType, *stage);
}
int TranslateKernel::execute()
{
std::ostream *metaOut(nullptr);
if (m_filterJSON.size() && m_filterType.size())
throw pdal_error("Cannot set both --filter options and --json options");
if (m_metadataFile.size())
{
if (m_pipelineOutputFile.size())
m_log->get(LogLevel::Info) << "Metadata will not be written. "
"'pipeline' option prevents execution.";
else
{
metaOut = FileUtils::createFile(m_metadataFile);
if (! metaOut)
throw pdal_error("Couldn't output metadata output file '" +
m_metadataFile + "'.");
}
}
if (!m_filterJSON.empty())
makeJSONPipeline();
else
makeArgPipeline();
// If we write pipeline output, we don't run, and therefore don't write
if (m_pipelineOutputFile.size() > 0)
{
PipelineWriter::writePipeline(m_manager.getStage(),
m_pipelineOutputFile);
return 0;
}
if (m_manager.execute(m_mode).m_mode == ExecMode::None)
throw pdal_error("Couldn't run translation pipeline in requested "
"execution mode.");
if (metaOut)
{
MetadataNode m = m_manager.getMetadata();
*metaOut << Utils::toJSON(m);
FileUtils::closeFile(metaOut);
}
return 0;
}
} // namespace pdal
|