QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgspointcloudindex.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudindex.cpp
3 --------------------
4 begin : October 2020
5 copyright : (C) 2020 by Peter Petrik
6 email : zilolv 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 "qgspointcloudindex.h"
19#include "moc_qgspointcloudindex.cpp"
20#include <QFile>
21#include <QFileInfo>
22#include <QDir>
23#include <QJsonArray>
24#include <QJsonDocument>
25#include <QJsonObject>
26#include <QTime>
27#include <QtDebug>
28
31#include "qgslogger.h"
32
34 mD( -1 ),
35 mX( 0 ),
36 mY( 0 ),
37 mZ( 0 )
38{}
39
40IndexedPointCloudNode::IndexedPointCloudNode( int _d, int _x, int _y, int _z ):
41 mD( _d ),
42 mX( _x ),
43 mY( _y ),
44 mZ( _z )
45{}
46
48{
49 return IndexedPointCloudNode( mD - 1, mX / 2, mY / 2, mZ / 2 );
50}
51
53{
54 QStringList lst = str.split( '-' );
55 if ( lst.count() != 4 )
56 return IndexedPointCloudNode();
57 return IndexedPointCloudNode( lst[0].toInt(), lst[1].toInt(), lst[2].toInt(), lst[3].toInt() );
58}
59
61{
62 return QStringLiteral( "%1-%2-%3-%4" ).arg( mD ).arg( mX ).arg( mY ).arg( mZ );
63}
64
66{
67 return mD;
68}
69
71{
72 return mX;
73}
74
76{
77 return mY;
78}
79
81{
82 return mZ;
83}
84
86{
87 return id.d() + id.x() + id.y() + id.z();
88}
89
91
92//
93// QgsPointCloudCacheKey
94//
95
96QgsPointCloudCacheKey::QgsPointCloudCacheKey( const IndexedPointCloudNode &n, const QgsPointCloudRequest &request, const QgsPointCloudExpression &expression, const QString &uri )
97 : mNode( n )
98 , mUri( uri )
99 , mRequest( request )
100 , mFilterExpression( expression )
101{
102}
103
105{
106 return mNode == other.mNode &&
107 mUri == other.mUri &&
108 mRequest == other.mRequest &&
109 mFilterExpression == other.mFilterExpression;
110}
111
112uint qHash( const QgsPointCloudCacheKey &key )
113{
114 return qHash( key.node() ) ^ qHash( key.request() ) ^ qHash( key.uri() ) ^ qHash( key.filterExpression() );
115}
116
117//
118// QgsPointCloudDataBounds
119//
120
122
123QgsPointCloudDataBounds::QgsPointCloudDataBounds( qint64 xmin, qint64 ymin, qint64 zmin, qint64 xmax, qint64 ymax, qint64 zmax )
124 : mXMin( xmin )
125 , mYMin( ymin )
126 , mZMin( zmin )
127 , mXMax( xmax )
128 , mYMax( ymax )
129 , mZMax( zmax )
130{
131
132}
133
135{
136 return mXMin;
137}
138
140{
141 return mYMin;
142}
143
145{
146 return mXMax;
147}
148
150{
151 return mYMax;
152}
153
155{
156 return mZMin;
157}
158
160{
161 return mZMax;
162}
163
165{
166 return QgsRectangle(
167 mXMin * scale.x() + offset.x(), mYMin * scale.y() + offset.y(),
168 mXMax * scale.x() + offset.x(), mYMax * scale.y() + offset.y()
169 );
170}
171
173{
174 return QgsDoubleRange( mZMin * scale.z() + offset.z(), mZMax * scale.z() + offset.z() );
175}
176
178
179//
180// QgsPointCloudIndex
181//
182
184QCache<QgsPointCloudCacheKey, QgsPointCloudBlock> QgsPointCloudIndex::sBlockCache( 200'000'000 ); // 200MB of cached points
185
187
189
191{
192 QMutexLocker locker( &mHierarchyMutex );
193 return mHierarchy.contains( n );
194}
195
197{
198 QMutexLocker locker( &mHierarchyMutex );
199 return mHierarchy.value( n, -1 );
200}
201
202QList<IndexedPointCloudNode> QgsPointCloudIndex::nodeChildren( const IndexedPointCloudNode &n ) const
203{
204 Q_ASSERT( hasNode( n ) );
205 QList<IndexedPointCloudNode> lst;
206 const int d = n.d() + 1;
207 const int x = n.x() * 2;
208 const int y = n.y() * 2;
209 const int z = n.z() * 2;
210
211 for ( int i = 0; i < 8; ++i )
212 {
213 int dx = i & 1, dy = !!( i & 2 ), dz = !!( i & 4 );
214 const IndexedPointCloudNode n2( d, x + dx, y + dy, z + dz );
215 if ( hasNode( n2 ) )
216 lst.append( n2 );
217 }
218 return lst;
219}
220
225
227{
228 qint64 xMin, yMin, zMin, xMax, yMax, zMax;
229
230 const qint64 d = mRootBounds.xMax() - mRootBounds.xMin();
231 const double dLevel = ( double )d / pow( 2, n.d() );
232
233 xMin = round( mRootBounds.xMin() + dLevel * n.x() );
234 xMax = round( mRootBounds.xMin() + dLevel * ( n.x() + 1 ) );
235 yMin = round( mRootBounds.yMin() + dLevel * n.y() );
236 yMax = round( mRootBounds.yMin() + dLevel * ( n.y() + 1 ) );
237 zMin = round( mRootBounds.zMin() + dLevel * n.z() );
238 zMax = round( mRootBounds.zMin() + dLevel * ( n.z() + 1 ) );
239
240 QgsPointCloudDataBounds db( xMin, yMin, zMin, xMax, yMax, zMax );
241 return db;
242}
243
248
253
255{
256 const double w = nodeMapExtent( n ).width();
257 return w / mSpan;
258}
259
261{
262 return mScale;
263}
264
266{
267 return mOffset;
268}
269
274
276{
277 return mSpan;
278}
279
280bool QgsPointCloudIndex::setSubsetString( const QString &subset )
281{
282 const QString lastExpression = mFilterExpression;
283 mFilterExpression.setExpression( subset );
284 if ( mFilterExpression.hasParserError() && !subset.isEmpty() )
285 {
286 mFilterExpression.setExpression( lastExpression );
287 return false;
288 }
289
290 // fail if expression references unknown attributes
291 int offset;
292 const QSet<QString> attributes = mFilterExpression.referencedAttributes();
293 for ( const QString &attribute : attributes )
294 {
295 if ( !mAttributes.find( attribute, offset ) )
296 {
297 mFilterExpression.setExpression( lastExpression );
298 return false;
299 }
300 }
301 return true;
302}
303
305{
306 return mFilterExpression;
307}
308
309QVariant QgsPointCloudIndex::metadataStatistic( const QString &attribute, Qgis::Statistic statistic ) const
310{
311 if ( attribute == QLatin1String( "X" ) && statistic == Qgis::Statistic::Min )
312 return mExtent.xMinimum();
313 if ( attribute == QLatin1String( "X" ) && statistic == Qgis::Statistic::Max )
314 return mExtent.xMaximum();
315
316 if ( attribute == QLatin1String( "Y" ) && statistic == Qgis::Statistic::Min )
317 return mExtent.yMinimum();
318 if ( attribute == QLatin1String( "Y" ) && statistic == Qgis::Statistic::Max )
319 return mExtent.yMaximum();
320
321 if ( attribute == QLatin1String( "Z" ) && statistic == Qgis::Statistic::Min )
322 return mZMin;
323 if ( attribute == QLatin1String( "Z" ) && statistic == Qgis::Statistic::Max )
324 return mZMax;
325
326 return QVariant();
327}
328
329QVariantList QgsPointCloudIndex::metadataClasses( const QString &attribute ) const
330{
331 Q_UNUSED( attribute );
332 return QVariantList();
333}
334
335QVariant QgsPointCloudIndex::metadataClassStatistic( const QString &attribute, const QVariant &value, Qgis::Statistic statistic ) const
336{
337 Q_UNUSED( attribute );
338 Q_UNUSED( value );
339 Q_UNUSED( statistic );
340 return QVariant();
341}
342
344{
345 QMap<QString, QgsPointCloudAttributeStatistics> statsMap;
346 for ( QgsPointCloudAttribute attribute : attributes().attributes() )
347 {
348 QString name = attribute.name();
350 QVariant min = metadataStatistic( name, Qgis::Statistic::Min );
351 QVariant max = metadataStatistic( name, Qgis::Statistic::Max );
352 QVariant mean = metadataStatistic( name, Qgis::Statistic::Mean );
353 QVariant stDev = metadataStatistic( name, Qgis::Statistic::StDev );
354 if ( !min.isValid() )
355 continue;
356
357 s.minimum = min.toDouble();
358 s.maximum = max.toDouble();
359 s.mean = mean.toDouble();
360 s.stDev = stDev.toDouble();
362 QVariantList classes = metadataClasses( name );
363 for ( QVariant c : classes )
364 {
365 s.classCount[ c.toInt() ] = metadataClassStatistic( name, c, Qgis::Statistic::Count ).toInt();
366 }
367 statsMap[ name ] = s;
368 }
369 return QgsPointCloudStatistics( pointCount(), statsMap );
370}
371
373{
374 // Base QgsPointCloudIndex fields
375 destination->mUri = mUri;
376 destination->mExtent = mExtent;
377 destination->mZMin = mZMin;
378 destination->mZMax = mZMax;
379 destination->mHierarchy = mHierarchy;
380 destination->mScale = mScale;
381 destination->mOffset = mOffset;
382 destination->mRootBounds = mRootBounds;
383 destination->mAttributes = mAttributes;
384 destination->mSpan = mSpan;
386}
387
389{
390 QgsPointCloudCacheKey key( node, request, mFilterExpression, mUri );
391
392 QMutexLocker l( &sBlockCacheMutex );
393 QgsPointCloudBlock *cached = sBlockCache.object( key );
394 return cached ? cached->clone() : nullptr;
395}
396
401
402void QgsPointCloudIndex::storeNodeDataToCacheStatic( QgsPointCloudBlock *data, const IndexedPointCloudNode &node, const QgsPointCloudRequest &request, const QgsPointCloudExpression &expression, const QString &uri )
403{
404 if ( !data )
405 return;
406
407 QgsPointCloudCacheKey key( node, request, expression, uri );
408
409 const int cost = data->pointCount() * data->pointRecordSize();
410
411 QMutexLocker l( &sBlockCacheMutex );
412 QgsDebugMsgLevel( QStringLiteral( "(%1/%2): Caching node %3 of %4" ).arg( sBlockCache.totalCost() ).arg( sBlockCache.maxCost() ).arg( key.node().toString() ).arg( key.uri() ), 4 );
413 sBlockCache.insert( key, data->clone(), cost );
414}
Represents a indexed point cloud node in octree.
int y() const
Returns y.
static IndexedPointCloudNode fromString(const QString &str)
Creates node from string.
int x() const
Returns x.
QString toString() const
Encode node to string.
int d() const
Returns d.
IndexedPointCloudNode parentNode() const
Returns the parent of the node.
int z() const
Returns z.
IndexedPointCloudNode()
Constructs invalid node.
Statistic
Available generic statistics.
Definition qgis.h:5446
@ StDev
Standard deviation of values.
@ Mean
Mean of values.
@ Max
Max of values.
@ Min
Min of values.
QgsRange which stores a range of double values.
Definition qgsrange.h:231
Collection of point cloud attributes.
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.
Base class for storing raw data from point cloud nodes.
int pointCount() const
Returns number of points that are stored in the block.
int pointRecordSize() const
Returns the total size of each individual point record.
QgsPointCloudBlock * clone() const
Clones the QgsPointCloudBlock returning a new copy.
Container class for QgsPointCloudBlock cache keys.
IndexedPointCloudNode node() const
Returns the key's IndexedPointCloudNode.
QgsPointCloudExpression filterExpression() const
Returns the key's QgsPointCloudExpression.
QgsPointCloudRequest request() const
Returns the key's QgsPointCloudRequest.
QgsPointCloudCacheKey(const IndexedPointCloudNode &n, const QgsPointCloudRequest &request, const QgsPointCloudExpression &expression, const QString &uri)
Ctor.
bool operator==(const QgsPointCloudCacheKey &other) const
QString uri() const
Returns the key's uri.
Represents packaged data bounds.
qint64 xMin() const
Returns x min.
qint64 zMin() const
Returns z min.
qint64 yMax() const
Returns y max.
qint64 xMax() const
Returns x max.
QgsDoubleRange zRange(const QgsVector3D &offset, const QgsVector3D &scale) const
Returns the z range, applying the specified offset and scale.
QgsPointCloudDataBounds()
Constructs invalid bounds.
QgsRectangle mapExtent(const QgsVector3D &offset, const QgsVector3D &scale) const
Returns 2D rectangle in map coordinates.
qint64 zMax() const
Returns z max.
qint64 yMin() const
Returns y min.
Represents a indexed point clouds data in octree.
int span() const
Returns the number of points in one direction in a single node.
double zMax() const
Returns z max.
QgsPointCloudBlock * getNodeDataFromCache(const IndexedPointCloudNode &node, const QgsPointCloudRequest &request)
Fetches the requested node data from the cache for the specified node and request.
static void storeNodeDataToCacheStatic(QgsPointCloudBlock *data, const IndexedPointCloudNode &node, const QgsPointCloudRequest &request, const QgsPointCloudExpression &expression, const QString &uri)
Stores existing data to the cache for the specified node, request, expression and uri.
virtual qint64 nodePointCount(const IndexedPointCloudNode &n) const
Returns the number of points of a given node n.
QgsRectangle nodeMapExtent(const IndexedPointCloudNode &node) const
Returns the extent of a node in map coordinates.
virtual QList< IndexedPointCloudNode > nodeChildren(const IndexedPointCloudNode &n) const
Returns all children of node.
QgsPointCloudIndex()
Constructs index.
QString subsetString() const
Returns the string used to define a subset of the point cloud.
double zMin() const
Returns z min.
void storeNodeDataToCache(QgsPointCloudBlock *data, const IndexedPointCloudNode &node, const QgsPointCloudRequest &request)
Stores existing data to the cache for the specified node and request.
QgsVector3D offset() const
Returns offset.
QgsVector3D scale() const
Returns scale.
virtual qint64 pointCount() const =0
Returns the number of points in the point cloud.
virtual QVariantList metadataClasses(const QString &attribute) const
Returns the classes of attribute.
void copyCommonProperties(QgsPointCloudIndex *destination) const
Copies common properties to the destination index.
bool setSubsetString(const QString &subset)
Sets the string used to define a subset of the point cloud.
QHash< IndexedPointCloudNode, int > mHierarchy
Data hierarchy.
double mZMax
Vertical extent of data.
QgsPointCloudDataBounds mRootBounds
Bounds of the root node's cube (in int32 coordinates)
virtual QgsPointCloudStatistics metadataStatistics() const
Returns the object containing the statistics metadata extracted from the dataset.
QgsRectangle mExtent
2D extent of data
QgsPointCloudAttributeCollection mAttributes
QgsPointCloudDataBounds nodeBounds(const IndexedPointCloudNode &node) const
Returns bounds of particular node.
virtual bool hasNode(const IndexedPointCloudNode &n) const
Returns whether the octree contain given node.
QgsVector3D mOffset
Offset of our int32 coordinates compared to CRS coords.
static QMutex sBlockCacheMutex
QgsDoubleRange nodeZRange(const IndexedPointCloudNode &node) const
Returns the z range of a node.
static QCache< QgsPointCloudCacheKey, QgsPointCloudBlock > sBlockCache
virtual QVariant metadataStatistic(const QString &attribute, Qgis::Statistic statistic) const
Returns the statistic statistic of attribute.
float nodeError(const IndexedPointCloudNode &n) const
Returns node's error in map units (used to determine in whether the node has enough detail for the cu...
int mSpan
All native attributes stored in the file.
QgsVector3D mScale
Scale of our int32 coordinates compared to CRS coords.
virtual QVariant metadataClassStatistic(const QString &attribute, const QVariant &value, Qgis::Statistic statistic) const
Returns the statistic statistic of the class value of the attribute attribute.
QgsPointCloudExpression mFilterExpression
The filter expression to be evaluated when fetching node data.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Sets native attributes of the data.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
Point cloud data request.
Class used to store statistics of a point cloud dataset.
A rectangle specified with double values.
double xMinimum
double yMinimum
double xMaximum
double yMaximum
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
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define str(x)
Definition qgis.cpp:39
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
uint qHash(IndexedPointCloudNode id)
Hash function for indexed nodes.
Class used to store statistics of one attribute of a point cloud dataset.