[Liblas-commits] hg: add add-vlr and delete-vlr operations to the kernel, general...

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Sep 28 16:31:30 EDT 2010


changeset f2c9b18e8e37 in /Volumes/Data/www/liblas.org/hg
details: http://hg.liblas.orghg?cmd=changeset;node=f2c9b18e8e37
summary: add add-vlr and delete-vlr operations to the kernel, generalize ReadSQL into a generic utility

diffstat:

 apps/las2oci.cpp             |    6 +-
 apps/laskernel.cpp           |  164 ++++++++++++++++++++++++++++++++++++++++++-
 apps/laskernel.hpp           |   11 ++-
 apps/oci_util.hpp            |    2 -
 include/liblas/lasheader.hpp |    2 +-
 src/detail/writer/header.cpp |    2 +-
 src/lasheader.cpp            |   52 +++++--------
 src/lasvariablerecord.cpp    |    5 +
 8 files changed, 203 insertions(+), 41 deletions(-)

diffs (truncated from 376 to 300 lines):

diff -r f5c582719379 -r f2c9b18e8e37 apps/las2oci.cpp
--- a/apps/las2oci.cpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/apps/las2oci.cpp	Tue Sep 28 15:31:22 2010 -0500
@@ -917,7 +917,7 @@
             std::string sql = vm["pre-sql"].as< string >();
             bool used_file = false;
             try {
-                pre_sql = ReadSQLData(sql);
+                pre_sql = TryReadFileData(sql);
                 used_file = true;
             } catch (std::runtime_error const& e) {
                 boost::ignore_unused_variable_warning(e);
@@ -935,7 +935,7 @@
             std::string sql = vm["post-sql"].as< string >();
             bool used_file = false;
             try {
-                post_sql = ReadSQLData(sql);
+                post_sql = TryReadFileData(sql);
                 used_file = true;
             } catch (std::runtime_error const& e) {
                 boost::ignore_unused_variable_warning(e);
@@ -953,7 +953,7 @@
             std::string sql = vm["pre-block-sql"].as< string >();
             bool used_file = false;
             try {
-                pre_block_sql = ReadSQLData(sql);
+                pre_block_sql = TryReadFileData(sql);
                 used_file = true;
             } catch (std::runtime_error const& e) {
                 boost::ignore_unused_variable_warning(e);
diff -r f5c582719379 -r f2c9b18e8e37 apps/laskernel.cpp
--- a/apps/laskernel.cpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/apps/laskernel.cpp	Tue Sep 28 15:31:22 2010 -0500
@@ -1,6 +1,63 @@
 
 #include "laskernel.hpp"
 
+std::istream* OpenInput(std::string filename, bool bEnd) 
+{
+    std::ios::openmode mode = std::ios::in | std::ios::binary;
+    if (bEnd == true) {
+        mode = mode | std::ios::ate;
+    }
+    std::istream* istrm;
+    if (compare_no_case(filename.c_str(),"STDIN",5) == 0)
+    {
+        istrm = &std::cin;
+    }
+    else 
+    {
+        istrm = new std::ifstream(filename.c_str(), mode);
+    }
+    
+    if (!istrm->good())
+    {
+        delete istrm;
+        throw std::runtime_error("Reading stream was not able to be created");
+    }
+    return istrm;
+}
+
+std::string TryReadFileData(std::string filename)
+{
+    std::vector<char> data = TryReadRawFileData(filename);
+    return std::string(data.front(), (std::size_t) data.size());
+}
+
+std::vector<char> TryReadRawFileData(std::string filename)
+{
+    std::istream* infile = OpenInput(filename.c_str(), true);
+    std::ifstream::pos_type size;
+    // char* data;
+    std::vector<char> data;
+    if (infile->good()){
+        size = infile->tellg();
+        data.resize(size);
+        // data = new char [size];
+        infile->seekg (0, std::ios::beg);
+        infile->read (&data.front(), size);
+        // infile->close();
+
+        // delete[] data;
+        delete infile;
+        return data;
+    } 
+    else 
+    {   
+        delete infile;
+        return data;
+    }
+}
+
+
+
 bool IsDualRangeFilter(std::string parse_string) 
 {
 
@@ -119,6 +176,8 @@
         ("min-offset", po::value<bool>()->zero_tokens(), "Set the offset of the header to the minimums of all values in the file.  Note that this requires multiple read passes through the file to achieve.")
         ("file-creation", po::value< std::vector<string> >()->multitoken(), "Set the header's day/year.  Specify either as \"1 2010\" for the first day of 2010, or as \"now\" to specify the current day/year")
         ("add-schema", po::value<bool>()->zero_tokens(), "Add the liblas.org schema VLR record to the file.")
+        ("delete-vlr", po::value<std::vector<std::string> >()->multitoken(), "Removes VLRs with the given name and id combination. --delete-vlr LASF_Projection 34737")
+        ("add-vlr", po::value<std::vector<std::string> >()->multitoken(), "Add VLRs with the given name and id combination. --add-vlr hobu 1234 \"Description of the VLR\" \"filename.ext\"")
 
     ;
     
@@ -749,7 +808,110 @@
         liblas::VariableRecord vlr = header.GetSchema().GetVLR();
         header.AddVLR(vlr);
     }
-    
+
+    if (vm.count("delete-vlr")) 
+    {
+        std::vector<std::string> vlrs = vm["delete-vlr"].as< std::vector<std::string> >();
+        
+        
+        if (vlrs.size() % 2 != 0) {
+            ostringstream err;
+            err << "VLR descriptions must be in pairs of 2";
+            throw std::runtime_error(err.str());
+        }
+        ostringstream oss;
+        
+        for (std::vector<std::string>::const_iterator i = vlrs.begin();
+             i != vlrs.end();
+             i++) 
+            {
+                oss << *i << " ";
+            }
+        if (verbose)
+        {
+
+                std::cout << "Deleting VLRs with the values: " << oss.str() << std::endl;
+        }
+        
+        for (std::vector<std::string>::size_type i = 0; i < vlrs.size(); i=i+2)
+        {
+            header.DeleteVLRs(vlrs[i], atoi(vlrs[i+1].c_str()));
+        }
+    }
+
+    if (vm.count("add-vlr")) 
+    {
+        std::vector<std::string> vlrs = vm["add-vlr"].as< std::vector<std::string> >();
+        
+        
+        if (vlrs.size() < 3) {
+            ostringstream err;
+            err << "VLR additions must be at least 3 arguments -- --add-vlr NAME 42 \"filename.ext\"";
+            throw std::runtime_error(err.str());
+        }
+        if (vlrs.size() > 4)
+            throw std::runtime_error("Only one VLR may be added at a time");
+
+        ostringstream oss;
+        
+        for (std::vector<std::string>::const_iterator i = vlrs.begin();
+             i != vlrs.end();
+             i++) 
+            {
+                oss << *i << " ";
+            }
+
+        
+        liblas::VariableRecord v;
+        v.SetUserId(vlrs[0]);
+        v.SetRecordId(atoi(vlrs[1].c_str()));
+        
+        std::vector<boost::uint8_t> data;
+        
+        std::string data_or_filename;
+        if (vlrs.size() == 4){
+
+            v.SetDescription(vlrs[2]);
+            data_or_filename = vlrs[3];
+        } else {
+            data_or_filename = vlrs[2];
+        } 
+
+        try {
+            std::vector<char> d;
+            d = TryReadRawFileData(data_or_filename);
+            for (std::vector<char>::const_iterator i = d.begin(); i != d.end(); ++i) 
+            {
+                data.push_back(*i);
+            }
+            
+        } catch (std::runtime_error const& ) {
+            std::string::const_iterator i;
+            for (i = data_or_filename.begin(); i != data_or_filename.end(); ++i)
+            {
+                data.push_back(*i);
+            }
+        }    
+
+        if (data.size() > std::numeric_limits<boost::uint16_t>::max()) {
+            std::ostringstream oss;
+            oss << "This VLR with length " << data.size() << " does" 
+                << " not fit within the maximum VLR size of " 
+                << std::numeric_limits<boost::uint16_t>::max();
+            throw std::runtime_error(oss.str());
+        }
+
+        if (verbose)
+        {
+
+                std::cout << "Adding VLRs with the values: " << oss.str() << std::endl;
+        }
+
+
+        v.SetData(data);
+        v.SetRecordLength(data.size());
+        header.AddVLR(v);
+    }   
     return transforms;
 }
 
diff -r f5c582719379 -r f2c9b18e8e37 apps/laskernel.hpp
--- a/apps/laskernel.hpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/apps/laskernel.hpp	Tue Sep 28 15:31:22 2010 -0500
@@ -85,7 +85,14 @@
 std::vector<liblas::FilterPtr> GetFilters(po::variables_map vm, bool verbose);
 std::vector<liblas::TransformPtr> GetTransforms(po::variables_map vm, bool verbose, liblas::Header& header);
 
-// boost::property_tree::ptree SummarizePoints(liblas::Reader& reader );
-// boost::property_tree::ptree SummarizeHeader(liblas::Header const& header );
+#ifdef _WIN32
+#define compare_no_case(a,b,n)  _strnicmp( (a), (b), (n) )
+#else
+#define compare_no_case(a,b,n)  strncasecmp( (a), (b), (n) )
+#endif
+
+std::istream* OpenInput(std::string filename, bool bEnd);
+std::string TryReadFileData(std::string filename);
+std::vector<char> TryReadRawFileData(std::string filename);
 
 #endif // LIBLAS_ITERATOR_HPP_INCLUDED
diff -r f5c582719379 -r f2c9b18e8e37 apps/oci_util.hpp
--- a/apps/oci_util.hpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/apps/oci_util.hpp	Tue Sep 28 15:31:22 2010 -0500
@@ -14,8 +14,6 @@
 #include <boost/cstdint.hpp>
 #include <boost/concept_check.hpp>
 
-std::istream* OpenInput(std::string filename, bool bEnd);
-std::string ReadSQLData(std::string filename);
 
 bool EnableTracing(OWConnection* connection);
 bool IsGeographic(OWConnection* connection, long srid);
diff -r f5c582719379 -r f2c9b18e8e37 include/liblas/lasheader.hpp
--- a/include/liblas/lasheader.hpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/include/liblas/lasheader.hpp	Tue Sep 28 15:31:22 2010 -0500
@@ -316,7 +316,7 @@
 
     /// Removes a VLR from the the header.
     void DeleteVLR(boost::uint32_t index);
-    void DeleteVLR(std::string const& name, boost::uint16_t id);
+    void DeleteVLRs(std::string const& name, boost::uint16_t id);
 
     /// Rewrite variable-length record with georeference infomation, if available.
     void SetGeoreference();
diff -r f5c582719379 -r f2c9b18e8e37 src/detail/writer/header.cpp
--- a/src/detail/writer/header.cpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/src/detail/writer/header.cpp	Tue Sep 28 15:31:22 2010 -0500
@@ -131,7 +131,7 @@
         if (m_header.GetSchema().IsCustom()) {
             
             // Wipe any schema-related VLRs we might have, as this is now out of date.
-            m_header.DeleteVLR("liblas", 7);
+            m_header.DeleteVLRs("liblas", 7);
         
             VariableRecord v = m_header.GetSchema().GetVLR();
             std::cout <<  m_header.GetSchema()<< std::endl;
diff -r f5c582719379 -r f2c9b18e8e37 src/lasheader.cpp
--- a/src/lasheader.cpp	Tue Sep 28 11:07:33 2010 -0500
+++ b/src/lasheader.cpp	Tue Sep 28 15:31:22 2010 -0500
@@ -47,6 +47,7 @@
 #include <liblas/guid.hpp>
 // boost
 #include <boost/cstdint.hpp>
+#include <boost/lambda/lambda.hpp>
 //std
 #include <algorithm>
 #include <fstream>
@@ -65,6 +66,7 @@
 char const* const Header::SystemIdentifier = "libLAS";
 char const* const Header::SoftwareIdentifier = "libLAS 1.2";
 
+
 Header::Header() : m_schema(ePointFormat0)
 {
     Init();
@@ -592,39 +594,27 @@
     SetScale(0.01, 0.01, 0.01);
 }
 
-void Header::DeleteVLR(std::string const& name, boost::uint16_t id)
+bool SameVLRs(std::string const& name, boost::uint16_t id, liblas::VariableRecord const& record)
 {


More information about the Liblas-commits mailing list