Skip to content

Commit

Permalink
Merge pull request #336 from gadomski/issue/278-infer-reader-type
Browse files Browse the repository at this point in the history
In a pipeline, infer the reader type from filename in the absence of a type attr
  • Loading branch information
hobu committed May 6, 2014
2 parents e22d4f7 + 8fccb49 commit 3ec4c10
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
7 changes: 5 additions & 2 deletions include/pdal/StageFactory.hpp
Expand Up @@ -87,8 +87,11 @@ class PDAL_DLL StageFactory

// infer the driver to use based on filename extension
// returns "" if no driver found
//
// this may also add on an option to pass to the driver, such as the filename
static std::string inferReaderDriver(const std::string& filename);

// the same as inferReaderDriver(const std::string&), but
// this may also add on an option to pass to the driver, such as the
// filename
static std::string inferReaderDriver(const std::string& filename, pdal::Options& options);

// infer the driver to use based on filename extension
Expand Down
22 changes: 22 additions & 0 deletions src/PipelineReader.cpp
Expand Up @@ -76,6 +76,11 @@ class PipelineReader::StageParserContext
++m_numTypes;
}

int getNumTypes()
{
return m_numTypes;
}

void addStage()
{
++m_numStages;
Expand Down Expand Up @@ -280,6 +285,23 @@ Reader* PipelineReader::parseElement_Reader(const ptree& tree)
context.addType();
}

// If we aren't provided a type, try to infer the type from the filename
// #278
if (context.getNumTypes() == 0)
{
try
{
const std::string filename = options.getValueOrThrow<std::string>("filename");
type = StageFactory::inferReaderDriver(filename);
if (!type.empty())
{
context.addType();
}
}
catch (option_not_found)
{} // noop
}

context.validate();

return m_manager.addReader(type, options);
Expand Down
15 changes: 10 additions & 5 deletions src/StageFactory.cpp
Expand Up @@ -169,13 +169,10 @@ StageFactory::StageFactory()
return;
}

std::string StageFactory::inferReaderDriver(const std::string& filename, pdal::Options& options)

std::string StageFactory::inferReaderDriver(const std::string& filename)
{
std::string ext = boost::filesystem::extension(filename);

pdal::Option& fn = options.getOptionByRef("filename");
fn.setValue<std::string>(filename);

std::map<std::string, std::string> drivers;
drivers["las"] = "drivers.las.reader";
drivers["laz"] = "drivers.las.reader";
Expand All @@ -202,6 +199,14 @@ std::string StageFactory::inferReaderDriver(const std::string& filename, pdal::O
}


std::string StageFactory::inferReaderDriver(const std::string& filename, pdal::Options& options)
{
pdal::Option& fn = options.getOptionByRef("filename");
fn.setValue<std::string>(filename);
return StageFactory::inferReaderDriver(filename);
}


std::string StageFactory::inferWriterDriver(const std::string& filename)
{
std::string ext = boost::filesystem::extension(filename);
Expand Down
13 changes: 13 additions & 0 deletions test/data/pipeline/pipeline_read_notype.xml
@@ -0,0 +1,13 @@
<?xml version="1.0"?>
<Pipeline version="1.0">
<Filter type="filters.crop">
<Option name="bounds">
([0,1000000],[0,1000000],[0,1000000])
</Option>
<Reader>
<Option name="filename">
../1.2-with-color.las
</Option>
</Reader>
</Filter>
</Pipeline>
8 changes: 8 additions & 0 deletions test/unit/drivers/pipeline/PipelineReaderTest.cpp
Expand Up @@ -258,4 +258,12 @@ BOOST_AUTO_TEST_CASE(PipelineReaderTest_MultiOptions)
}


BOOST_AUTO_TEST_CASE(testNoType)
{
PipelineManager manager;
PipelineReader reader(manager);
reader.readPipeline(Support::datapath("pipeline/pipeline_read_notype.xml"));
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 3ec4c10

Please sign in to comment.