From d97a978fd4d556885c7b34249ccac0886b552eb7 Mon Sep 17 00:00:00 2001 From: Howard Butler Date: Wed, 8 Mar 2017 14:55:24 -0600 Subject: [PATCH] more pdal metadata VLR WIP -- a test too --- io/LasReader.cpp | 12 +++++++++- io/LasWriter.cpp | 31 +++++++++++++++++--------- io/LasWriter.hpp | 2 +- test/unit/io/LasWriterTest.cpp | 40 ++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 12 deletions(-) diff --git a/io/LasReader.cpp b/io/LasReader.cpp index dd3d2ef354..9594c7d7ef 100644 --- a/io/LasReader.cpp +++ b/io/LasReader.cpp @@ -360,12 +360,22 @@ void LasReader::extractHeaderMetadata(MetadataNode& forward, MetadataNode& m) m_header.pointCount(), "This field contains the total " "number of point records within the file."); + // PDAL metadata VLR LasVLR *vlr = m_header.findVlr("PDAL", 12); if (vlr) { const char *pos = vlr->data(); size_t size = vlr->dataLen(); - m.addWithType("pdal", std::string(pos, size), "json", "PDAL Processing Metadata"); + m.addWithType("pdal_metadata", std::string(pos, size), "json", "PDAL Processing Metadata"); + } + // + // PDAL pipeline VLR + vlr = m_header.findVlr("PDAL", 13); + if (vlr) + { + const char *pos = vlr->data(); + size_t size = vlr->dataLen(); + m.addWithType("pdal_pipeline", std::string(pos, size), "json", "PDAL Processing Pipeline"); } } diff --git a/io/LasWriter.cpp b/io/LasWriter.cpp index f1e885df85..ec3ced60c6 100644 --- a/io/LasWriter.cpp +++ b/io/LasWriter.cpp @@ -244,6 +244,9 @@ void LasWriter::fillForwardList() void LasWriter::readyTable(PointTableRef table) { m_forwardMetadata = table.privateMetadata("lasforward"); + MetadataNode m = table.metadata(); + if(m_writePDALMetadata) + setPDALVLRs(m); setExtraBytesVlr(); } @@ -265,9 +268,6 @@ void LasWriter::prepOutput(std::ostream *outStream, const SpatialReference& srs) { - MetadataNode m = getMetadata(); - if(m_writePDALMetadata) - setPDALVlrFromMetadata(m); // Use stage SRS if provided. m_srs = getSpatialReference().empty() ? srs : getSpatialReference(); @@ -349,18 +349,29 @@ MetadataNode LasWriter::findVlrMetadata(MetadataNode node, return node.find(pred); } -void LasWriter::setPDALVlrFromMetadata(MetadataNode& forward) +void LasWriter::setPDALVLRs(MetadataNode& forward) { std::ostringstream ostr; - Utils::toJSON(getMetadata(), ostr); - + Utils::toJSON(forward, ostr); std::string json = ostr.str(); - std::vector data;//(LasVLR::MAX_DATA_SIZE, 0); - data.resize(json.size()); - std::copy(json.begin(), json.end(), data.begin()); + auto store = [this](std::string json, int recordId, std::string description) + { + std::vector data; + data.resize(json.size()); + std::copy(json.begin(), json.end(), data.begin()); + addVlr("PDAL", recordId, description, data); + + + }; + + store(ostr.str(), 12, "PDAL metadata"); + ostr.str(""); + + PipelineWriter::writePipeline(this, ostr); + + store(ostr.str(), 13, "PDAL pipeline"); - addVlr("PDAL", 12, "pdal metadata", data); } /// Set VLRs from metadata for forwarded info. diff --git a/io/LasWriter.hpp b/io/LasWriter.hpp index 71cbc19156..94cc11da06 100644 --- a/io/LasWriter.hpp +++ b/io/LasWriter.hpp @@ -151,7 +151,7 @@ class PDAL_DLL LasWriter : public FlexWriter void writeLasZipBuf(char *data, size_t pointLen, point_count_t numPts); void writeLazPerfBuf(char *data, size_t pointLen, point_count_t numPts); void setVlrsFromMetadata(MetadataNode& forward); - void setPDALVlrFromMetadata(MetadataNode& m); + void setPDALVLRs(MetadataNode& m); MetadataNode findVlrMetadata(MetadataNode node, uint16_t recordId, const std::string& userId); void setExtraBytesVlr(); diff --git a/test/unit/io/LasWriterTest.cpp b/test/unit/io/LasWriterTest.cpp index 93cc6a616b..17d37edc7e 100644 --- a/test/unit/io/LasWriterTest.cpp +++ b/test/unit/io/LasWriterTest.cpp @@ -716,6 +716,46 @@ TEST(LasWriterTest, fix1063_1064_1065) EXPECT_EQ(ref.getWKT(), wkt); } +TEST(LasWriterTest, pdal_metadata) +{ + PointTable table; + + std::string infile(Support::datapath("las/1.2-with-color.las")); + std::string outfile(Support::temppath("simple.las")); + + // remove file from earlier run, if needed + FileUtils::deleteFile(outfile); + + Options readerOpts; + readerOpts.add("filename", infile); + + Options writerOpts; + writerOpts.add("pdal_metadata", true); + writerOpts.add("filename", outfile); + + LasReader reader; + reader.setOptions(readerOpts); + + LasWriter writer; + writer.setOptions(writerOpts); + writer.setInput(reader); + writer.prepare(table); + writer.execute(table); + + PointTable t2; + Options readerOpts2; + readerOpts2.add("filename", outfile); + LasReader reader2; + reader2.setOptions(readerOpts2); + + reader2.prepare(t2); + reader2.execute(t2); + + EXPECT_EQ(reader2.getMetadata().children("pdal_metadata").size(), 1UL); + EXPECT_EQ(reader2.getMetadata().children("pdal_pipeline").size(), 1UL); + +} + /** namespace {