Skip to content

Commit

Permalink
Fixes for chunk table in no-point LAZ files. (#151)
Browse files Browse the repository at this point in the history
* Allow empty chunk table when reading LAZ file with no points.

* Fix broken chunk table output for no-point LAZ.
  • Loading branch information
vsautin1 committed Aug 23, 2023
1 parent 4ac3dd4 commit c9f2814
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
8 changes: 8 additions & 0 deletions cpp/lazperf/readers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@ void basic_file::Private::parseChunkTable()
if (chunk_table_header.version != 0)
throw error("Bad chunk table. Invalid version.");

if (chunk_table_header.chunk_count == 0)
{
if (pointCount() != 0)
throw error("Missing chunk table.");

return;
}

// Allocate enough room for the chunk table plus one because of the crazy way that
// the chunk table is written. Once it is fixed up, we resize back to the correct size.
chunks.resize(chunk_table_header.chunk_count + 1);
Expand Down
5 changes: 3 additions & 2 deletions cpp/lazperf/writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ bool basic_file::Private::open(std::ostream& out, const header12& h, uint32_t cs

if (compressed())
{
// Seek past the chunk table offset.
out.seekp(sizeof(uint64_t), std::ios_base::cur);
// Reserve 8 bytes for the chunk table offset.
const uint64_t dummy = 0;
out.write(reinterpret_cast<const char*>(&dummy), sizeof(uint64_t));
}
stream.reset(new OutFileStream(out));
return true;
Expand Down
21 changes: 21 additions & 0 deletions cpp/test/io_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -742,4 +742,25 @@ TEST(io_tests, can_read_1_1)
}
}

TEST(io_tests, can_open_no_points_file)
{
for (const std::string filename : { "no-points-1.3.las", "no-points-1.3.laz" })
{
reader::named_file f(testFile(filename));
EXPECT_EQ(f.header().point_count, 0);
}
}

TEST(io_tests, write_laz_with_no_points_and_read_back)
{
const auto file_name = makeTempFileName();

const writer::named_file::config c({ 0.01, 0.01, 0.01 }, { 0.0, 0.0, 0.0 });
writer::named_file out(file_name, c);
out.close();

reader::named_file f(file_name);
EXPECT_EQ(f.header().point_count, 0);
}

} // namespace lazperf
Binary file added cpp/test/raw-sets/no-points-1.3.las
Binary file not shown.
Binary file added cpp/test/raw-sets/no-points-1.3.laz
Binary file not shown.

0 comments on commit c9f2814

Please sign in to comment.