File: ts2las.cpp

package info (click to toggle)
liblas 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 7,888 kB
  • ctags: 4,614
  • sloc: cpp: 31,630; xml: 4,195; python: 2,928; ansic: 2,439; cs: 2,411; sh: 143; makefile: 37
file content (380 lines) | stat: -rwxr-xr-x 11,145 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// $Id$
//
// ts2las translates TerraSolid .bin file to ASPRS LAS file.
//
// TerraSolid format: http://cdn.terrasolid.fi/tscan.pdf
//
// (C) Copyright Howard Butler 2009, hobu.inc@gmail.com
//
// Distributed under the BSD License
// (See accompanying file LICENSE.txt or copy at
// http://www.opensource.org/licenses/bsd-license.php)
//
#include "ts2las.hpp"
#include "laskernel.hpp"

// boost
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4512)
#endif

#include <boost/program_options.hpp>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

namespace po = boost::program_options;

#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");
//         exit(1);
//     }
//     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");
//         exit(1);
//     }
//     
//     return ostrm;
// }

liblas::Header CreateHeader(ScanHdr* hdr, bool verbose)
{
    liblas::Header header;
    
    // Checks for time and color values
    liblas::PointFormatName format = liblas::ePointFormat0;
    
    if (hdr->Time) {
        if (hdr->Color) {
            format = liblas::ePointFormat3;
        } else {
            format = liblas::ePointFormat1;
        }
    } else if (hdr->Color) {
        format = liblas::ePointFormat2;
    } 
    header.SetVersionMinor(2);
    header.SetDataFormatId(format);

    double scale = 1.0/(double)hdr->Units;
    header.SetScale(scale, scale, scale);
    header.SetOffset(-hdr->OrgX*scale, -hdr->OrgY*scale, -hdr->OrgZ*scale);
    header.SetPointRecordsCount(hdr->PntCnt);
    
    if (verbose)
    {
        std::cout << "The file says there should be " << hdr->PntCnt << " points" << std::endl;
        std::cout << "units: " << hdr->Units << std::endl;
        std::cout << "format: " << format << std::endl;
        std::cout << "scale: " << scale << std::endl;
        std::cout << "x origin: " << hdr->OrgX << std::endl;
        std::cout << "y origin: " << hdr->OrgY << std::endl;
        std::cout << "z origin: " << hdr->OrgZ << std::endl;

        boost::uint32_t precision = GetStreamPrecision(scale);

        std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
        std::cout.precision(precision);
        std::cout << "offset x: " << header.GetOffsetX() << std::endl;
        std::cout << "offset y: " << header.GetOffsetY() << std::endl;
        std::cout << "offset z: " << header.GetOffsetZ() << 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&)
    {
        return false;
    }    

}

bool WritePoints(liblas::Writer& writer, std::istream& strm, ScanHdr* hdr, bool verbose) 
{
    ScanPnt* point = new ScanPnt;
    ScanRow* row = new ScanRow;
    boost::uint32_t i = 0;
    
    for (std::size_t t = 0; t < static_cast<std::size_t>(hdr->PntCnt); t++)
    {

        if (hdr->HdrVersion == 20020715) {
            try 
            {
                liblas::detail::read_n(*point, strm, sizeof(ScanPnt));
            }
            catch (std::out_of_range&) // we reached the end of the file
            {
                return true;
                // break;
            }            
        } else{
            try 
            {
                liblas::detail::read_n(*row, strm, sizeof(ScanRow));
            }
            catch (std::out_of_range&) // we reached the end of the file
            {
                return true;
                // break;
            }   
            
            point->Pnt.x = row->x;
            point->Pnt.y = row->y;
            point->Pnt.z = row->z;
            point->Code = row->Code;
            point->Line = row->Line;
            point->Intensity = row->EchoInt & 0x3FFF;
            point->Echo = (row->EchoInt >> 14);
        }
        Point p(&writer.GetHeader());
        
        p.SetRawX(point->Pnt.x);
        p.SetRawY(point->Pnt.y);
        p.SetRawZ(point->Pnt.z);

        // std::cout << "read x: " << point->Pnt.x << " y: "<< point->Pnt.y << " z: " <<point->Pnt.z<< std::endl;
        // std::cout << "wrote 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);
        if (hdr->Time) {
            boost::uint32_t t = 0xFFFFFFFF;
            liblas::detail::read_n(t, strm, sizeof(t));

            // Time stamps are assumed to be GPS week seconds. The 
            // storage format is a 32 bit unsigned integer where 
            // each integer step is 0.0002 seconds.

            p.SetTime(t*0.0002);
        }
        if (hdr->Color) {
            boost::uint8_t r, g, b, a = 0;
            liblas::Color color;
            liblas::detail::read_n(r, strm, sizeof(r));
            liblas::detail::read_n(b, strm, sizeof(b));
            liblas::detail::read_n(g, strm, sizeof(g));
            
            // TS .bin says to read 4 bytes here for some reason.  Maybe 
            // this is an alpha value or something
            liblas::detail::read_n(a, strm, sizeof(a));
            
            color.SetGreen(g);
            color.SetBlue(b);
            color.SetRed(r);
            p.SetColor(color);
        }
        
        // Set return number
        /* 
            TerraScan uses two bits for storing echo information. The possible values are: 
            0 Only echo 
            1 First of many echo 
            2 Intermediate echo 
            3 Last of many echo
        */
        if (point->Echo == 0) { 
            p.SetNumberOfReturns(1);
            p.SetReturnNumber(1);
        } else if (point->Echo == 1) {
            p.SetReturnNumber(1);
        } else if (point->Echo == 3) {
            p.SetReturnNumber(2);
            p.SetNumberOfReturns(2);
        } else {
            // I don't know what the hell to do here without cumulating
            // through all of the points.  Why wouldn't you store the return 
            // number?!
            p.SetReturnNumber(2);
            p.SetNumberOfReturns(3);
        }

        try {
            writer.WritePoint(p);
        } catch (std::exception) 
        {
            std::cerr << "Point writing failed!" << std::endl; 
        }
        i++;
        if (verbose)
            term_progress(std::cout, (i + 1) / static_cast<double>(hdr->PntCnt));
     
    }

    delete point;
    delete row;
    return true;    

}

void OutputHelp( std::ostream & oss, po::options_description const& options)
{
    oss << "--------------------------------------------------------------------\n";
    oss << "    ts2las (" << GetFullVersion() << ")\n";
    oss << "--------------------------------------------------------------------\n";

    oss << options;

    oss <<"\nFor more information, see the full documentation for ts2las at:\n";

    oss << " http://liblas.org/utilities/ts2las.html\n";
    oss << "----------------------------------------------------------\n";

}

int main(int argc, char* argv[])
{
    std::string input;
    std::string output;
    bool verbose = false;
    std::vector<liblas::FilterPtr> filters;
    
    po::options_description ts2las_options("ts2las options");
    po::options_description filtering_options = GetFilteringOptions();
    po::positional_options_description p;
    p.add("input", 1);
    p.add("output", 1);

    ts2las_options.add_options()
        ("help,h", "Produce this help message")
        ("input,i", po::value< std::string >(&input), "input TerraSolid .bin file")
        ("output,o", po::value< std::string >(&output)->default_value(""), "The output .las file (defaults to input filename + .las)")
        ("verbose,v", po::value<bool>(&verbose)->zero_tokens(), "Verbose message output")

    ;
    
    po::variables_map vm;        
    po::options_description options;
    options.add(ts2las_options).add(filtering_options);
    po::store(po::command_line_parser(argc, argv).
      options(options).positional(p).run(), vm);
;

    po::notify(vm);

    if (vm.count("help")) 
    {
        OutputHelp(std::cout, options);
        return 1;
    }
    
    if (input.empty())
    {
        std::cerr << "No input TerraSolid .bin file was specfied!" << std::endl;
        OutputHelp(std::cout, options);
        return 1;
    }
    
    
    filters = GetFilters(vm, verbose);

    if (verbose)
        std::cout << "input: " << input<<  " output: " <<output<<std::endl;
    
    ScanHdr* hdr = new ScanHdr;

    std::ifstream istrm;
    bool opened = liblas::Open(istrm, input);
    if (!opened)
    {
        std::cerr << "Could not open file '" << input << "' to read TerraSolid .bin data! " << std::endl;
        return 1;
    }
    
    std::ofstream ostrm;
    bool created = liblas::Create(ostrm, output);
    if (!created)
    {
        std::cerr << "Could not create file " << output << " to write LAS data!" << std::endl;;
        return 1;
    }
    
    bool success;
    success = ReadHeader(hdr, istrm);
    if (!success) 
    {
        std::cerr<<"Unable to read " << input << "to read file!" << std::endl; 
        return 1;
    }
    
    // std::cout << "stream position is: " << istrm->tellg() << std::endl;
    liblas::Header header = CreateHeader(hdr, verbose);
    liblas::Writer writer(ostrm, header);
    
    writer.SetFilters(filters);
    
    success = WritePoints(writer, istrm, hdr, verbose);

    if (verbose)
    {
        std::cout << "Successfully wrote " << header.GetPointRecordsCount() 
                  <<" points to " << output << std::endl;
    }
    return 0;
}