[Liblas-commits] r1205 - trunk/apps

liblas-commits at liblas.org liblas-commits at liblas.org
Mon Apr 13 12:11:42 EDT 2009


Author: hobu
Date: Mon Apr 13 12:11:41 2009
New Revision: 1205
URL: http://liblas.org/changeset/1205

Log:
add ts2las

Added:
   trunk/apps/ts2las.cpp
   trunk/apps/ts2las.hpp
Modified:
   trunk/apps/Makefile.am

Modified: trunk/apps/Makefile.am
==============================================================================
--- trunk/apps/Makefile.am	(original)
+++ trunk/apps/Makefile.am	Mon Apr 13 12:11:41 2009
@@ -13,9 +13,10 @@
 lasmerge_SOURCES = lasmerge.c lascommon.c
 las2txt_SOURCES = las2txt.c lascommon.c
 txt2las_SOURCES = txt2las.c
+ts2las_SOURCES = ts2las.cpp
 
 
-bin_PROGRAMS = lasinfo las2las lasmerge las2txt txt2las
+bin_PROGRAMS = lasinfo las2las lasmerge las2txt txt2las ts2las
 
 bin_SCRIPTS = liblas-config
 

Added: trunk/apps/ts2las.cpp
==============================================================================
--- (empty file)
+++ trunk/apps/ts2las.cpp	Mon Apr 13 12:11:41 2009
@@ -0,0 +1,233 @@
+
+#include "ts2las.hpp"
+
+#include <fstream>
+#include <iostream>
+#include <string>
+
+using namespace liblas;
+
+
+std::istream* OpenInput(std::string filename) 
+{
+    std::ios::openmode const mode = std::ios::in | std::ios::binary;
+    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::ostream* OpenOutput(std::string filename)
+{
+    std::ostream* ostrm;
+    std::ios::openmode m;
+    m = std::ios::out | std::ios::binary | std::ios::ate;
+            
+    if (compare_no_case(filename.c_str(),"STOUT",5) == 0)
+    {
+        ostrm = &std::cout;
+    }
+    else 
+    {
+        ostrm = new std::ofstream(filename.c_str(), m);
+    }
+
+    
+    if (!ostrm->good())
+    {
+        delete ostrm;
+        throw std::runtime_error("Writing stream was not able to be created");
+    }
+    
+    return ostrm;
+}
+
+LASHeader CreateHeader(ScanHdr* hdr)
+{
+    LASHeader header;
+    
+    // Checks for time and color values
+    liblas::LASHeader::PointFormat format = liblas::LASHeader::ePointFormat0;
+    
+    if (hdr->Time) {
+        if (hdr->Color) {
+            format = liblas::LASHeader::ePointFormat3;
+        } else {
+            format = liblas::LASHeader::ePointFormat1;
+        }
+    } else if (hdr->Color) {
+        format = liblas::LASHeader::ePointFormat2;
+    } 
+    header.SetVersionMinor(2);
+    header.SetDataFormatId(format);
+    header.SetPointRecordsCount(hdr->PntCnt);
+    header.SetOffset(hdr->OrgX, hdr->OrgY, hdr->OrgZ);
+    std::cout << "offset x: " << header.GetOffsetX() << " offset y: " << header.GetOffsetY()  << " offset z: " <<header.GetOffsetZ() << std::endl;
+    std::cout << "units: " << hdr->Units << std::endl;
+    std::cout << "format: " << format << std::endl;
+    return header;
+}
+bool ReadHeader(ScanHdr* hdr, std::istream* istrm) {
+    
+    try {
+        liblas::detail::read_n(*hdr, *istrm, sizeof(ScanHdr));
+        
+        if (hdr->Tunniste != 970401) return false;
+        if (memcmp(hdr->Magic,"CXYZ",4)) return false;
+        
+        int version = hdr->HdrVersion;
+        if (version == 970404) return true;
+        if (version == 20010129) return true;
+        if (version == 20010712) return true;
+        if (version == 20020715) return true;
+        if (( version > 20020715) && (version < 20051231)) return true;
+        return false;
+    }
+    catch (std::exception const& e)
+    {
+        return false;
+    }    
+
+    return true;
+}
+
+bool WritePoints(LASWriter* writer, std::istream* strm, ScanHdr* hdr) 
+{
+    while (true) {
+        
+        if (hdr->HdrVersion == 20020715) {
+///std::cout << "We have header version" << std::endl;
+            ScanPnt* point = new ScanPnt;
+            try
+            {
+                std::cout << "stream position is: " << strm->tellg() << std::endl;
+
+                detail::read_n(*point, *strm, sizeof(ScanPnt));
+                LASPoint p;
+                p.SetCoordinates(writer->GetHeader(),point->Pnt.x,point->Pnt.y,point->Pnt.z);
+                std::cout << "x: " << point->Pnt.x << " y: "<< point->Pnt.y << " z: " <<point->Pnt.z<< std::endl;
+                std::cout << "x: " << p.GetX() << " y: "<< p.GetY() << " z: " <<p.GetZ()<< std::endl;
+
+                std::cout << "Code: " << point->Code << " Intensity: "<< point->Intensity << std::endl;
+                p.SetClassification(point->Code);
+                p.SetIntensity(point->Intensity);
+                writer->WritePoint(p);
+                std::cout << "stream position is: " << strm->tellg() << std::endl;
+
+                exit(-1);
+               // std::cout << "Wrote point"<< std::endl;
+                
+            }
+            catch (std::out_of_range const& e) // we reached the end of the file
+            {
+                std::cout << "catching out of range error!" ;
+                break;
+            }
+            catch (std::exception const& e) // we reached the end of the file
+            {
+                std::cout << e.what() << std::endl;
+                break;
+            }   
+        }
+        
+        // Reader::FillPoint(record, point);
+        // point.SetCoordinates(header, point.GetX(), point.GetY(), point.GetZ());
+        //     
+        // if (header.GetDataFormatId() == LASHeader::ePointFormat1) {
+        //     detail::read_n(t, m_ifs, sizeof(double));
+        //     point.SetTime(t);
+        // }
+    }
+    return true;    
+
+}
+
+void usage() {}
+
+int main(int argc, char* argv[])
+{
+    int rc = 0;
+
+    std::string input;
+    std::string output;
+    
+    for (int i = 1; i < argc; i++) {
+        if (    strcmp(argv[i],"-h") == 0 ||
+                strcmp(argv[i],"--help") == 0
+            )
+        {
+            usage();
+            exit(0);
+        }
+        else if (   strcmp(argv[i],"--input") == 0  ||
+                    strcmp(argv[i],"-input") == 0   ||
+                    strcmp(argv[i],"-i") == 0       ||
+                    strcmp(argv[i],"-in") == 0
+                )
+        {
+            i++;
+            input = std::string(argv[i]);
+        }
+        else if (   strcmp(argv[i],"--output") == 0  ||
+                    strcmp(argv[i],"--out") == 0     ||
+                    strcmp(argv[i],"-out") == 0     ||
+                    strcmp(argv[i],"-o") == 0       
+                )
+        {
+            i++;
+            output = std::string(argv[i]);
+        }
+        else if (i == argc - 2 && output.empty() && input.empty())
+        {
+            input = std::string(argv[i]);
+        }
+        else if (i == argc - 1 && output.empty() && input.empty())
+        {
+            input = std::string(argv[i]);
+        }
+        else if (i == argc - 1 && output.empty() && input.empty())
+        {
+            output = std::string(argv[i]);
+        }
+        else 
+        {
+            usage();
+            exit(1);
+        }
+    }
+    
+    std::cout << "input: " << input<<  " output: " <<output<<std::endl;
+    
+    ScanHdr* hdr = new ScanHdr;
+    std::istream* istrm = OpenInput(input);
+    std::ostream* ostrm = OpenOutput(output);
+    
+    bool success;
+    success = ReadHeader(hdr, istrm);
+    if (!success) 
+    {
+        std::cout<<"header was not read! exiting" << std::cout; exit(1);
+    }
+    
+    std::cout << "stream position is: " << istrm->tellg() << std::endl;
+    LASHeader header = CreateHeader(hdr);
+    LASWriter* writer = new LASWriter(*ostrm, header);
+    
+     success = WritePoints(writer, istrm, hdr);
+    
+    std::cout << "Point Count: " << hdr->PntCnt <<std::endl;
+    std::cout << "success: " << success << std::endl;
+    return rc;
+}
\ No newline at end of file

Added: trunk/apps/ts2las.hpp
==============================================================================
--- (empty file)
+++ trunk/apps/ts2las.hpp	Mon Apr 13 12:11:41 2009
@@ -0,0 +1,199 @@
+#ifndef TS2LAS_HPP_INCLUDED
+
+#include <liblas/cstdint.hpp>
+#include <liblas/detail/utility.hpp>
+
+#include <liblas/laswriter.hpp>
+#include <liblas/laspoint.hpp>
+#include <liblas/lascolor.hpp>
+#include <iostream>
+#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
+
+using namespace liblas;
+
+struct ScanRow
+{
+    ScanRow() :
+        Code(0),
+        Line(0),
+        EchoInt(0),
+        x(0),
+        y(0),
+        z(0)
+    {}
+
+    liblas::uint8_t Code;
+    liblas::uint8_t Line;
+    liblas::uint16_t EchoInt;
+
+    liblas::int32_t x;
+    liblas::int32_t y;
+    liblas::int32_t z;
+
+};
+
+
+struct Point3d
+{
+    Point3d() :
+        x(0),
+        y(0),
+        z(0)
+    {}
+
+    liblas::int32_t x;
+    liblas::int32_t y;
+    liblas::int32_t z;
+
+};
+
+struct ScanPnt
+{
+    ScanPnt() :
+        Code(0),
+        Echo(0),
+        Flag(0),
+        Mark(0),
+        Line(0),
+        Intensity(0)
+    {}
+
+    Point3d Pnt;
+    liblas::uint8_t Code;
+    liblas::uint8_t Echo;
+    liblas::uint8_t Flag;
+    liblas::uint8_t Mark;
+    liblas::uint16_t Line;
+    liblas::uint16_t Intensity;
+
+};
+
+
+struct ScanHdr
+{
+    ScanHdr() :
+        HdrSize(0),
+        HdrVersion(0),
+        Tunniste(0),
+        PntCnt(0),
+        Units(0),
+        OrgX(0),
+        OrgY(0),
+        OrgZ(0),
+        Time(0),
+        Color(0)
+    {}
+
+    liblas::int32_t HdrSize;
+    liblas::int32_t HdrVersion;
+    liblas::int32_t Tunniste;
+    char Magic[4];
+    liblas::int32_t PntCnt;
+    liblas::int32_t Units;
+    double OrgX;
+    double OrgY;
+    double OrgZ;
+    liblas::int32_t Time;
+    liblas::int32_t Color;
+
+    // 
+    // int HdrSize;
+    // int HdrVersion;
+    // int Tunniste;
+    // char Magic[4];
+    // int PntCnt;
+    // int Units;
+    // double OrgX;
+    // double OrgY;
+    // double OrgZ;
+    // int Time;
+    // int Color;
+};
+
+
+namespace liblas { namespace detail {
+
+
+ 
+template <>
+inline void read_n <ScanRow>(ScanRow& dest, std::istream& src, std::streamsize const& num)
+{
+    // TODO: Review and redesign errors handling logic if necessary
+    if (!src)
+        throw std::runtime_error("detail::liblas::read_n<ScanRow> input stream is not readable");
+
+    src.read(detail::as_buffer(dest), num);
+    detail::check_stream_state(src);
+
+    // Fix little-endian
+    LIBLAS_SWAP_BYTES(dest.x);
+    LIBLAS_SWAP_BYTES(dest.y);
+    LIBLAS_SWAP_BYTES(dest.z);
+    LIBLAS_SWAP_BYTES(dest.Code);
+    LIBLAS_SWAP_BYTES(dest.Line);
+    LIBLAS_SWAP_BYTES(dest.EchoInt);
+
+}
+
+template <>
+inline void read_n <ScanPnt>(ScanPnt& dest, std::istream& src, std::streamsize const& num)
+{
+    // TODO: Review and redesign errors handling logic if necessary
+    if (!src)
+        throw std::runtime_error("detail::liblas::read_n<ScanPnt> input stream is not readable");
+
+    std::cout << "ScanPnt stream position is: " << src.tellg() << std::endl;
+    src.read(detail::as_buffer(dest), num);
+    std::cout << "ScanPnt stream position is: " << src.tellg() << std::endl;
+
+    detail::check_stream_state(src);
+
+
+    // Fix little-endian
+    LIBLAS_SWAP_BYTES(dest.Pnt.x);
+    LIBLAS_SWAP_BYTES(dest.Pnt.y);
+    LIBLAS_SWAP_BYTES(dest.Pnt.z);
+    LIBLAS_SWAP_BYTES(dest.Code);
+    LIBLAS_SWAP_BYTES(dest.Echo);
+    LIBLAS_SWAP_BYTES(dest.Flag);
+    LIBLAS_SWAP_BYTES(dest.Mark);
+    LIBLAS_SWAP_BYTES(dest.Line);
+    LIBLAS_SWAP_BYTES(dest.Intensity);
+
+    std::cout << "x: " << dest.Pnt.x << " y: " << dest.Pnt.y << " z: " << dest.Pnt.z <<std::endl; 
+
+}
+
+template <>
+inline void read_n <ScanHdr>(ScanHdr& dest, std::istream& src, std::streamsize const& num)
+{
+    // TODO: Review and redesign errors handling logic if necessary
+    if (!src)
+        throw std::runtime_error("detail::liblas::read_n<ScanHdr> input stream is not readable");
+
+    std::cout << "ScanHdr stream position is: " << src.tellg() << std::endl;
+
+    src.read(detail::as_buffer(dest), num);
+
+    std::cout << "ScanHdr stream position is: " << src.tellg() << std::endl;
+    detail::check_stream_state(src);
+
+    // Fix little-endian
+    LIBLAS_SWAP_BYTES(dest.HdrSize);
+    LIBLAS_SWAP_BYTES(dest.HdrVersion);
+    LIBLAS_SWAP_BYTES(dest.Tunniste);
+    LIBLAS_SWAP_BYTES(dest.Magic);
+    LIBLAS_SWAP_BYTES(dest.PntCnt);
+    LIBLAS_SWAP_BYTES(dest.Units);
+    LIBLAS_SWAP_BYTES(dest.Time);
+    LIBLAS_SWAP_BYTES(dest.Color);
+
+}
+}} // namespace liblas::detail
+
+#endif // TS2LAS_HPP_INCLUDED
+


More information about the Liblas-commits mailing list