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 pickle support to LasHeader with tests. #45

Merged
merged 5 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 54 additions & 1 deletion python/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,60 @@ PYBIND11_MODULE(copclib, m)
.def_readwrite("wave_offset", &las::LasHeader::wave_offset)
.def_readwrite("evlr_offset", &las::LasHeader::evlr_offset)
.def_readwrite("evlr_count", &las::LasHeader::evlr_count)
.def_readwrite("points_by_return_14", &las::LasHeader::points_by_return_14);
.def_readwrite("point_count_14", &las::LasHeader::point_count)
.def_readwrite("points_by_return_14", &las::LasHeader::points_by_return_14)
.def(py::pickle(
[](const las::LasHeader &h) { // __getstate__
/* Return a tuple that fully encodes the state of the object */
return py::make_tuple(h.file_source_id, h.global_encoding, h.GUID(), h.version_major, h.version_minor,
h.SystemIdentifier(), h.GeneratingSoftware(), h.creation_day, h.creation_year,
h.header_size, h.point_offset, h.vlr_count, h.point_format_id,
h.point_record_length, h.point_count, h.points_by_return, h.scale.x, h.scale.y,
h.scale.z, h.offset.x, h.offset.y, h.offset.z, h.max.x, h.min.x, h.max.y, h.min.y,
h.max.z, h.min.z, h.wave_offset, h.evlr_offset, h.evlr_count, h.point_count_14,
h.points_by_return_14);
},
[](py::tuple t) { // __setstate__
if (t.size() != 33)
throw std::runtime_error("Invalid state!");

/* Create a new C++ instance */
las::LasHeader h;
h.file_source_id = t[0].cast<uint16_t>();
h.global_encoding = t[1].cast<uint16_t>();
h.GUID(t[2].cast<std::string>());
h.version_major = t[3].cast<uint8_t>();
h.version_minor = t[4].cast<uint8_t>();
h.SystemIdentifier(t[5].cast<std::string>());
h.GeneratingSoftware(t[6].cast<std::string>());
h.creation_day = t[7].cast<uint16_t>();
h.creation_year = t[8].cast<uint16_t>();
h.header_size = t[9].cast<uint16_t>();
h.point_offset = t[10].cast<uint32_t>();
h.vlr_count = t[11].cast<uint32_t>();
h.point_format_id = t[12].cast<int8_t>();
h.point_record_length = t[13].cast<uint16_t>();
h.point_count = t[14].cast<uint32_t>();
h.points_by_return = t[15].cast<std::array<uint32_t, 5>>();
h.scale.x = t[16].cast<double>();
h.scale.y = t[17].cast<double>();
h.scale.z = t[18].cast<double>();
h.offset.x = t[19].cast<double>();
h.offset.y = t[20].cast<double>();
h.offset.z = t[21].cast<double>();
h.max.x = t[22].cast<double>();
h.min.x = t[23].cast<double>();
h.max.y = t[24].cast<double>();
h.min.y = t[25].cast<double>();
h.max.z = t[26].cast<double>();
h.min.z = t[27].cast<double>();
h.wave_offset = t[28].cast<uint64_t>();
h.evlr_offset = t[29].cast<uint64_t>();
h.evlr_count = t[30].cast<uint32_t>();
h.point_count_14 = t[31].cast<uint64_t>();
h.points_by_return_14 = t[32].cast<std::array<uint64_t, 15>>();
return h;
}));

py::class_<Writer::LasConfig>(m, "LasConfig")
.def(py::init<const int8_t &, const Vector3 &, const Vector3 &>(), py::arg("point_format_id"),
Expand Down
2 changes: 2 additions & 0 deletions src/las/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ LasHeader LasHeader::FromLazPerf(const lazperf::header14 &header)
h.wave_offset = header.wave_offset;
h.evlr_offset = header.evlr_offset;
h.evlr_count = header.evlr_count;
h.point_count_14 = header.point_count_14;
std::copy(std::begin(header.points_by_return_14), std::end(header.points_by_return_14),
std::begin(h.points_by_return_14));
return h;
Expand Down Expand Up @@ -86,6 +87,7 @@ lazperf::header14 LasHeader::ToLazPerf() const
h.wave_offset = wave_offset;
h.evlr_offset = evlr_offset;
h.evlr_count = evlr_count;
h.point_count_14 = point_count_14;

std::copy(std::begin(points_by_return_14), std::end(points_by_return_14), std::begin(h.points_by_return_14));
return h;
Expand Down
36 changes: 36 additions & 0 deletions test/las_header_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pickle
import copclib as copc


Expand Down Expand Up @@ -34,3 +35,38 @@ def test_las_header():
assert las_header.evlr_offset == las_header.evlr_offset
assert las_header.evlr_count == las_header.evlr_count
assert las_header.points_by_return_14 == las_header.points_by_return_14


def test_pickling():

reader = copc.FileReader("../test/data/autzen-classified.copc.laz")
las_header = reader.GetLasHeader()

data = pickle.dumps(las_header, -1)
las_header2 = pickle.loads(data)

assert las_header.file_source_id == las_header2.file_source_id
leo-stan marked this conversation as resolved.
Show resolved Hide resolved
assert las_header.global_encoding == las_header2.global_encoding
assert las_header.guid == las_header2.guid
assert las_header.version_major == las_header2.version_major
assert las_header.version_minor == las_header2.version_minor
assert las_header.system_identifier == las_header2.system_identifier
assert las_header.generating_software == las_header2.generating_software
assert las_header.creation_day == las_header2.creation_day
assert las_header.creation_year == las_header2.creation_year
assert las_header.header_size == las_header2.header_size
assert las_header.point_offset == las_header2.point_offset
assert las_header.vlr_count == las_header2.vlr_count
assert las_header.point_format_id == las_header2.point_format_id
assert las_header.point_record_length == las_header2.point_record_length
assert las_header.point_count == las_header2.point_count
assert las_header.points_by_return == las_header2.points_by_return
assert las_header.scale == las_header2.scale
assert las_header.offset == las_header2.offset
assert las_header.max == las_header2.max
assert las_header.min == las_header2.min
assert las_header.wave_offset == las_header2.wave_offset
assert las_header.evlr_offset == las_header2.evlr_offset
assert las_header.evlr_count == las_header2.evlr_count
assert las_header.point_count_14 == las_header2.point_count_14
assert las_header.points_by_return_14 == las_header2.points_by_return_14