QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
qgslazdecoder.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslazdecoder.cpp
3 --------------------
4 begin : March 2022
5 copyright : (C) 2022 by Belgacem Nedjima
6 email : belgacem dot nedjima at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsconfig.h"
19#include "qgslazdecoder.h"
20
21#include <cstring>
22#include <iostream>
23#include <memory>
24#include <string>
25#include <zstd.h>
26
27#include "lazperf/las.hpp"
28#include "lazperf/readers.hpp"
29#include "qgslazinfo.h"
30#include "qgslogger.h"
32#include "qgspointcloudexpression.h"
33#include "qgsvector3d.h"
34
35#include <QDir>
36#include <QElapsedTimer>
37#include <QFile>
38#include <QString>
39#include <QTemporaryFile>
40
41using namespace Qt::StringLiterals;
42
43#if defined( _MSC_VER )
44#ifndef UNICODE
45#define UNICODE
46#endif
47#include <locale>
48#include <codecvt>
49#endif
50
52
53template<typename T> bool lazStoreToStream_( char *s, size_t position, QgsPointCloudAttribute::DataType type, T value )
54{
55 switch ( type )
56 {
58 {
59 const char val = char( value );
60 s[position] = val;
61 break;
62 }
64 {
65 const unsigned char val = ( unsigned char ) ( value );
66 s[position] = val;
67 break;
68 }
69
71 {
72 short val = short( value );
73 memcpy( s + position, reinterpret_cast<char * >( &val ), sizeof( short ) );
74 break;
75 }
77 {
78 unsigned short val = static_cast< unsigned short>( value );
79 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( unsigned short ) );
80 break;
81 }
82
84 {
85 qint32 val = qint32( value );
86 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( qint32 ) );
87 break;
88 }
90 {
91 quint32 val = quint32( value );
92 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( quint32 ) );
93 break;
94 }
95
97 {
98 qint64 val = qint64( value );
99 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( qint64 ) );
100 break;
101 }
103 {
104 quint64 val = quint64( value );
105 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( quint64 ) );
106 break;
107 }
108
110 {
111 float val = float( value );
112 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( float ) );
113 break;
114 }
116 {
117 double val = double( value );
118 memcpy( s + position, reinterpret_cast< char * >( &val ), sizeof( double ) );
119 break;
120 }
121 }
122
123 return true;
124}
125
126bool lazSerialize_( char *data, size_t outputPosition, QgsPointCloudAttribute::DataType outputType, const char *input, QgsPointCloudAttribute::DataType inputType, int inputSize, size_t inputPosition )
127{
128 if ( outputType == inputType )
129 {
130 memcpy( data + outputPosition, input + inputPosition, inputSize );
131 return true;
132 }
133
134 switch ( inputType )
135 {
137 {
138 const char val = *( input + inputPosition );
139 return lazStoreToStream_<char>( data, outputPosition, outputType, val );
140 }
142 {
143 const unsigned char val = *( input + inputPosition );
144 return lazStoreToStream_<unsigned char>( data, outputPosition, outputType, val );
145 }
147 {
148 const short val = *reinterpret_cast< const short * >( input + inputPosition );
149 return lazStoreToStream_<short>( data, outputPosition, outputType, val );
150 }
152 {
153 const unsigned short val = *reinterpret_cast< const unsigned short * >( input + inputPosition );
154 return lazStoreToStream_<unsigned short>( data, outputPosition, outputType, val );
155 }
157 {
158 const qint32 val = *reinterpret_cast<const qint32 * >( input + inputPosition );
159 return lazStoreToStream_<qint32>( data, outputPosition, outputType, val );
160 }
162 {
163 const quint32 val = *reinterpret_cast<const quint32 * >( input + inputPosition );
164 return lazStoreToStream_<quint32>( data, outputPosition, outputType, val );
165 }
167 {
168 const qint64 val = *reinterpret_cast<const qint64 * >( input + inputPosition );
169 return lazStoreToStream_<qint64>( data, outputPosition, outputType, val );
170 }
172 {
173 const quint64 val = *reinterpret_cast<const quint64 * >( input + inputPosition );
174 return lazStoreToStream_<quint64>( data, outputPosition, outputType, val );
175 }
177 {
178 const float val = *reinterpret_cast< const float * >( input + inputPosition );
179 return lazStoreToStream_<float>( data, outputPosition, outputType, val );
180 }
182 {
183 const double val = *reinterpret_cast< const double * >( input + inputPosition );
184 return lazStoreToStream_<double>( data, outputPosition, outputType, val );
185 }
186 }
187 return true;
188}
189
190// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
191
192std::vector< QgsLazDecoder::RequestedAttributeDetails > prepareRequestedAttributeDetails_(
193 const QgsPointCloudAttributeCollection &requestedAttributes, QVector<QgsLazInfo::ExtraBytesAttributeDetails> &extrabytesAttr
194)
195{
196 const QVector<QgsPointCloudAttribute> requestedAttributesVector = requestedAttributes.attributes();
197
198 std::vector< QgsLazDecoder::RequestedAttributeDetails > requestedAttributeDetails;
199 requestedAttributeDetails.reserve( requestedAttributesVector.size() );
200
201 for ( const QgsPointCloudAttribute &requestedAttribute : requestedAttributesVector )
202 {
203 if ( requestedAttribute.name().compare( 'X'_L1, Qt::CaseInsensitive ) == 0 )
204 {
205 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::X, requestedAttribute.type(), requestedAttribute.size() ) );
206 }
207 else if ( requestedAttribute.name().compare( 'Y'_L1, Qt::CaseInsensitive ) == 0 )
208 {
209 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Y, requestedAttribute.type(), requestedAttribute.size() ) );
210 }
211 else if ( requestedAttribute.name().compare( 'Z'_L1, Qt::CaseInsensitive ) == 0 )
212 {
213 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Z, requestedAttribute.type(), requestedAttribute.size() ) );
214 }
215 else if ( requestedAttribute.name().compare( "Classification"_L1, Qt::CaseInsensitive ) == 0 )
216 {
217 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Classification, requestedAttribute.type(), requestedAttribute.size() ) );
218 }
219 else if ( requestedAttribute.name().compare( "Intensity"_L1, Qt::CaseInsensitive ) == 0 )
220 {
221 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Intensity, requestedAttribute.type(), requestedAttribute.size() ) );
222 }
223 else if ( requestedAttribute.name().compare( "ReturnNumber"_L1, Qt::CaseInsensitive ) == 0 )
224 {
225 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::ReturnNumber, requestedAttribute.type(), requestedAttribute.size() ) );
226 }
227 else if ( requestedAttribute.name().compare( "NumberOfReturns"_L1, Qt::CaseInsensitive ) == 0 )
228 {
229 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::NumberOfReturns, requestedAttribute.type(), requestedAttribute.size() ) );
230 }
231 else if ( requestedAttribute.name().compare( "ScanDirectionFlag"_L1, Qt::CaseInsensitive ) == 0 )
232 {
233 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::ScanDirectionFlag, requestedAttribute.type(), requestedAttribute.size() ) );
234 }
235 else if ( requestedAttribute.name().compare( "EdgeOfFlightLine"_L1, Qt::CaseInsensitive ) == 0 )
236 {
237 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::EdgeOfFlightLine, requestedAttribute.type(), requestedAttribute.size() ) );
238 }
239 else if ( requestedAttribute.name().compare( "ScanAngleRank"_L1, Qt::CaseInsensitive ) == 0 )
240 {
241 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::ScanAngleRank, requestedAttribute.type(), requestedAttribute.size() ) );
242 }
243 else if ( requestedAttribute.name().compare( "UserData"_L1, Qt::CaseInsensitive ) == 0 )
244 {
245 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::UserData, requestedAttribute.type(), requestedAttribute.size() ) );
246 }
247 else if ( requestedAttribute.name().compare( "PointSourceId"_L1, Qt::CaseInsensitive ) == 0 )
248 {
249 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::PointSourceId, requestedAttribute.type(), requestedAttribute.size() ) );
250 }
251 else if ( requestedAttribute.name().compare( "GpsTime"_L1, Qt::CaseInsensitive ) == 0 )
252 {
253 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::GpsTime, requestedAttribute.type(), requestedAttribute.size() ) );
254 }
255 else if ( requestedAttribute.name().compare( "Red"_L1, Qt::CaseInsensitive ) == 0 )
256 {
257 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Red, requestedAttribute.type(), requestedAttribute.size() ) );
258 }
259 else if ( requestedAttribute.name().compare( "Green"_L1, Qt::CaseInsensitive ) == 0 )
260 {
261 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Green, requestedAttribute.type(), requestedAttribute.size() ) );
262 }
263 else if ( requestedAttribute.name().compare( "Blue"_L1, Qt::CaseInsensitive ) == 0 )
264 {
265 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Blue, requestedAttribute.type(), requestedAttribute.size() ) );
266 }
267 else if ( requestedAttribute.name().compare( "ScannerChannel"_L1, Qt::CaseInsensitive ) == 0 )
268 {
269 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::ScannerChannel, requestedAttribute.type(), requestedAttribute.size() ) );
270 }
271 else if ( requestedAttribute.name().compare( "Synthetic"_L1, Qt::CaseInsensitive ) == 0 )
272 {
273 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Synthetic, requestedAttribute.type(), requestedAttribute.size() ) );
274 }
275 else if ( requestedAttribute.name().compare( "KeyPoint"_L1, Qt::CaseInsensitive ) == 0 )
276 {
277 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::KeyPoint, requestedAttribute.type(), requestedAttribute.size() ) );
278 }
279 else if ( requestedAttribute.name().compare( "Withheld"_L1, Qt::CaseInsensitive ) == 0 )
280 {
281 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Withheld, requestedAttribute.type(), requestedAttribute.size() ) );
282 }
283 else if ( requestedAttribute.name().compare( "Overlap"_L1, Qt::CaseInsensitive ) == 0 )
284 {
285 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::Overlap, requestedAttribute.type(), requestedAttribute.size() ) );
286 }
287 else if ( requestedAttribute.name().compare( "Infrared"_L1, Qt::CaseInsensitive ) == 0 )
288 {
289 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::NIR, requestedAttribute.type(), requestedAttribute.size() ) );
290 }
291 else
292 {
293 bool foundAttr = false;
294 for ( QgsLazInfo::ExtraBytesAttributeDetails &eba : extrabytesAttr )
295 {
296 if ( requestedAttribute.name().compare( eba.attribute.trimmed() ) == 0 )
297 {
298 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::ExtraBytes, eba.type, eba.size, eba.offset ) );
299 foundAttr = true;
300 break;
301 }
302 }
303 if ( !foundAttr )
304 {
305 // this can possibly happen -- e.g. if a style built using a different point cloud format references an attribute which isn't available from the laz file
306 requestedAttributeDetails.emplace_back( QgsLazDecoder::RequestedAttributeDetails( QgsLazDecoder::LazAttribute::MissingOrUnknown, requestedAttribute.type(), requestedAttribute.size() ) );
307 }
308 }
309 }
310 return requestedAttributeDetails;
311}
312
313bool decodePoint( char *buf, int lasPointFormat, char *dataBuffer, std::size_t &outputOffset, std::vector< QgsLazDecoder::RequestedAttributeDetails > &requestedAttributeDetails )
314{
315 lazperf::las::point10 p10;
316 lazperf::las::gpstime gps;
317 lazperf::las::rgb rgb;
318 lazperf::las::nir14 nir;
319 lazperf::las::point14 p14;
320
321 // Does the point record start with the common fields for formats introduced
322 // in the LAS 1.4 spec?
323 const bool isLas14 = ( lasPointFormat == 6 || lasPointFormat == 7 || lasPointFormat == 8 || lasPointFormat == 9 || lasPointFormat == 10 );
324
325 switch ( lasPointFormat )
326 {
327 // LAS 1.2 file support
328 case 0: // base
329 p10.unpack( buf );
330 break;
331 case 1: // base + gps time
332 p10.unpack( buf );
333 gps.unpack( buf + sizeof( lazperf::las::point10 ) );
334 break;
335 case 2: // base + rgb
336 p10.unpack( buf );
337 rgb.unpack( buf + sizeof( lazperf::las::point10 ) );
338 break;
339 case 3: // base + gps time + rgb
340 p10.unpack( buf );
341 gps.unpack( buf + sizeof( lazperf::las::point10 ) );
342 rgb.unpack( buf + sizeof( lazperf::las::point10 ) + sizeof( lazperf::las::gpstime ) );
343 break;
344
345 // LAS 1.4 file support
346 case 6: // base (includes gps time)
347 p14.unpack( buf );
348 break;
349 case 7: // base + rgb
350 p14.unpack( buf );
351 rgb.unpack( buf + sizeof( lazperf::las::point14 ) );
352 break;
353 case 8: // base + rgb + nir
354 p14.unpack( buf );
355 rgb.unpack( buf + sizeof( lazperf::las::point14 ) );
356 nir.unpack( buf + sizeof( lazperf::las::point14 ) + sizeof( lazperf::las::rgb ) );
357 break;
358
359 default:
360 Q_ASSERT( false ); // must not happen - we checked earlier that the format is supported
361 return false;
362 }
363
364 for ( const QgsLazDecoder::RequestedAttributeDetails &requestedAttribute : requestedAttributeDetails )
365 {
366 switch ( requestedAttribute.attribute )
367 {
368 case QgsLazDecoder::LazAttribute::X:
369 lazStoreToStream_<qint32>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.x() : p10.x );
370 break;
371 case QgsLazDecoder::LazAttribute::Y:
372 lazStoreToStream_<qint32>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.y() : p10.y );
373 break;
374 case QgsLazDecoder::LazAttribute::Z:
375 lazStoreToStream_<qint32>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.z() : p10.z );
376 break;
377 case QgsLazDecoder::LazAttribute::Classification:
378 {
379 if ( isLas14 )
380 {
381 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, p14.classification() );
382 }
383 else
384 {
385 // p10 format encoded "Overlap" as Classification=12, so in that case we set Classification=0 (Never classified) and will set Overlap=1 a few lines below
386 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, ( p10.classification & 0x1F ) == 12 ? 0 : p10.classification & 0x1F );
387 }
388 break;
389 }
390 case QgsLazDecoder::LazAttribute::Intensity:
391 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.intensity() : p10.intensity );
392 break;
393 case QgsLazDecoder::LazAttribute::ReturnNumber:
394 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.returnNum() : p10.return_number );
395 break;
396 case QgsLazDecoder::LazAttribute::NumberOfReturns:
397 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.numReturns() : p10.number_of_returns_of_given_pulse );
398 break;
399 case QgsLazDecoder::LazAttribute::ScanDirectionFlag:
400 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.scanDirFlag() : p10.scan_direction_flag );
401 break;
402 case QgsLazDecoder::LazAttribute::EdgeOfFlightLine:
403 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.eofFlag() : p10.edge_of_flight_line );
404 break;
405 case QgsLazDecoder::LazAttribute::ScanAngleRank:
406 lazStoreToStream_<float>(
407 dataBuffer,
408 outputOffset,
409 requestedAttribute.type,
410 isLas14
411 // Formats from LAS 1.4 spec store the angle in 0.006 degree increments
412 ? p14.scanAngle() * 0.006f
413 // Older formats store integer values
414 : p10.scan_angle_rank
415 );
416 break;
417 case QgsLazDecoder::LazAttribute::UserData:
418 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.userData() : p10.user_data );
419 break;
420 case QgsLazDecoder::LazAttribute::PointSourceId:
421 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.pointSourceID() : p10.point_source_ID );
422 break;
423 case QgsLazDecoder::LazAttribute::GpsTime:
424 // lazperf internally stores gps value as int64 field, but in fact it is a double value
425 lazStoreToStream_<double>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? p14.gpsTime() : *reinterpret_cast<const double *>( reinterpret_cast<const void *>( &gps.value ) ) );
426 break;
427 case QgsLazDecoder::LazAttribute::Red:
428 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, rgb.r );
429 break;
430 case QgsLazDecoder::LazAttribute::Green:
431 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, rgb.g );
432 break;
433 case QgsLazDecoder::LazAttribute::Blue:
434 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, rgb.b );
435 break;
436 case QgsLazDecoder::LazAttribute::ScannerChannel:
437 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? char( p14.scannerChannel() ) : 0 );
438 break;
439 case QgsLazDecoder::LazAttribute::Synthetic:
440 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? char( ( p14.classFlags() >> 0 ) & 0x01 ) : char( ( p10.classification >> 5 ) & 0x01 ) );
441 break;
442 case QgsLazDecoder::LazAttribute::KeyPoint:
443 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? char( ( p14.classFlags() >> 1 ) & 0x01 ) : char( ( p10.classification >> 6 ) & 0x01 ) );
444 break;
445 case QgsLazDecoder::LazAttribute::Withheld:
446 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, isLas14 ? char( ( p14.classFlags() >> 2 ) & 0x01 ) : char( ( p10.classification >> 7 ) & 0x01 ) );
447 break;
448 case QgsLazDecoder::LazAttribute::Overlap:
449 {
450 if ( isLas14 )
451 {
452 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, char( ( p14.classFlags() >> 3 ) & 0x01 ) );
453 }
454 else
455 {
456 // p10 format encoded "Overlap" as Classification=12, so in that case we set Overlap=1 (we have already set Classification=0)
457 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, ( p10.classification & 0x1F ) == 12 ? 1 : 0 );
458 }
459 break;
460 }
461 case QgsLazDecoder::LazAttribute::NIR:
462 {
463 if ( lasPointFormat == 8 || lasPointFormat == 10 )
464 {
465 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, nir.val );
466 }
467 else
468 {
469 // just store 0 for missing attributes
470 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, 0 );
471 }
472 break;
473 }
474 case QgsLazDecoder::LazAttribute::ExtraBytes:
475 {
476 switch ( requestedAttribute.type )
477 {
479 lazStoreToStream_<char>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<char * >( &buf[requestedAttribute.offset] ) );
480 break;
482 lazStoreToStream_<unsigned char>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<unsigned char * >( &buf[requestedAttribute.offset] ) );
483 break;
485 lazStoreToStream_<qint16>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<qint16 * >( &buf[requestedAttribute.offset] ) );
486 break;
488 lazStoreToStream_<quint16>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<quint16 * >( &buf[requestedAttribute.offset] ) );
489 break;
491 lazStoreToStream_<qint32>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<qint32 * >( &buf[requestedAttribute.offset] ) );
492 break;
494 lazStoreToStream_<quint32>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<quint32 * >( &buf[requestedAttribute.offset] ) );
495 break;
497 lazStoreToStream_<qint64>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<qint64 * >( &buf[requestedAttribute.offset] ) );
498 break;
500 lazStoreToStream_<quint64>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<quint64 * >( &buf[requestedAttribute.offset] ) );
501 break;
503 lazStoreToStream_<float>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<float * >( &buf[requestedAttribute.offset] ) );
504 break;
506 lazStoreToStream_<double>( dataBuffer, outputOffset, requestedAttribute.type, *reinterpret_cast<double * >( &buf[requestedAttribute.offset] ) );
507 break;
508 }
509 }
510 break;
511 case QgsLazDecoder::LazAttribute::MissingOrUnknown:
512 // just store 0 for unknown/missing attributes
513 lazStoreToStream_<unsigned short>( dataBuffer, outputOffset, requestedAttribute.type, 0 );
514 break;
515 }
516
517 outputOffset += requestedAttribute.size;
518 }
519 return true;
520}
521
522template<typename FileType>
523std::unique_ptr<QgsPointCloudBlock> decompressLaz_( FileType &file, const QgsPointCloudAttributeCollection &requestedAttributes, QgsPointCloudExpression &filterExpression, QgsRectangle &filterRect )
524{
525 if ( !file.good() )
526 return nullptr;
527
528#ifdef QGISDEBUG
529 QElapsedTimer t;
530 t.start();
531#endif
532
533 // lazperf may throw exceptions
534 try
535 {
536 lazperf::reader::generic_file f( file );
537
538
539 // output file formats from entwine/untwine:
540 // - older versions write LAZ 1.2 files with point formats 0, 1, 2 or 3
541 // - newer versions write LAZ 1.4 files with point formats 6, 7 or 8
542
543 int lasPointFormat = f.header().pointFormat();
544 if ( lasPointFormat != 0 && lasPointFormat != 1 && lasPointFormat != 2 && lasPointFormat != 3 && lasPointFormat != 6 && lasPointFormat != 7 && lasPointFormat != 8 )
545 {
546 QgsDebugError( u"Unexpected point format record (%1) - only 0, 1, 2, 3, 6, 7, 8 are supported"_s.arg( lasPointFormat ) );
547 return nullptr;
548 }
549
550 const size_t count = f.header().point_count;
551 const QgsVector3D scale( f.header().scale.x, f.header().scale.y, f.header().scale.z );
552 const QgsVector3D offset( f.header().offset.x, f.header().offset.y, f.header().offset.z );
553
554 QByteArray bufArray( f.header().point_record_length, 0 );
555 char *buf = bufArray.data();
556
557 const size_t requestedPointRecordSize = requestedAttributes.pointRecordSize();
558 QByteArray data;
559 data.resize( requestedPointRecordSize * count );
560 char *dataBuffer = data.data();
561
562 std::size_t outputOffset = 0;
563
564 auto block = std::make_unique< QgsPointCloudBlock >( count, requestedAttributes, data, scale, offset );
565
566 int skippedPoints = 0;
567 const bool filterIsValid = filterExpression.isValid();
568 if ( !filterExpression.prepare( block.get() ) && filterIsValid )
569 {
570 // skip processing if the expression cannot be prepared
571 block->setPointCount( 0 );
572 return block;
573 }
574
575 int xAttributeOffset, yAttributeOffset;
576 const QgsPointCloudAttribute *attributeX = nullptr;
577 const QgsPointCloudAttribute *attributeY = nullptr;
578 const bool hasFilterRect = !filterRect.isEmpty();
579 if ( hasFilterRect )
580 {
581 attributeX = requestedAttributes.find( "X"_L1, xAttributeOffset );
582 attributeY = requestedAttributes.find( "Y"_L1, yAttributeOffset );
583 filterRect.setXMinimum( ( filterRect.xMinimum() - offset.x() ) / scale.x() );
584 filterRect.setXMaximum( ( filterRect.xMaximum() - offset.x() ) / scale.x() );
585 filterRect.setYMinimum( ( filterRect.yMinimum() - offset.y() ) / scale.y() );
586 filterRect.setYMaximum( ( filterRect.yMaximum() - offset.y() ) / scale.y() );
587 }
588
589 std::vector<char> rawExtrabytes = f.vlrData( "LASF_Spec", 4 );
590 QVector<QgsLazInfo::ExtraBytesAttributeDetails> extrabyteAttributesDetails = QgsLazInfo::parseExtrabytes( rawExtrabytes.data(), rawExtrabytes.size(), f.header().point_record_length );
591 std::vector< QgsLazDecoder::RequestedAttributeDetails > requestedAttributeDetails = prepareRequestedAttributeDetails_( requestedAttributes, extrabyteAttributesDetails );
592
593 for ( size_t i = 0; i < count; i++ )
594 {
595 f.readPoint( buf ); // read the point out
596
597 bool skipThisPoint = !decodePoint( buf, lasPointFormat, dataBuffer, outputOffset, requestedAttributeDetails );
598
599 // check if point needs to be filtered out
600 if ( !skipThisPoint && hasFilterRect && attributeX && attributeY )
601 {
602 const double x = attributeX->convertValueToDouble( dataBuffer + outputOffset - requestedPointRecordSize + xAttributeOffset );
603 const double y = attributeY->convertValueToDouble( dataBuffer + outputOffset - requestedPointRecordSize + yAttributeOffset );
604 if ( !filterRect.contains( x, y ) )
605 skipThisPoint = true;
606 }
607 if ( !skipThisPoint && filterIsValid )
608 {
609 // we're always evaluating the last written point in the buffer
610 double eval = filterExpression.evaluate( i - skippedPoints );
611 if ( !eval || std::isnan( eval ) )
612 skipThisPoint = true;
613 }
614 if ( skipThisPoint )
615 {
616 // if the point is filtered out, rewind the offset so the next point is written over it
617 outputOffset -= requestedPointRecordSize;
618 ++skippedPoints;
619 }
620 }
621
622#ifdef QGISDEBUG
623 QgsDebugMsgLevel( u"LAZ-PERF Read through the points in %1 seconds."_s.arg( t.elapsed() / 1000. ), 2 );
624#endif
625 block->setPointCount( count - skippedPoints );
626 return block;
627 }
628 catch ( std::exception &e )
629 {
630 QgsDebugError( "Error decompressing laz file: " + QString::fromLatin1( e.what() ) );
631 return nullptr;
632 }
633}
634
635std::unique_ptr<QgsPointCloudBlock> QgsLazDecoder::decompressLaz(
636 const QString &filename, const QgsPointCloudAttributeCollection &requestedAttributes, QgsPointCloudExpression &filterExpression, QgsRectangle &filterRect
637)
638{
639 std::ifstream file( toNativePath( filename ), std::ios::binary );
640
641 return decompressLaz_<std::ifstream>( file, requestedAttributes, filterExpression, filterRect );
642}
643
644std::unique_ptr<QgsPointCloudBlock> QgsLazDecoder::decompressLaz(
645 const QByteArray &byteArrayData, const QgsPointCloudAttributeCollection &requestedAttributes, QgsPointCloudExpression &filterExpression, QgsRectangle &filterRect
646)
647{
648 std::istringstream file( byteArrayData.toStdString() );
649 return decompressLaz_<std::istringstream>( file, requestedAttributes, filterExpression, filterRect );
650}
651
652std::unique_ptr<QgsPointCloudBlock> QgsLazDecoder::decompressCopc(
653 const QByteArray &data, QgsLazInfo &lazInfo, int32_t pointCount, const QgsPointCloudAttributeCollection &requestedAttributes, QgsPointCloudExpression &filterExpression, QgsRectangle &filterRect
654)
655{
656 // COPC only supports point formats 6, 7 and 8
657 int lasPointFormat = lazInfo.pointFormat();
658 if ( lasPointFormat != 6 && lasPointFormat != 7 && lasPointFormat != 8 )
659 {
660 QgsDebugError( u"Unexpected point format record (%1) - only 6, 7, 8 are supported for COPC format"_s.arg( lasPointFormat ) );
661 return nullptr;
662 }
663
664 std::unique_ptr<char[]> decodedData( new char[lazInfo.pointRecordLength()] );
665
666 lazperf::reader::chunk_decompressor decompressor( lasPointFormat, lazInfo.extrabytesCount(), data.data() );
667
668 const size_t requestedPointRecordSize = requestedAttributes.pointRecordSize();
669 QByteArray blockData;
670 blockData.resize( requestedPointRecordSize * pointCount );
671 char *dataBuffer = blockData.data();
672
673 std::size_t outputOffset = 0;
674
675 QVector<QgsLazInfo::ExtraBytesAttributeDetails> extrabyteAttributesDetails = lazInfo.extrabytes();
676 std::vector< RequestedAttributeDetails > requestedAttributeDetails = prepareRequestedAttributeDetails_( requestedAttributes, extrabyteAttributesDetails );
677 auto block = std::make_unique< QgsPointCloudBlock >( pointCount, requestedAttributes, blockData, lazInfo.scale(), lazInfo.offset() );
678
679 int skippedPoints = 0;
680 const bool filterIsValid = filterExpression.isValid();
681 if ( !filterExpression.prepare( block.get() ) && filterIsValid )
682 {
683 // skip processing if the expression cannot be prepared
684 block->setPointCount( 0 );
685 return block;
686 }
687
688 int xAttributeOffset, yAttributeOffset;
689 const QgsPointCloudAttribute *attributeX = nullptr;
690 const QgsPointCloudAttribute *attributeY = nullptr;
691 const bool hasFilterRect = !filterRect.isEmpty();
692 if ( hasFilterRect )
693 {
694 attributeX = requestedAttributes.find( "X"_L1, xAttributeOffset );
695 attributeY = requestedAttributes.find( "Y"_L1, yAttributeOffset );
696 filterRect.setXMinimum( ( filterRect.xMinimum() - lazInfo.offset().x() ) / lazInfo.scale().x() );
697 filterRect.setXMaximum( ( filterRect.xMaximum() - lazInfo.offset().x() ) / lazInfo.scale().x() );
698 filterRect.setYMinimum( ( filterRect.yMinimum() - lazInfo.offset().y() ) / lazInfo.scale().y() );
699 filterRect.setYMaximum( ( filterRect.yMaximum() - lazInfo.offset().y() ) / lazInfo.scale().y() );
700 }
701 for ( int i = 0; i < pointCount; ++i )
702 {
703 decompressor.decompress( decodedData.get() );
704 char *buf = decodedData.get();
705
706 bool skipThisPoint = !decodePoint( buf, lasPointFormat, dataBuffer, outputOffset, requestedAttributeDetails );
707
708 // check if point needs to be filtered out
709 if ( !skipThisPoint && hasFilterRect && attributeX && attributeY )
710 {
711 const double x = attributeX->convertValueToDouble( dataBuffer + outputOffset - requestedPointRecordSize + xAttributeOffset );
712 const double y = attributeY->convertValueToDouble( dataBuffer + outputOffset - requestedPointRecordSize + yAttributeOffset );
713 if ( !filterRect.contains( x, y ) )
714 skipThisPoint = true;
715 }
716 if ( !skipThisPoint && filterIsValid )
717 {
718 // we're always evaluating the last written point in the buffer
719 double eval = filterExpression.evaluate( i - skippedPoints );
720 if ( !eval || std::isnan( eval ) )
721 skipThisPoint = true;
722 }
723 if ( skipThisPoint )
724 {
725 // if the point is filtered out, rewind the offset so the next point is written over it
726 outputOffset -= requestedPointRecordSize;
727 ++skippedPoints;
728 }
729 }
730
731 block->setPointCount( pointCount - skippedPoints );
732 return block;
733}
734
735#if defined( _MSC_VER )
736std::wstring QgsLazDecoder::toNativePath( const QString &filename )
737{
738 std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > converter;
739 return converter.from_bytes( filename.toStdString() );
740}
741#else
742std::string QgsLazDecoder::toNativePath( const QString &filename )
743{
744 return filename.toStdString();
745}
746#endif
747
Extracts information contained in a LAZ file, such as the public header block and variable length rec...
Definition qgslazinfo.h:38
int extrabytesCount() const
Returns the number of extrabytes contained in the LAZ dataset.
Definition qgslazinfo.h:99
QgsVector3D scale() const
Returns the scale of the points coordinates.
Definition qgslazinfo.h:73
int pointFormat() const
Returns the point format of the point records contained in the LAZ file.
Definition qgslazinfo.h:81
QVector< ExtraBytesAttributeDetails > extrabytes() const
Returns the list of extrabytes contained in the LAZ file.
Definition qgslazinfo.h:119
QgsVector3D offset() const
Returns the offset of the points coordinates.
Definition qgslazinfo.h:75
int pointRecordLength() const
Returns the length of each point record in bytes.
Definition qgslazinfo.h:97
static QVector< ExtraBytesAttributeDetails > parseExtrabytes(char *rawData, int length, int pointRecordLength)
Static function to parse the raw extrabytes VLR into a list of recognizable extrabyte attributes.
A collection of point cloud attributes.
int pointRecordSize() const
Returns total size of record.
const QgsPointCloudAttribute * find(const QString &attributeName, int &offset) const
Finds the attribute with the name.
QVector< QgsPointCloudAttribute > attributes() const
Returns all attributes.
Attribute for point cloud data pair of name and size in bytes.
DataType
Systems of unit measurement.
@ UShort
Unsigned short int 2 bytes.
@ UChar
Unsigned char 1 byte.
@ UInt32
Unsigned int32 4 bytes.
@ UInt64
Unsigned int64 8 bytes.
double convertValueToDouble(const char *ptr) const
Returns the attribute's value as a double for data pointed to by ptr.
A rectangle specified with double values.
bool contains(const QgsRectangle &rect) const
Returns true when rectangle contains other rectangle.
double xMinimum
double yMinimum
double xMaximum
void setYMinimum(double y)
Set the minimum y value.
void setXMinimum(double x)
Set the minimum x value.
void setYMaximum(double y)
Set the maximum y value.
void setXMaximum(double x)
Set the maximum x value.
double yMaximum
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:60
double x() const
Returns X coordinate.
Definition qgsvector3d.h:58
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:63
#define QgsDebugError(str)
Definition qgslogger.h:59