20#include "lazperf/vlr.hpp"
27using namespace Qt::StringLiterals;
36 if ( mVersion.first == 1 && mVersion.second == 4 )
38 if ( mVersion.first == 1 && mVersion.second == 3 )
40 if ( mVersion.first == 1 && mVersion.second <= 2 )
48 if ( std::string( data, 4 ) !=
"LASF" )
50 mError = u
"Supplied header is not from a LAZ file"_s;
54 std::istringstream file( std::string( data, length ) );
55 lazperf::header14
header = lazperf::header14::create( file );
63 uint64_t currentOffset = 0;
64 for ( uint64_t i = 0; i < ( uint64_t ) mVlrCount && currentOffset < length; ++i )
66 lazperf::vlr_header vlrHeader;
67 vlrHeader.fill( data + currentOffset, 54 );
70 vlr.
userId = QString::fromStdString( vlrHeader.user_id );
72 vlr.
data = QByteArray( data + currentOffset + 54, vlrHeader.data_length );
73 mVlrVector.push_back( vlr );
74 currentOffset += 54 + vlrHeader.data_length;
78 parseExtrabyteAttributes();
82void QgsLazInfo::parseHeader( lazperf::header14 &header )
88 mCreationYearDay = QPair<uint16_t, uint16_t>(
header.creation.year,
header.creation.day );
89 mVersion = QPair<uint8_t, uint8_t>(
header.version.major,
header.version.minor );
90 mPointFormat =
header.pointFormat();
92 mProjectId = QString( QByteArray(
header.guid, 16 ).toHex() );
93 mSystemId = QString::fromLocal8Bit(
header.system_identifier, 32 );
94 while ( !mSystemId.isEmpty() && mSystemId.back() ==
'\0' )
96 mSystemId.remove( mSystemId.size() - 1, 1 );
98 mSoftwareId = QString::fromLocal8Bit(
header.generating_software, 32 ).trimmed();
99 while ( !mSoftwareId.isEmpty() && mSoftwareId.back() ==
'\0' )
101 mSoftwareId.remove( mSoftwareId.size() - 1, 1 );
107 mVlrCount =
header.vlr_count;
109 parseLazAttributes();
113void QgsLazInfo::parseCrs()
116 for (
LazVlr &vlr : mVlrVector )
118 if ( vlr.userId.trimmed() ==
"LASF_Projection"_L1 && vlr.recordId == 2112 )
128 QVariantMap metadata;
129 metadata[u
"creation_year"_s] = mHeader.creation.year;
130 metadata[u
"creation_day"_s] = mHeader.creation.day;
131 metadata[u
"major_version"_s] = mHeader.version.major;
132 metadata[u
"minor_version"_s] = mHeader.version.minor;
133 metadata[u
"dataformat_id"_s] = mHeader.pointFormat();
134 metadata[u
"scale_x"_s] = mScale.x();
135 metadata[u
"scale_y"_s] = mScale.y();
136 metadata[u
"scale_z"_s] = mScale.z();
137 metadata[u
"offset_x"_s] = mOffset.x();
138 metadata[u
"offset_y"_s] = mOffset.y();
139 metadata[u
"offset_z"_s] = mOffset.z();
140 metadata[u
"project_id"_s] = QString( QByteArray( mHeader.guid, 16 ).toHex() );
141 metadata[u
"system_id"_s] = QString::fromLocal8Bit( mHeader.system_identifier, 32 );
142 metadata[u
"software_id"_s] = QString::fromLocal8Bit( mHeader.generating_software, 32 );
148 for (
LazVlr vlr : mVlrVector )
150 if ( vlr.userId == userId && vlr.recordId == recordId )
158void QgsLazInfo::parseLazAttributes()
160 if ( mPointFormat < 0 || mPointFormat > 10 )
162 QgsDebugMsgLevel( u
"Invalid point record format %1"_s.arg( mPointFormat ), 2 );
182 if ( mPointFormat == 6 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 9 || mPointFormat == 10 )
186 if ( mPointFormat != 0 && mPointFormat != 2 )
190 if ( mPointFormat == 2 || mPointFormat == 3 || mPointFormat == 5 || mPointFormat == 7 || mPointFormat == 8 || mPointFormat == 10 )
196 if ( mPointFormat == 8 || mPointFormat == 10 )
203void QgsLazInfo::parseExtrabyteAttributes()
205 QByteArray ebVlrRaw =
vlrData(
"LASF_Spec", 4 );
208 for ( QgsLazInfo::ExtraBytesAttributeDetails attr : mExtrabyteAttributes )
210 mAttributes.push_back( QgsPointCloudAttribute( attr.attribute, attr.type ) );
216 QVector<QgsLazInfo::ExtraBytesAttributeDetails> extrabyteAttributes;
217 lazperf::eb_vlr ebVlr;
218 ebVlr.fill( rawData, length );
219 for ( std::vector<lazperf::eb_vlr::ebfield>::reverse_iterator it = ebVlr.items.rbegin(); it != ebVlr.items.rend(); ++it )
221 lazperf::eb_vlr::ebfield &field = *it;
223 ebAtrr.
attribute = QString::fromStdString( field.name );
224 switch ( field.data_type )
228 ebAtrr.
size = field.options;
272 ebAtrr.
size = field.options;
275 int accOffset = ( extrabyteAttributes.empty() ?
pointRecordLength : extrabyteAttributes.back().offset ) - ebAtrr.
size;
276 ebAtrr.
offset = accOffset;
277 extrabyteAttributes.push_back( ebAtrr );
279 return extrabyteAttributes;
286 char headerRawData[375];
288 file.read( headerRawData, 375 );
292 std::unique_ptr<char[]> vlrEntriesRawData(
new char[vlrDataSize] );
294 file.read( vlrEntriesRawData.get(), vlrDataSize );
306 QNetworkRequest nr( url );
308 nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
309 nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
310 nr.setRawHeader(
"Range",
"bytes=0-374" );
318 if ( req.
reply().
attribute( QNetworkRequest::HttpStatusCodeAttribute ).toInt() == 200 )
324 lazInfo.mError = u
"Range query 0-374 to \"%1\" failed: \"%2\""_s.arg( url.toString() ).arg( req.
errorMessage() );
331 QByteArray lazHeaderData = reply.
content();
333 lazInfo.
parseRawHeader( lazHeaderData.data(), lazHeaderData.size() );
336 const QUrl requestedUrl = reply.
request().url();
337 if ( requestedUrl != url && authcfg.isEmpty() )
339 url.setUrl( requestedUrl.toString() );
345 QNetworkRequest nr( url );
347 nr.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::AlwaysNetwork );
348 nr.setAttribute( QNetworkRequest::CacheSaveControlAttribute,
false );
350 QByteArray vlrRequestRange = u
"bytes=%1-%2"_s.arg( firstVlrOffset ).arg( lazInfo.
firstPointRecordOffset() - 1 ).toLocal8Bit();
351 nr.setRawHeader(
"Range", vlrRequestRange );
359 lazInfo.mError = u
"Range query %1-%2 to \"%3\" failed: \"%4\""_s.arg( firstVlrOffset ).arg( lazInfo.
firstPointRecordOffset() - 1 ).arg( url.toString() ).arg( req.
errorMessage() );
A thread safe class for performing blocking (sync) network requests, with full support for QGIS proxy...
void setAuthCfg(const QString &authCfg)
Sets the authentication config id which should be used during the request.
QString errorMessage() const
Returns the error message string, after a get(), post(), head() or put() request has been made.
ErrorCode get(QNetworkRequest &request, bool forceRefresh=false, QgsFeedback *feedback=nullptr, RequestFlags requestFlags=QgsBlockingNetworkRequest::RequestFlags())
Performs a "get" operation on the specified request.
@ 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.
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.
lazperf::header14 header() const
Returns the LAZPERF header object.
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.
QgsLazInfo()
Constructor for an empty laz info parser.
int pointRecordLength() const
Returns the length of each point record in bytes.
static QgsLazInfo fromUrl(QUrl &url, const QString &authcfg=QString())
Static function to create a QgsLazInfo class from a file over network.
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...
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code.
QByteArray content() const
Returns the reply content.
QNetworkRequest request() const
Returns the original network request.
@ UShort
Unsigned short int 2 bytes.
@ Short
Short int 2 bytes.
@ UChar
Unsigned char 1 byte.
@ UInt32
Unsigned int32 4 bytes.
@ UInt64
Unsigned int64 8 bytes.
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
#define QgsDebugMsgLevel(str, level)
#define QgsDebugError(str)
#define QgsSetRequestInitiatorClass(request, _class)