diff --git a/include/pdal/Options.hpp b/include/pdal/Options.hpp index aef2a7115c..682308b59f 100644 --- a/include/pdal/Options.hpp +++ b/include/pdal/Options.hpp @@ -312,6 +312,11 @@ class PDAL_DLL Options return (m_options.size() == 0); } + size_t size() const + { + return m_options.size(); + } + Options const operator+(const Options& rhs) { return Options(*this) += rhs; diff --git a/include/pdal/Stage.hpp b/include/pdal/Stage.hpp index f7f841416c..73bdd99844 100644 --- a/include/pdal/Stage.hpp +++ b/include/pdal/Stage.hpp @@ -86,7 +86,8 @@ class PDAL_DLL Stage { return m_options; } void setOptions(Options options) { m_options = options; } - void addOptions(Options opts) + void addConditionalOptions(const Options& opts); + void addOptions(const Options& opts) { for (const auto& o : opts.getOptions()) m_options.add(o); diff --git a/kernels/tindex/TIndexKernel.cpp b/kernels/tindex/TIndexKernel.cpp index f71ffc26b9..548c890809 100644 --- a/kernels/tindex/TIndexKernel.cpp +++ b/kernels/tindex/TIndexKernel.cpp @@ -441,6 +441,8 @@ void TIndexKernel::mergeFile() } writer->setInput(merge); + applyExtraStageOptionsRecursive(writer); + Options writerOptions; writerOptions.add("filename", m_filespec); writerOptions.add("scale_x", 1e-9); @@ -449,7 +451,7 @@ void TIndexKernel::mergeFile() writerOptions.add("offset_x", "auto"); writerOptions.add("offset_y", "auto"); writerOptions.add("offset_z", "auto"); - writer->setOptions(writerOptions); + writer->addConditionalOptions(writerOptions); PointTable table; diff --git a/src/Stage.cpp b/src/Stage.cpp index 8f21550de8..976c8f80d6 100644 --- a/src/Stage.cpp +++ b/src/Stage.cpp @@ -52,6 +52,18 @@ Stage::Stage() } +/// Only add options if an option with the same name doesn't already exist. +/// +/// \param[in] ops Options to add. +/// +void Stage::addConditionalOptions(const Options& opts) +{ + for (const auto& o : opts.getOptions()) + if (!m_options.hasOption(o.getName())) + m_options.add(o); +} + + void Stage::Construct() { m_debug = false; diff --git a/test/unit/OptionsTest.cpp b/test/unit/OptionsTest.cpp index 671778a319..b001e271ca 100644 --- a/test/unit/OptionsTest.cpp +++ b/test/unit/OptionsTest.cpp @@ -335,3 +335,34 @@ TEST(OptionsTest, metadata) } EXPECT_TRUE(Support::compare_files(goodfile, testfile)); } + +TEST(OptionsTest, conditional) +{ + CropFilter s; + + Options ops; + ops.add("foo", "foo"); + ops.add("bar", "bar"); + ops.add("baz", "baz"); + + s.setOptions(ops); + + Options condOps; + condOps.add("foo", "lose"); + condOps.add("bar", "lose"); + condOps.add("baz", "lose"); + condOps.add("foot", "win"); + condOps.add("barf", "win"); + condOps.add("bazel", "win"); + + s.addConditionalOptions(condOps); + ops = s.getOptions(); + EXPECT_EQ(ops.size(), 6u); + EXPECT_EQ(ops.getValueOrDefault("foo", std::string()), "foo"); + EXPECT_EQ(ops.getValueOrDefault("bar", std::string()), "bar"); + EXPECT_EQ(ops.getValueOrDefault("baz", std::string()), "baz"); + EXPECT_EQ(ops.getValueOrDefault("foot", std::string()), "win"); + EXPECT_EQ(ops.getValueOrDefault("barf", std::string()), "win"); + EXPECT_EQ(ops.getValueOrDefault("bazel", std::string()), "win"); +} +