QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgslazinfo.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslazinfo.cpp
3 --------------------
4 begin : April 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 "qgslazinfo.h"
19
20#include "qgslogger.h"
23
24#include "lazperf/readers.hpp"
25
26// QgsLazInfo
27
29
31{
32 if ( mVersion.first == 1 && mVersion.second == 4 )
33 return 375;
34 if ( mVersion.first == 1 && mVersion.second == 3 )
35 return 235;
36 if ( mVersion.first == 1 && mVersion.second <= 2 )
37 return 227;
38 return 0;
39}
40
41void QgsLazInfo::parseRawHeader( char *data, uint64_t length )
42{
43 mIsValid = true;
44 if ( std::string( data, 4 ) != "LASF" )
45 {
46 mError = QStringLiteral( "Supplied header is not from a LAZ file" );
47 mIsValid = false;
48 return;
49 }
50 std::istringstream file( std::string( data, length ) );
51 lazperf::header14 header = lazperf::header14::create( file );
52 parseHeader( header );
53}
54
55void QgsLazInfo::parseRawVlrEntries( char *data, uint64_t length )
56{
57 if ( !mIsValid )
58 return;
59 uint64_t currentOffset = 0;
60 for ( uint64_t i = 0; i < ( uint64_t )mVlrCount && currentOffset < length; ++i )
61 {
62 lazperf::vlr_header vlrHeader;
63 vlrHeader.fill( data + currentOffset, 54 );
64
65 LazVlr vlr;
66 vlr.userId = QString::fromStdString( vlrHeader.user_id );
67 vlr.recordId = vlrHeader.record_id;
68 vlr.data = QByteArray( data + currentOffset + 54, vlrHeader.data_length );
69 mVlrVector.push_back( vlr );
70 currentOffset += 54 + vlrHeader.data_length;
71 }
72
73 parseCrs();
74 parseExtrabyteAttributes();
75}
76
77
78void QgsLazInfo::parseHeader( lazperf::header14 &header )
79{
80 mHeader = header;
81
82 mScale = QgsVector3D( header.scale.x, header.scale.y, header.scale.z );
83 mOffset = QgsVector3D( header.offset.x, header.offset.y, header.offset.z );
84 mCreationYearDay = QPair<uint16_t, uint16_t>( header.creation.year, header.creation.day );
85 mVersion = QPair<uint8_t, uint8_t>( header.version.major, header.version.minor );
86 mPointFormat = header.pointFormat();
87
88 mProjectId = QString( QByteArray( header.guid, 16 ).toHex() );
89 mSystemId = QString::fromLocal8Bit( header.system_identifier, 32 );
90 while ( !mSystemId.isEmpty() && mSystemId.back() == '\0' )
91 {
92 mSystemId.remove( mSystemId.size() - 1, 1 );
93 }
94 mSoftwareId = QString::fromLocal8Bit( header.generating_software, 32 ).trimmed();
95 while ( !mSoftwareId.isEmpty() && mSoftwareId.back() == '\0' )
96 {
97 mSoftwareId.remove( mSoftwareId.size() - 1, 1 );
98 }
99
100 mMinCoords = QgsVector3D( header.minx, header.miny, header.minz );
101 mMaxCoords = QgsVector3D( header.maxx, header.maxy, header.maxz );
102
103 mVlrCount = header.vlr_count;
104
105 parseLazAttributes();
106}
107
108
109void QgsLazInfo::parseCrs()
110{
111 // TODO: handle other kind of CRS in the laz spec
112 for ( LazVlr &vlr : mVlrVector )
113 {
114 if ( vlr.userId.trimmed() == QLatin1String( "LASF_Projection" ) && vlr.recordId == 2112 )
115 {
116 mCrs = QgsCoordinateReferenceSystem::fromWkt( QString::fromStdString( vlr.data.toStdString() ) );
117 break;
118 }
119 }
120}
121
122QVariantMap QgsLazInfo::toMetadata() const
123{
124 QVariantMap metadata;
125 metadata[ QStringLiteral( "creation_year" ) ] = mHeader.creation.year;
126 metadata[ QStringLiteral( "creation_day" ) ] = mHeader.creation.day;
127 metadata[ QStringLiteral( "major_version" ) ] = mHeader.version.major;
128 metadata[ QStringLiteral( "minor_version" ) ] = mHeader.version.minor;
129 metadata[ QStringLiteral( "dataformat_id" ) ] = mHeader.pointFormat();
130 metadata[ QStringLiteral( "scale_x" ) ] = mScale.x();
131 metadata[ QStringLiteral( "scale_y" ) ] = mScale.y();
132 metadata[ QStringLiteral( "scale_z" ) ] = mScale.z();
133 metadata[ QStringLiteral( "offset_x" ) ] = mOffset.x();
134 metadata[ QStringLiteral( "offset_y" ) ] = mOffset.y();
135 metadata[ QStringLiteral( "offset_z" ) ] = mOffset.z();
136 metadata[ QStringLiteral( "project_id" ) ] = QString( QByteArray( mHeader.guid, 16 ).toHex() );
137 metadata[ QStringLiteral( "system_id" ) ] = QString::fromLocal8Bit( mHeader.system_identifier, 32 );
138 metadata[ QStringLiteral( "software_id" ) ] = QString::fromLocal8Bit( mHeader.generating_software, 32 );
139 return metadata;
140}
141
142QByteArray QgsLazInfo::vlrData( QString userId, int recordId )
143{
144 for ( LazVlr vlr : mVlrVector )
145 {
146 if ( vlr.userId == userId && vlr.recordId == recordId )
147 {
148 return vlr.data;
149 }
150 }
151 return QByteArray();
152}
153
154void QgsLazInfo::parseLazAttributes()
155{
156 if ( mPointFormat < 0 || mPointFormat > 10 )
157 {
158 QgsDebugMsgLevel( QStringLiteral( "Invalid point record format %1" ).arg( mPointFormat ), 2 );
159 return;
160 }
165 mAttributes.push_back( QgsPointCloudAttribute( "ReturnNumber", QgsPointCloudAttribute::Char ) );
166 mAttributes.push_back( QgsPointCloudAttribute( "NumberOfReturns", QgsPointCloudAttribute::Char ) );
167 mAttributes.push_back( QgsPointCloudAttribute( "ScanDirectionFlag", QgsPointCloudAttribute::Char ) );
168 mAttributes.push_back( QgsPointCloudAttribute( "EdgeOfFlightLine", QgsPointCloudAttribute::Char ) );
169 mAttributes.push_back( QgsPointCloudAttribute( "Classification", QgsPointCloudAttribute::UChar ) );
170 mAttributes.push_back( QgsPointCloudAttribute( "ScanAngleRank", QgsPointCloudAttribute::Short ) );
172 mAttributes.push_back( QgsPointCloudAttribute( "PointSourceId", QgsPointCloudAttribute::UShort ) );
177
178 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
179 {
180 mAttributes.push_back( QgsPointCloudAttribute( "ScannerChannel", QgsPointCloudAttribute::Char ) );
181 }
182 if ( mPointFormat != 0 && mPointFormat != 2 )
183 {
185 }
186 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
187 {
188 mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "Red" ), QgsPointCloudAttribute::UShort ) );
189 mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "Green" ), QgsPointCloudAttribute::UShort ) );
190 mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "Blue" ), QgsPointCloudAttribute::UShort ) );
191 }
192 if ( mPointFormat == 8 || mPointFormat == 10 )
193 {
194 mAttributes.push_back( QgsPointCloudAttribute( QStringLiteral( "Infrared" ), QgsPointCloudAttribute::UShort ) );
195 }
196 // Note: wave packet attributes are not handled and are unreadable
197}
198
199void QgsLazInfo::parseExtrabyteAttributes()
200{
201 QByteArray ebVlrRaw = vlrData( "LASF_Spec", 4 );
202 mExtrabyteAttributes = QgsLazInfo::parseExtrabytes( ebVlrRaw.data(), ebVlrRaw.size(), mHeader.point_record_length );
203
204 for ( QgsLazInfo::ExtraBytesAttributeDetails attr : mExtrabyteAttributes )
205 {
206 mAttributes.push_back( QgsPointCloudAttribute( attr.attribute, attr.type ) );
207 }
208}
209
210QVector<QgsLazInfo::ExtraBytesAttributeDetails> QgsLazInfo::parseExtrabytes( char *rawData, int length, int pointRecordLength )
211{
212 QVector<QgsLazInfo::ExtraBytesAttributeDetails> extrabyteAttributes;
213 lazperf::eb_vlr ebVlr;
214 ebVlr.fill( rawData, length );
215 for ( std::vector<lazperf::eb_vlr::ebfield>::reverse_iterator it = ebVlr.items.rbegin(); it != ebVlr.items.rend(); ++it )
216 {
217 lazperf::eb_vlr::ebfield &field = *it;
219 ebAtrr.attribute = QString::fromStdString( field.name );
220 switch ( field.data_type )
221 {
222 case 0:
224 ebAtrr.size = field.options;
225 break;
226 case 1:
228 ebAtrr.size = 1;
229 break;
230 case 2:
232 ebAtrr.size = 1;
233 break;
234 case 3:
236 ebAtrr.size = 2;
237 break;
238 case 4:
240 ebAtrr.size = 2;
241 break;
242 case 5:
244 ebAtrr.size = 4;
245 break;
246 case 6:
248 ebAtrr.size = 4;
249 break;
250 case 7:
252 ebAtrr.size = 8;
253 break;
254 case 8:
256 ebAtrr.size = 8;
257 break;
258 case 9:
260 ebAtrr.size = 4;
261 break;
262 case 10:
264 ebAtrr.size = 8;
265 break;
266 default:
268 ebAtrr.size = field.options;
269 break;
270 }
271 int accOffset = ( extrabyteAttributes.empty() ? pointRecordLength : extrabyteAttributes.back().offset ) - ebAtrr.size;
272 ebAtrr.offset = accOffset;
273 extrabyteAttributes.push_back( ebAtrr );
274 }
275 return extrabyteAttributes;
276}
277
278QgsLazInfo QgsLazInfo::fromFile( std::ifstream &file )
279{
280 QgsLazInfo lazInfo;
281
282 char headerRawData[ 375 ];
283 file.seekg( 0 );
284 file.read( headerRawData, 375 );
285 lazInfo.parseRawHeader( headerRawData, 375 );
286
287 int vlrDataSize = lazInfo.firstPointRecordOffset() - lazInfo.firstVariableLengthRecord();
288 std::unique_ptr<char[]> vlrEntriesRawData( new char[ vlrDataSize ] );
289 file.seekg( lazInfo.firstVariableLengthRecord() );
290 file.read( vlrEntriesRawData.get(), vlrDataSize );
291 lazInfo.parseRawVlrEntries( vlrEntriesRawData.get(), vlrDataSize );
292
293 return lazInfo;
294}
295
297{
298 QgsLazInfo lazInfo;
299
300 if ( !supportsRangeQueries( url ) )
301 {
302 lazInfo.mError = QStringLiteral( "The server of submitted URL doesn't support range queries" );
303 return lazInfo;
304 }
305
306 // Fetch header data
307 {
308 QNetworkRequest nr( url );
309 QgsSetRequestInitiatorClass( nr, QStringLiteral( "QgsLazInfo" ) );
310 nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
311 nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
312 nr.setRawHeader( "Range", "bytes=0-374" );
314 QgsBlockingNetworkRequest::ErrorCode errCode = req.get( nr );
315 if ( errCode != QgsBlockingNetworkRequest::NoError )
316 {
317 QgsDebugError( QStringLiteral( "Request failed: " ) + url.toString() );
318 lazInfo.mError = QStringLiteral( "Range query 0-374 to \"%1\" failed: \"%2\"" ).arg( url.toString() ).arg( req.errorMessage() );
319 return lazInfo;
320 }
321
322 const QgsNetworkReplyContent reply = req.reply();
323 QByteArray lazHeaderData = reply.content();
324
325 lazInfo.parseRawHeader( lazHeaderData.data(), lazHeaderData.size() );
326 }
327
328 // Fetch VLR data
329 {
330 QNetworkRequest nr( url );
331 QgsSetRequestInitiatorClass( nr, QStringLiteral( "QgsLazInfo" ) );
332 nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
333 nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
334 uint32_t firstVlrOffset = lazInfo.firstVariableLengthRecord();
335 QByteArray vlrRequestRange = QStringLiteral( "bytes=%1-%2" ).arg( firstVlrOffset ).arg( lazInfo.firstPointRecordOffset() - 1 ).toLocal8Bit();
336 nr.setRawHeader( "Range", vlrRequestRange );
338 QgsBlockingNetworkRequest::ErrorCode errCode = req.get( nr );
339 if ( errCode != QgsBlockingNetworkRequest::NoError )
340 {
341 QgsDebugError( QStringLiteral( "Request failed: " ) + url.toString() );
342
343 lazInfo.mError = QStringLiteral( "Range query %1-%2 to \"%3\" failed: \"%4\"" ).arg( firstVlrOffset ).arg( lazInfo.firstPointRecordOffset() - 1 )
344 .arg( url.toString() ).arg( req.errorMessage() );
345 return lazInfo;
346 }
347 QByteArray vlrDataRaw = req.reply().content();
348
349 lazInfo.parseRawVlrEntries( vlrDataRaw.data(), vlrDataRaw.size() );
350 }
351
352 return lazInfo;
353}
354
356{
357 QNetworkRequest nr( url );
358 QgsSetRequestInitiatorClass( nr, QStringLiteral( "QgsLazInfo" ) );
359 nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
360 nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute, false );
361 nr.setRawHeader( "Range", "bytes=0-0" );
363 // ignore the reply's status, we only care if accept-ranges is in the headers
364 req.head( nr );
365 QgsNetworkReplyContent reply = req.reply();
366
367 const QString acceptRangesHeader = reply.rawHeader( QStringLiteral( "Accept-Ranges" ).toLocal8Bit() );
368 return acceptRangesHeader.compare( QStringLiteral( "bytes" ), Qt::CaseSensitivity::CaseInsensitive ) == 0;
369}
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "get" operation on the specified request.
ErrorCode head(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr)
Performs a "head" operation on the specified request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
@ NoError
No error was encountered.
QgsNetworkReplyContent reply() const
Returns the content of the network reply, after a get(), post(), head() or put() request has been mad...
static QgsCoordinateReferenceSystem fromWkt(const QString &wkt)
Creates a CRS from a WKT spatial ref sys definition string.
Class for extracting information contained in LAZ file such as the public header block and variable l...
Definition qgslazinfo.h:39
uint32_t firstVariableLengthRecord() const
Returns the absolute offset to the first variable length record in the LAZ file.
QByteArray vlrData(QString userId, int recordId)
Returns the binary data of the variable length record with the user identifier userId and record iden...
uint32_t firstPointRecordOffset() const
Returns the absolute offset to the first point record in the LAZ file.
Definition qgslazinfo.h:97
static QgsLazInfo fromUrl(QUrl &url)
Static function to create a QgsLazInfo class from a file over network.
lazperf::header14 header() const
Returns the LAZPERF header object.
Definition qgslazinfo.h:127
void parseRawVlrEntries(char *data, uint64_t length)
Parses the variable length records found in the array data of length length.
QVariantMap toMetadata() const
Returns a map containing various metadata extracted from the LAZ file.
void parseRawHeader(char *data, uint64_t length)
Parses the raw header data loaded from a LAZ file.
static QgsLazInfo fromFile(std::ifstream &file)
Static function to create a QgsLazInfo class from a file.
static bool supportsRangeQueries(QUrl &url)
Static function to check whether the server of URL url supports range queries.
QgsLazInfo()
Constructor for an empty laz info parser.
int pointRecordLength() const
Returns the length of each point record in bytes.
Definition qgslazinfo.h:101
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.
Encapsulates a network reply within a container which is inexpensive to copy and safe to pass between...
QByteArray content() const
Returns the reply content.
QByteArray rawHeader(const QByteArray &headerName) const
Returns the content of the header with the specified headerName, or an empty QByteArray if the specif...
void push_back(const QgsPointCloudAttribute &attribute)
Adds extra attribute.
Attribute for point cloud data pair of name and size in bytes.
@ UShort
Unsigned short int 2 bytes.
@ UChar
Unsigned char 1 byte.
@ UInt32
Unsigned int32 4 bytes.
@ UInt64
Unsigned int64 8 bytes.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
double y() const
Returns Y coordinate.
Definition qgsvector3d.h:50
double z() const
Returns Z coordinate.
Definition qgsvector3d.h:52
double x() const
Returns X coordinate.
Definition qgsvector3d.h:48
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
#define QgsSetRequestInitiatorClass(request, _class)
QgsPointCloudAttribute::DataType type
Definition qgslazinfo.h:51
QByteArray data
Definition qgslazinfo.h:45