[Liblas-commits] hg: add a new SetHeadercompression method to the kernel so other...

liblas-commits at liblas.org liblas-commits at liblas.org
Wed Jan 12 11:50:58 EST 2011


details:   http://hg.liblas.orghg/rev/c9066685dd4f
changeset: 2737:c9066685dd4f
user:      Howard Butler <hobu.inc at gmail.com>
date:      Wed Jan 12 10:50:13 2011 -0600
description:
add a new SetHeadercompression method to the kernel so other apps can use the same logic
Subject: hg: add the ability for lasblock to read compressed data, write to stdout, and write compressed tiles using --write-points

details:   http://hg.liblas.orghg/rev/e3efe5548e3b
changeset: 2738:e3efe5548e3b
user:      Howard Butler <hobu.inc at gmail.com>
date:      Wed Jan 12 10:50:47 2011 -0600
description:
add the ability for lasblock to read compressed data, write to stdout, and write compressed tiles using --write-points

diffstat:

 apps/las2las.cpp   |   57 ++--------
 apps/lasblock.cpp  |  262 ++++++++++++++++++++++++++++++++--------------------
 apps/laskernel.cpp |   40 ++++++++
 apps/laskernel.hpp |    1 +
 4 files changed, 217 insertions(+), 143 deletions(-)

diffs (truncated from 501 to 300 lines):

diff -r 8dc111adfbfa -r e3efe5548e3b apps/las2las.cpp
--- a/apps/las2las.cpp	Wed Jan 12 08:31:50 2011 -0800
+++ b/apps/las2las.cpp	Wed Jan 12 10:50:47 2011 -0600
@@ -26,19 +26,19 @@
 
 
 WriterPtr start_writer(   std::ostream*& ofs, 
-                          std::string const& output, 
-                          liblas::Header const& header)
+                      std::string const& output, 
+                      liblas::Header const& header)
 {
-    ofs = WriterFactory::FileCreate(output);
-    if (!ofs)
-    {
-        std::ostringstream oss;
-        oss << "Cannot create " << output << "for write.  Exiting...";
-        throw std::runtime_error(oss.str());
-    }
+ofs = WriterFactory::FileCreate(output);
+if (!ofs)
+{
+    std::ostringstream oss;
+    oss << "Cannot create " << output << "for write.  Exiting...";
+    throw std::runtime_error(oss.str());
+}
 
-    WriterPtr writer( new liblas::Writer(*ofs, header));
-    return writer;
+WriterPtr writer( new liblas::Writer(*ofs, header));
+return writer;
 }
 
 bool process(   std::istream& ifs,
@@ -325,44 +325,13 @@
         // Transforms alter our header as well.  Setting scales, offsets, etc.
         transforms = GetTransforms(vm, verbose, header);
 
-        // our policy for determining the output format is this:
-        //   if -compressed given, use LAZ
-        //   else if we see .las or .laz, use LAS or LAZ (resp.)
-        //   else just use LAS
-        WriterFactory::FileType output_file_type = WriterFactory::FileType_Unknown;
         if (bCompressed)
         {
-            output_file_type = WriterFactory::FileType_LAZ;
+            header.SetCompressed(true);
         }
         else 
         {
-            WriterFactory::FileType ext_type = WriterFactory::InferFileTypeFromExtension(output);
-            if (ext_type != WriterFactory::FileType_Unknown)
-            {
-                output_file_type = ext_type;
-            }
-            else
-            {
-                output_file_type = WriterFactory::FileType_LAS;
-            }
-        }
-
-        switch (output_file_type)
-        {
-        case WriterFactory::FileType_LAS:
-            header.SetCompressed(false);
-            break;
-        case WriterFactory::FileType_LAZ:
-#ifdef HAVE_LASZIP
-            header.SetCompressed(true);
-#else
-            throw configuration_error("LASzip compression support not enabled in this libLAS configuration.");
-#endif
-            break;
-        case WriterFactory::FileType_Unknown:
-        default:
-            throw liblas_error("Unknown output file type");
-            break;
+            SetHeaderCompression(header, output);
         }
 
         std::istream* ifs = ReaderFactory::FileOpen(input);
diff -r 8dc111adfbfa -r e3efe5548e3b apps/lasblock.cpp
--- a/apps/lasblock.cpp	Wed Jan 12 08:31:50 2011 -0800
+++ b/apps/lasblock.cpp	Wed Jan 12 10:50:47 2011 -0600
@@ -41,24 +41,42 @@
 typedef boost::shared_ptr<liblas::CoordinateSummary> SummaryPtr;
 typedef boost::shared_ptr<std::ofstream> OStreamPtr;
 
-WriterPtr start_writer(   OStreamPtr strm, 
-                          std::string const& output, 
-                          liblas::Header const& header)
+
+liblas::Writer* start_writer(   std::ostream*& ofs, 
+                      std::string const& output, 
+                      liblas::Header const& header)
 {
-    
-    if (!liblas::Create(*strm, output.c_str()))
-    {
-        std::ostringstream oss;
-        oss << "Cannot create " << output << "for write.  Exiting...";
-        throw std::runtime_error(oss.str());
-    }
+ofs = liblas::WriterFactory::FileCreate(output);
+if (!ofs)
+{
+    std::ostringstream oss;
+    oss << "Cannot create " << output << "for write.  Exiting...";
+    throw std::runtime_error(oss.str());
+}
 
-    WriterPtr writer( new liblas::Writer(*strm, header));
-    return writer;
-    
+return new liblas::Writer(*ofs, header);
 }
 
 
+
+// WriterPtr start_writer(   OStreamPtr strm, 
+//                           std::string const& output, 
+//                           liblas::Header const& header)
+// {
+//     
+//     if (!liblas::Create(*strm, output.c_str()))
+//     {
+//         std::ostringstream oss;
+//         oss << "Cannot create " << output << "for write.  Exiting...";
+//         throw std::runtime_error(oss.str());
+//     }
+// 
+//     WriterPtr writer( new liblas::Writer(*strm, header));
+//     return writer;
+//     
+// }
+
+
 using namespace liblas;
 
 void OutputHelp( std::ostream & oss, po::options_description const& options)
@@ -80,7 +98,8 @@
 void write_tiles(std::string& output, 
                  liblas::chipper::Chipper& c, 
                  liblas::Reader& reader, 
-                 bool verbose)
+                 bool verbose,
+                 bool bCompressed)
 {
 
 
@@ -91,6 +110,9 @@
     
     liblas::Header header = reader.GetHeader();
     
+    if (bCompressed)
+        header.SetCompressed(true);
+    
     std::string::size_type dot_pos = output.find_first_of(".");
     out = output.substr(0, dot_pos);
     
@@ -103,16 +125,20 @@
     for ( boost::uint32_t i = 0; i < c.GetBlockCount(); ++i )
     {
         std::ostringstream name;
-        name << out << "-" << i <<".las";
+        name << out << "-" << i;
+        if (bCompressed)
+            name << ".laz";
+        else
+            name << ".las";
         SummaryPtr summary(new::liblas::CoordinateSummary);        
 
         {
-            OStreamPtr ofs(new std::ofstream);
-
+            std::ostream* ofs;
+            
             const liblas::chipper::Block& b = c.GetBlock(i);
             header.SetExtent(b.GetBounds());
         
-            WriterPtr writer = start_writer(ofs, name.str(), header);
+            liblas::Writer* writer = start_writer(ofs, name.str(), header);
 
             std::vector<boost::uint32_t> ids = b.GetIDs();
         
@@ -127,7 +153,11 @@
                     writer->WritePoint(p);
                 }
             }
-
+            
+            if (writer != 0)
+                delete writer;
+            if (ofs != 0)
+                delete ofs;
             
         }
         
@@ -145,9 +175,17 @@
                  liblas::chipper::Chipper& c, 
                  liblas::Reader& /*reader*/, 
                  long precision, 
-                 bool verbose)
+                 bool verbose,
+                 bool bUseStdout)
 {
-    std::ofstream out(output.c_str());
+    std::ostream* out;
+    if (bUseStdout) 
+    {
+        out = &std::cout;
+    } else
+    {
+        out = static_cast<std::ostream*>(new std::ofstream(output.c_str()));
+    }
         
     boost::uint32_t num_blocks = c.GetBlockCount();
     
@@ -161,27 +199,29 @@
         const liblas::chipper::Block& b = c.GetBlock(i);
 
         std::vector<boost::uint32_t> ids = b.GetIDs();
-        out << i << " " << ids.size() << " ";
+        *out << i << " " << ids.size() << " ";
         
-        out.setf(std::ios::fixed,std::ios::floatfield);
-        out.precision(precision);
+        out->setf(std::ios::fixed,std::ios::floatfield);
+        out->precision(precision);
         liblas::Bounds<double> const& bnd = b.GetBounds();
-        out << (bnd.min)(0) << " " << (bnd.min)(1) << " " << (bnd.max)(0) << " " <<  (bnd.max)(1) << " " ;
+        *out << (bnd.min)(0) << " " << (bnd.min)(1) << " " << (bnd.max)(0) << " " <<  (bnd.max)(1) << " " ;
         
         for ( boost::uint32_t pi = 0; pi < ids.size(); ++pi )
         {
-            out << ids[pi] << " ";
+            *out << ids[pi] << " ";
         }
 
-        out << std::endl;
+        *out << std::endl;
 
         // Set back to writing decimals
-        out.setf(std::ios::dec);
+        out->setf(std::ios::dec);
 
         if (verbose)
             term_progress(std::cout, (prog + 1) / static_cast<double>(num_blocks));
         prog++;
     }
+    if (out != 0 && !bUseStdout)
+        delete out;
 }
 int main(int argc, char* argv[])
 {
@@ -192,89 +232,113 @@
     long precision = 8;
     bool verbose = false;
     bool tiling = false;
+    bool bCompressed = false;
+    bool bUseStdout = false;
 
+    try
+    {
+        po::options_description desc("Allowed lasblock options");
+        po::positional_options_description p;
+        p.add("input", 1);
+        p.add("output", 1);
 
-    po::options_description desc("Allowed lasblock options");
-    po::positional_options_description p;
-    p.add("input", 1);
-    p.add("output", 1);
+        desc.add_options()
+            ("help,h", "Produce this help message")
+            ("capacity,c", po::value<long>(&capacity)->default_value(3000), "Number of points to nominally put into each block (note that this number will not be exact)")
+            ("precision,p", po::value<long>(&precision)->default_value(8), "Number of decimal points to write for each bbox")
+            ("stdout", po::value<bool>(&bUseStdout)->zero_tokens()->implicit_value(true), "Output data to stdout")
+            ("write-points", po::value<bool>(&tiling)->zero_tokens()->implicit_value(true), "Write .las files for each block instead of an index file")
+            ("compressed,c", po::value<bool>(&bCompressed)->zero_tokens()->implicit_value(true), "Produce .laz compressed data for --write-points tiles")
 
-    desc.add_options()
-        ("help,h", "Produce this help message")
-        ("capacity,c", po::value<long>(&capacity)->default_value(3000), "Number of points to nominally put into each block (note that this number will not be exact)")
-        ("precision,p", po::value<long>(&precision)->default_value(8), "Number of decimal points to write for each bbox")
-        ("write-points", po::value<bool>(&tiling)->zero_tokens()->implicit_value(true), "Write .las files for each block instead of an index file")
+            ("input,i", po::value< std::string >(), "input LAS file")
+            ("output,o", po::value< std::string >(&output)->default_value(""), "The output .kdx file (defaults to input filename + .kdx)")
+            ("verbose,v", po::value<bool>(&verbose)->zero_tokens(), "Verbose message output")
 
-        ("input,i", po::value< std::string >(), "input LAS file")
-        ("output,o", po::value< std::string >(&output)->default_value(""), "The output .kdx file (defaults to input filename + .kdx)")
-        ("verbose,v", po::value<bool>(&verbose)->zero_tokens(), "Verbose message output")
+        ;
+    
+        po::variables_map vm;        
+        po::store(po::command_line_parser(argc, argv).
+          options(desc).positional(p).run(), vm);
 
-    ;


More information about the Liblas-commits mailing list