File: lasblock.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 (325 lines) | stat: -rwxr-xr-x 9,423 bytes parent folder | download | duplicates (3)
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
// $Id$
//
// lasblock generates block output for las2oci to store lidar data in OPC tables
//
//
// (C) Copyright Howard Butler 2010, 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 <liblas/liblas.hpp>
#include <liblas/chipper.hpp>
#include "laskernel.hpp"

// std
#include <fstream>
#include <vector>

// 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;

#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

typedef boost::shared_ptr<liblas::Writer> WriterPtr;
typedef boost::shared_ptr<liblas::CoordinateSummary> SummaryPtr;
typedef boost::shared_ptr<std::ofstream> OStreamPtr;


liblas::Writer* start_writer(   std::ostream*& ofs, 
                      std::string const& output, 
                      liblas::Header const& header)
{
    ofs = liblas::Create(output, std::ios::out | std::ios::binary);
    if (!ofs)
    {
        std::ostringstream oss;
        oss << "Cannot create " << output << "for write.  Exiting...";
        throw std::runtime_error(oss.str());
    }

    return new liblas::Writer(*ofs, header);
}

using namespace liblas;

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

    oss << options;

    oss <<"\nFor more information, see the full documentation for lasblock at:\n";
    
    oss << " http://liblas.org/utilities/lasblock.html\n";
    oss << "----------------------------------------------------------\n";

}


void write_tiles(std::string& output, 
                 liblas::chipper::Chipper& c, 
                 liblas::Reader& reader, 
                 bool verbose,
                 bool bCompressed)
{


    std::string out = output;
    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);
    

    if (verbose)
        std::cout << "Writing " << c.GetBlockCount() << " blocks to " << output << std::endl;

    boost::uint32_t prog = 0;
    
    for ( boost::uint32_t i = 0; i < c.GetBlockCount(); ++i )
    {
        std::ostringstream name;
        name << out << "-" << i;
        if (bCompressed)
            name << ".laz";
        else
            name << ".las";
        SummaryPtr summary(new::liblas::CoordinateSummary);        

        {
            std::ostream* ofs;
            
            const liblas::chipper::Block& b = c.GetBlock(i);
            header.SetExtent(b.GetBounds());
        
            liblas::Writer* writer = start_writer(ofs, name.str(), header);

            std::vector<boost::uint32_t> ids = b.GetIDs();
        
        
            for ( boost::uint32_t pi = 0; pi < ids.size(); ++pi )
            {
            
                bool read = reader.ReadPointAt(ids[pi]);
                if (read) {
                    liblas::Point const& p = reader.GetPoint();
                    summary->AddPoint(p);
                    writer->WritePoint(p);
                }
            }
            
            if (writer != 0)
                delete writer;
            if (ofs != 0)
            {
                liblas::Cleanup(ofs);
            }
            
        }
        
        liblas::Header hnew = FetchHeader(name.str());
        RepairHeader(*summary, hnew);
        RewriteHeader(hnew, name.str());

        if (verbose)
            term_progress(std::cout, (prog + 1) / static_cast<double>(c.GetBlockCount()));
        prog++;
    }

}
void write_index(std::string& output, 
                 liblas::chipper::Chipper& c, 
                 liblas::Reader& /*reader*/, 
                 long precision, 
                 bool verbose,
                 bool bUseStdout)
{
    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();
    
    if (verbose)
        std::cout << "Writing " << c.GetBlockCount() << " blocks to " << output << std::endl;

    boost::uint32_t prog = 0;
    
    for ( boost::uint32_t i = 0; i < c.GetBlockCount(); ++i )
    {
        const liblas::chipper::Block& b = c.GetBlock(i);

        std::vector<boost::uint32_t> ids = b.GetIDs();
        *out << i << " " << ids.size() << " ";
        
        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) << " " ;
        
        for ( boost::uint32_t pi = 0; pi < ids.size(); ++pi )
        {
            *out << ids[pi] << " ";
        }

        *out << std::endl;

        // Set back to writing decimals
        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[])
{
    std::string input;
    std::string output;
    
    long capacity = 3000;
    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);

        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", po::value<bool>(&bCompressed)->zero_tokens()->implicit_value(true), "Produce .laz compressed data for --write-points tiles")

            ("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);

        po::notify(vm);

        if (vm.count("help")) 
        {
            std::cout << desc << "\n";
            std::cout <<"\nFor more information, see the full documentation for lasblock at:\n";
    
            std::cout << " http://liblas.org/utilities/block.html\n";
            std::cout << "----------------------------------------------------------\n";
            return 1;
        }
        
        if (vm.count("input")) 
        {
            input = vm["input"].as< std::string >();
            std::ifstream ifs;

            if (!liblas::Open(ifs, input.c_str()))
            {
                std::cerr << "Cannot open file '" << input << "'for read.  Exiting...";
                return 1;
            }
        } 
             else {
                std::cerr << "Input LAS file not specified!\n";
                OutputHelp(std::cout, desc);
                return 1;
            }    


        if (output.empty())
        {
            output = std::string(input) + ".kdx";
        }

        std::istream* ifs = liblas::Open(input, std::ios::in | std::ios::binary);
        if (!ifs)
        {
            std::cerr << "Cannot open " << input << " for read.  Exiting..." << std::endl;
            return 1;
        }

            
        
        {
            liblas::ReaderFactory f;
            liblas::Reader reader = f.CreateWithStream(*ifs);
    
            liblas::chipper::Chipper c(&reader, capacity);

            if (verbose)
                std::cout << "Chipping " << input<< " to " << output <<std::endl;

            c.Chip();
        

            if (!tiling)
            {
                write_index(output, c, reader, precision, verbose, bUseStdout);
            } else 
            {
                write_tiles(output, c, reader, verbose, bCompressed);
            }
            
        }
        
        if (ifs != 0)
        {
            liblas::Cleanup(ifs);
            ifs = 0;
        }
    }

    catch(std::exception& e)
    {
        std::cerr << "error: " << e.what() << "\n";
        return 1;
    }
    catch(...)
    {
        std::cerr << "Exception of unknown type!\n";
    }
    
    return 0;
}