Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass ebVLRs to the writer. #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions cpp/lazperf/writers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ struct basic_file::Private
uint64_t newChunk();
uint64_t firstChunkOffset() const;
bool compressed() const;
bool open(std::ostream& out, const header12& h, uint32_t chunk_size);
bool open(std::ostream& out, const header12& h, uint32_t chunk_size, eb_vlr&& user_vlr = {});
void writePoint(const char *p);
void updateMinMax(const las::point10& p);
void writeHeader();
Expand All @@ -65,6 +65,7 @@ struct basic_file::Private
header14 head14;
std::ostream *f; // Pointer because we don't have a reference target at construction.
std::unique_ptr<OutFileStream> stream;
eb_vlr user_vlr;
};

struct named_file::Private
Expand All @@ -74,7 +75,7 @@ struct named_file::Private
Private(Base *b) : base(b)
{}

void open(const std::string& filename, const named_file::config& c);
void open(const std::string& filename, const named_file::config& c, eb_vlr&& user_vlr = {});

Base *base;
std::ofstream file;
Expand All @@ -99,14 +100,26 @@ struct named_file::Private
// Note that after being read, the table is fixed up to be usable when reading
// points.

bool basic_file::Private::open(std::ostream& out, const header12& h, uint32_t cs)
bool basic_file::Private::open(std::ostream& out, const header12& h, uint32_t cs, eb_vlr&& user_vlr_)
{
if (h.version.major != 1 || h.version.minor < 2 || h.version.minor > 4)
return false;

f = &out;
head12 = h;
chunk_size = cs;
user_vlr = user_vlr_;

if (user_vlr.items.empty()) {
for (int i = 0; i < head14.ebCount(); ++i)
{
eb_vlr::ebfield field;

field.name = "FIELD_" + std::to_string(i);
user_vlr_.addField(field);
}
}

writeHeader();

if (compressed())
Expand Down Expand Up @@ -198,15 +211,6 @@ void basic_file::Private::close()
void basic_file::Private::writeHeader()
{
laz_vlr lazVlr(head14.pointFormat(), head14.ebCount(), chunk_size);
eb_vlr ebVlr;

for (int i = 0; i < head14.ebCount(); ++i)
{
eb_vlr::ebfield field;

field.name = "FIELD_" + std::to_string(i);
ebVlr.addField(field);
}

// Set the version number to 2 in order to write something reasonable.
if (head14.version.minor < 2 || head14.version.minor > 4)
Expand All @@ -224,7 +228,7 @@ void basic_file::Private::writeHeader()
}
if (head14.ebCount())
{
head14.point_offset += (uint32_t)(ebVlr.size() + ebVlr.header().Size);
head14.point_offset += (uint32_t)(user_vlr.size() + user_vlr.header().Size);
head14.vlr_count++;
}

Expand Down Expand Up @@ -255,8 +259,8 @@ void basic_file::Private::writeHeader()
}
if (head14.ebCount())
{
ebVlr.header().write(*f);
ebVlr.write(*f);
user_vlr.header().write(*f);
user_vlr.write(*f);
}
}

Expand Down Expand Up @@ -305,9 +309,9 @@ bool basic_file::compressed() const
return p_->compressed();
}

bool basic_file::open(std::ostream& out, const header12& h, uint32_t chunk_size)
bool basic_file::open(std::ostream& out, const header12& h, uint32_t chunk_size, eb_vlr&& user_vlr)
{
return p_->open(out, h, chunk_size);
return p_->open(out, h, chunk_size, std::move(user_vlr));
}

void basic_file::writePoint(const char *buf)
Expand Down Expand Up @@ -372,7 +376,7 @@ header12 named_file::config::to_header() const
return h;
}

void named_file::Private::open(const std::string& filename, const named_file::config& c)
void named_file::Private::open(const std::string& filename, const named_file::config& c, eb_vlr&& user_vlr)
{
header12 h = c.to_header();

Expand All @@ -381,14 +385,14 @@ void named_file::Private::open(const std::string& filename, const named_file::co
file.open(filename, std::ios::binary | std::ios::trunc);
if (!file.good())
throw error("Couldn't open '" + filename + "' for writing.");
base->open(file, h, c.chunk_size);
base->open(file, h, c.chunk_size, std::move(user_vlr));
}


named_file::named_file(const std::string& filename, const named_file::config& c) :
named_file::named_file(const std::string& filename, const named_file::config& c, eb_vlr&& user_vlr) :
p_(new Private(basic_file::p_.get()))
{
p_->open(filename, c);
p_->open(filename, c, std::move(user_vlr));
}

named_file::~named_file()
Expand Down
5 changes: 3 additions & 2 deletions cpp/lazperf/writers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <memory>

#include "header.hpp"
#include "vlr.hpp"

namespace lazperf
{
Expand All @@ -41,7 +42,7 @@ class basic_file
virtual ~basic_file();

public:
LAZPERF_EXPORT bool open(std::ostream& out, const header12& h, uint32_t chunk_size);
LAZPERF_EXPORT bool open(std::ostream& out, const header12& h, uint32_t chunk_size, eb_vlr&& user_vlr = {});
LAZPERF_EXPORT void writePoint(const char *p);
LAZPERF_EXPORT void close();
LAZPERF_EXPORT uint64_t newChunk();
Expand Down Expand Up @@ -75,7 +76,7 @@ class named_file : public basic_file
header12 to_header() const;
};

LAZPERF_EXPORT named_file(const std::string& filename, const config& c);
LAZPERF_EXPORT named_file(const std::string& filename, const config& c, eb_vlr&& user_vlr = {});
LAZPERF_EXPORT virtual ~named_file();

LAZPERF_EXPORT void close();
Expand Down