Skip to content

Commit

Permalink
Improve the las reader's file signature error
Browse files Browse the repository at this point in the history
Catch the las header's invalid file signature error and improve the
error message, mostly by including the file name. In order to provide
the filename under more circumstances, set the "filename" option when
using the filename-only las::Reader constructor.

Fixes #277.
  • Loading branch information
gadomski committed May 6, 2014
1 parent 3ec4c10 commit a6e8bbc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/drivers/las/Reader.cpp
Expand Up @@ -86,7 +86,7 @@ Reader::Reader(const Options& options)


Reader::Reader(const std::string& filename)
: ReaderBase(Options::none())
: ReaderBase(Option("filename", filename))
, m_streamFactory(new FilenameStreamFactory(filename))
, m_ownsStreamFactory(true)
{}
Expand Down Expand Up @@ -115,7 +115,23 @@ void Reader::initialize()
std::istream& stream = m_streamFactory->allocate();

LasHeaderReader lasHeaderReader(m_lasHeader, stream);
lasHeaderReader.read(*this, m_schema);
try
{
lasHeaderReader.read(*this, m_schema);
}
catch (const std::invalid_argument& e)
{
// Improve the error message. #277
std::stringstream msg;
std::string filename(getOptions().getValueOrDefault<std::string>("filename", ""));
if (filename.empty())
{
throw e;
}
msg << "Unable to read file " << filename
<< ". It does not have a las file signature.";
throw std::invalid_argument(msg.str());
}

this->setBounds(m_lasHeader.getBounds());
this->setNumPoints(m_lasHeader.GetPointRecordsCount());
Expand Down
25 changes: 25 additions & 0 deletions test/unit/drivers/las/LasReaderTest.cpp
Expand Up @@ -455,4 +455,29 @@ BOOST_AUTO_TEST_CASE(test_no_xyz)
}


BOOST_AUTO_TEST_CASE(testFilenameConstructorSetOption)
{
pdal::drivers::las::Reader reader(Support::datapath("1.2-with-color.las"));
BOOST_CHECK_EQUAL(reader.getOptions().getValueOrDefault<std::string>("filename", ""),
Support::datapath("1.2-with-color.las"));
}


BOOST_AUTO_TEST_CASE(testInvalidFileSignature)
{
pdal::drivers::las::Reader reader(Support::datapath("1.2-with-color.las.wkt"));
try
{
reader.initialize();
}
catch (const std::invalid_argument& e)
{
std::string msg(e.what());
BOOST_CHECK(msg.find("1.2-with-color.las.wkt") != std::string::npos);
return;
}
BOOST_FAIL("reader.initialize() did not throw std::invalid_argument");
}


BOOST_AUTO_TEST_SUITE_END()

0 comments on commit a6e8bbc

Please sign in to comment.