QGIS API Documentation 3.39.0-Master (d85f3c2a281)
Loading...
Searching...
No Matches
qgspointclouddataprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointclouddataprovider.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 "qgis.h"
20#include "qgspointcloudindex.h"
21#include "qgsgeometry.h"
23#include "qgsgeos.h"
25#include "qgsthreadingutils.h"
26
27#include <mutex>
28#include <QDebug>
29#include <QtMath>
30
31#include <QtConcurrent/QtConcurrentMap>
32
34 const QString &uri,
37 : QgsDataProvider( uri, options, flags )
38{
39}
40
42
49
51{
53
54 QgsPointCloudIndex *lIndex = index();
55 return lIndex && lIndex->isValid();
56}
57
64
66{
68
69 return QVariantMap();
70}
71
73{
75
76 return nullptr;
77}
78
80{
81 static QMap< int, QString > sCodes
82 {
83 {0, QStringLiteral( "Created, Never Classified" )},
84 {1, QStringLiteral( "Unclassified" )},
85 {2, QStringLiteral( "Ground" )},
86 {3, QStringLiteral( "Low Vegetation" )},
87 {4, QStringLiteral( "Medium Vegetation" )},
88 {5, QStringLiteral( "High Vegetation" )},
89 {6, QStringLiteral( "Building" )},
90 {7, QStringLiteral( "Low Point (Low Noise)" )},
91 {8, QStringLiteral( "Reserved" )},
92 {9, QStringLiteral( "Water" )},
93 {10, QStringLiteral( "Rail" )},
94 {11, QStringLiteral( "Road Surface" )},
95 {12, QStringLiteral( "Reserved" )},
96 {13, QStringLiteral( "Wire - Guard (Shield)" )},
97 {14, QStringLiteral( "Wire - Conductor (Phase)" )},
98 {15, QStringLiteral( "Transmission Tower" )},
99 {16, QStringLiteral( "Wire-Structure Connector (Insulator)" )},
100 {17, QStringLiteral( "Bridge Deck" )},
101 {18, QStringLiteral( "High Noise" )},
102 };
103
104 static std::once_flag initialized;
105 std::call_once( initialized, [ = ]( )
106 {
107 for ( int i = 19; i <= 63; ++i )
108 sCodes.insert( i, QStringLiteral( "Reserved" ) );
109 for ( int i = 64; i <= 255; ++i )
110 sCodes.insert( i, QStringLiteral( "User Definable" ) );
111 } );
112
113 return sCodes;
114}
115
117{
118 static QMap< int, QString > sCodes
119 {
120 {0, QObject::tr( "Created, Never Classified" )},
121 {1, QObject::tr( "Unclassified" )},
122 {2, QObject::tr( "Ground" )},
123 {3, QObject::tr( "Low Vegetation" )},
124 {4, QObject::tr( "Medium Vegetation" )},
125 {5, QObject::tr( "High Vegetation" )},
126 {6, QObject::tr( "Building" )},
127 {7, QObject::tr( "Low Point (Noise)" )},
128 {8, QObject::tr( "Reserved" )},
129 {9, QObject::tr( "Water" )},
130 {10, QObject::tr( "Rail" )},
131 {11, QObject::tr( "Road Surface" )},
132 {12, QObject::tr( "Reserved" )},
133 {13, QObject::tr( "Wire - Guard (Shield)" )},
134 {14, QObject::tr( "Wire - Conductor (Phase)" )},
135 {15, QObject::tr( "Transmission Tower" )},
136 {16, QObject::tr( "Wire-Structure Connector (Insulator)" )},
137 {17, QObject::tr( "Bridge Deck" )},
138 {18, QObject::tr( "High Noise" )},
139 };
140
141 static std::once_flag initialized;
142 std::call_once( initialized, [ = ]( )
143 {
144 for ( int i = 19; i <= 63; ++i )
145 sCodes.insert( i, QObject::tr( "Reserved" ) );
146 for ( int i = 64; i <= 255; ++i )
147 sCodes.insert( i, QObject::tr( "User Definable" ) );
148 } );
149
150 return sCodes;
151}
152
154{
155 static const QMap< int, QString > sCodes
156 {
157 {0, QStringLiteral( "No color or time stored" )},
158 {1, QStringLiteral( "Time is stored" )},
159 {2, QStringLiteral( "Color is stored" )},
160 {3, QStringLiteral( "Color and time are stored" )},
161 {6, QStringLiteral( "Time is stored" )},
162 {7, QStringLiteral( "Time and color are stored)" )},
163 {8, QStringLiteral( "Time, color and near infrared are stored" )},
164 };
165
166 return sCodes;
167}
168
170{
171 static const QMap< int, QString > sCodes
172 {
173 {0, QObject::tr( "No color or time stored" )},
174 {1, QObject::tr( "Time is stored" )},
175 {2, QObject::tr( "Color is stored" )},
176 {3, QObject::tr( "Color and time are stored" )},
177 {6, QObject::tr( "Time is stored" )},
178 {7, QObject::tr( "Time and color are stored)" )},
179 {8, QObject::tr( "Time, color and near infrared are stored" )},
180 };
181
182 return sCodes;
183}
184
191
192QVariant QgsPointCloudDataProvider::metadataStatistic( const QString &attribute, Qgis::Statistic statistic ) const
193{
195
196 QgsPointCloudIndex *pcIndex = index();
197 if ( pcIndex )
198 {
199 return pcIndex->metadataStatistic( attribute, statistic );
200 }
201 return QVariant();
202}
203
204QVariantList QgsPointCloudDataProvider::metadataClasses( const QString &attribute ) const
205{
207
208 QgsPointCloudIndex *pcIndex = index();
209 if ( pcIndex )
210 {
211 return pcIndex->metadataClasses( attribute );
212 }
213 return QVariantList();
214}
215
216QVariant QgsPointCloudDataProvider::metadataClassStatistic( const QString &attribute, const QVariant &value, Qgis::Statistic statistic ) const
217{
219
220 QgsPointCloudIndex *pcIndex = index();
221 if ( pcIndex )
222 {
223 return pcIndex->metadataClassStatistic( attribute, value, statistic );
224 }
225 return QVariant();
226}
227
229{
231
232 QgsPointCloudIndex *pcIndex = index();
233 if ( pcIndex )
234 {
235 return pcIndex->metadataStatistics();
236 }
238}
239
241{
242 return true;
243}
244
246{
247 return tr( "QGIS expression" );
248}
249
251{
252 // unfortunately we can't access QgsHelp here, that's a GUI class!
253 return QString();
254}
255
257{
258 typedef QVector<QMap<QString, QVariant>> result_type;
259
260 MapIndexedPointCloudNode( QgsPointCloudRequest &request, const QgsVector3D &indexScale, const QgsVector3D &indexOffset,
261 const QgsGeometry &extentGeometry, const QgsDoubleRange &zRange, QgsPointCloudIndex *index, int pointsLimit )
262 : mRequest( request ), mIndexScale( indexScale ), mIndexOffset( indexOffset ), mExtentGeometry( extentGeometry ), mZRange( zRange ), mIndex( index ), mPointsLimit( pointsLimit )
263 { }
264
265 QVector<QVariantMap> operator()( IndexedPointCloudNode n )
266 {
267 QVector<QVariantMap> acceptedPoints;
268 std::unique_ptr<QgsPointCloudBlock> block( mIndex->nodeData( n, mRequest ) );
269
270 if ( !block || pointsCount == mPointsLimit )
271 return acceptedPoints;
272
273 const char *ptr = block->data();
274 const QgsPointCloudAttributeCollection blockAttributes = block->attributes();
275 const std::size_t recordSize = blockAttributes.pointRecordSize();
276 int xOffset = 0, yOffset = 0, zOffset = 0;
277 const QgsPointCloudAttribute::DataType xType = blockAttributes.find( QStringLiteral( "X" ), xOffset )->type();
278 const QgsPointCloudAttribute::DataType yType = blockAttributes.find( QStringLiteral( "Y" ), yOffset )->type();
279 const QgsPointCloudAttribute::DataType zType = blockAttributes.find( QStringLiteral( "Z" ), zOffset )->type();
280 std::unique_ptr< QgsGeos > extentEngine = std::make_unique< QgsGeos >( mExtentGeometry.constGet() );
281 extentEngine->prepareGeometry();
282 for ( int i = 0; i < block->pointCount() && pointsCount < mPointsLimit; ++i )
283 {
284 double x, y, z;
285 QgsPointCloudAttribute::getPointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, block->scale(), block->offset(), x, y, z );
286
287 if ( mZRange.contains( z ) && extentEngine->contains( x, y ) )
288 {
289 QVariantMap pointAttr = QgsPointCloudAttribute::getAttributeMap( ptr, i * recordSize, blockAttributes );
290 pointAttr[ QStringLiteral( "X" ) ] = x;
291 pointAttr[ QStringLiteral( "Y" ) ] = y;
292 pointAttr[ QStringLiteral( "Z" ) ] = z;
293 pointsCount++;
294 acceptedPoints.push_back( pointAttr );
295 }
296 }
297 return acceptedPoints;
298 }
299
307 int pointsCount = 0;
308};
309
311 double maxError,
312 const QgsGeometry &extentGeometry,
313 const QgsDoubleRange &extentZRange, int pointsLimit )
314{
316
317 QVector<QVariantMap> acceptedPoints;
318
319 QgsPointCloudIndex *index = this->index();
320
321 if ( !index || !index->isValid() )
322 return acceptedPoints;
323
324 const IndexedPointCloudNode root = index->root();
325
326 const QgsRectangle rootNodeExtent = index->nodeMapExtent( root );
327 const double rootError = rootNodeExtent.width() / index->span();
328
329 const QVector<IndexedPointCloudNode> nodes = traverseTree( index, root, maxError, rootError, extentGeometry, extentZRange );
330
331 const QgsPointCloudAttributeCollection attributeCollection = index->attributes();
332 QgsPointCloudRequest request;
333 request.setAttributes( attributeCollection );
334
335 acceptedPoints = QtConcurrent::blockingMappedReduced( nodes,
336 MapIndexedPointCloudNode( request, index->scale(), index->offset(), extentGeometry, extentZRange, index, pointsLimit ),
337 qOverload<const QVector<QMap<QString, QVariant>>&>( &QVector<QMap<QString, QVariant>>::append ),
338 QtConcurrent::UnorderedReduce );
339
340 return acceptedPoints;
341}
342
343QVector<IndexedPointCloudNode> QgsPointCloudDataProvider::traverseTree(
344 const QgsPointCloudIndex *pc,
346 double maxError,
347 double nodeError,
348 const QgsGeometry &extentGeometry,
349 const QgsDoubleRange &extentZRange )
350{
352
353 QVector<IndexedPointCloudNode> nodes;
354
355 const QgsDoubleRange nodeZRange = pc->nodeZRange( n );
356 if ( !extentZRange.overlaps( nodeZRange ) )
357 return nodes;
358
359 if ( !extentGeometry.intersects( pc->nodeMapExtent( n ) ) )
360 return nodes;
361
362 nodes.append( n );
363
364 const double childrenError = nodeError / 2.0;
365 if ( childrenError < maxError )
366 return nodes;
367
368 const QList<IndexedPointCloudNode> children = pc->nodeChildren( n );
369 for ( const IndexedPointCloudNode &nn : children )
370 {
371 if ( extentGeometry.intersects( pc->nodeMapExtent( nn ) ) )
372 nodes += traverseTree( pc, nn, maxError, childrenError, extentGeometry, extentZRange );
373 }
374
375 return nodes;
376}
377
378bool QgsPointCloudDataProvider::setSubsetString( const QString &subset, bool updateFeatureCount )
379{
381
382 Q_UNUSED( updateFeatureCount )
383 const auto i = index();
384 if ( !i )
385 return false;
386
387 if ( !i->setSubsetString( subset ) )
388 return false;
389 mSubsetString = subset;
390 emit dataChanged();
391 return true;
392}
393
400
Represents a indexed point cloud node in octree.
Statistic
Available generic statistics.
Definition qgis.h:5375
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
Abstract base class for spatial data provider implementations.
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
QgsRange which stores a range of double values.
Definition qgsrange.h:231
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
const QgsAbstractGeometry * constGet() const
Returns a non-modifiable (const) reference to the underlying abstract geometry primitive.
bool intersects(const QgsRectangle &rectangle) const
Returns true if this geometry exactly intersects with a rectangle.
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.
DataType
Systems of unit measurement.
static void getPointXYZ(const char *ptr, int i, std::size_t pointRecordSize, int xOffset, QgsPointCloudAttribute::DataType xType, int yOffset, QgsPointCloudAttribute::DataType yType, int zOffset, QgsPointCloudAttribute::DataType zType, const QgsVector3D &indexScale, const QgsVector3D &indexOffset, double &x, double &y, double &z)
Retrieves the x, y, z values for the point at index i.
static QVariantMap getAttributeMap(const char *data, std::size_t recordOffset, const QgsPointCloudAttributeCollection &attributeCollection)
Retrieves all the attributes of a point.
DataType type() const
Returns the data type.
QString subsetStringDialect() const override
Returns a user-friendly string describing the dialect which is supported for subset strings by the pr...
@ NoCapabilities
Provider has no capabilities.
bool setSubsetString(const QString &subset, bool updateFeatureCount=false) override
Set the subset string used to create a subset of features in the layer.
~QgsPointCloudDataProvider() override
QgsPointCloudDataProvider(const QString &uri, const QgsDataProvider::ProviderOptions &providerOptions, Qgis::DataProviderReadFlags flags=Qgis::DataProviderReadFlags())
Ctor.
static QMap< int, QString > dataFormatIds()
Returns the map of LAS data format ID to untranslated string value.
bool supportsSubsetString() const override
Returns true if the provider supports setting of subset strings.
QVector< QVariantMap > identify(double maxError, const QgsGeometry &extentGeometry, const QgsDoubleRange &extentZRange=QgsDoubleRange(), int pointsLimit=1000)
Returns the list of points of the point cloud according to a zoom level defined by maxError (in layer...
QgsPointCloudStatistics metadataStatistics()
Returns the object containing the statistics metadata extracted from the dataset.
QString subsetStringHelpUrl() const override
Returns a URL pointing to documentation describing the dialect which is supported for subset strings ...
QString subsetString() const override
Returns the subset definition string currently in use by the layer and used by the provider to limit ...
static QMap< int, QString > translatedDataFormatIds()
Returns the map of LAS data format ID to translated string value.
virtual QgsPointCloudDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities for the data provider.
static QMap< int, QString > translatedLasClassificationCodes()
Returns the map of LAS classification code to translated string value, corresponding to the ASPRS Sta...
QString mSubsetString
String used to define a subset of the layer.
static QMap< int, QString > lasClassificationCodes()
Returns the map of LAS classification code to untranslated string value, corresponding to the ASPRS S...
virtual bool hasStatisticsMetadata() const
Returns whether the dataset contains statistics metadata.
virtual QgsPointCloudIndex * index() const
Returns the point cloud index associated with the provider.
virtual QVariant metadataStatistic(const QString &attribute, Qgis::Statistic statistic) const
Returns a statistic for the specified attribute, taken only from the metadata of the point cloud data...
virtual QgsPointCloudRenderer * createRenderer(const QVariantMap &configuration=QVariantMap()) const
Creates a new 2D point cloud renderer, using provider backend specific information.
bool hasValidIndex() const
Returns whether provider has index which is valid.
virtual QVariantMap originalMetadata() const
Returns a representation of the original metadata included in a point cloud dataset.
virtual QgsGeometry polygonBounds() const
Returns the polygon bounds of the layer.
virtual QVariantList metadataClasses(const QString &attribute) const
Returns a list of existing classes which are present for the specified attribute, taken only from the...
virtual QVariant metadataClassStatistic(const QString &attribute, const QVariant &value, Qgis::Statistic statistic) const
Returns a statistic for one class value from the specified attribute, taken only from the metadata of...
Represents a indexed point clouds data in octree.
int span() const
Returns the number of points in one direction in a single node.
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.
QgsVector3D offset() const
Returns offset.
QgsVector3D scale() const
Returns scale.
virtual bool isValid() const =0
Returns whether index is loaded and valid.
IndexedPointCloudNode root()
Returns root node of the index.
virtual bool hasStatisticsMetadata() const =0
Returns whether the dataset contains metadata of statistics.
QgsDoubleRange nodeZRange(const IndexedPointCloudNode &node) const
Returns the z range of a node.
virtual std::unique_ptr< QgsPointCloudBlock > nodeData(const IndexedPointCloudNode &n, const QgsPointCloudRequest &request)=0
Returns node data block.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
Abstract base class for 2d point cloud renderers.
Point cloud data request.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.
Class used to store statistics of a point cloud dataset.
bool overlaps(const QgsRange< T > &other) const
Returns true if this range overlaps another range.
Definition qgsrange.h:176
bool contains(const QgsRange< T > &other) const
Returns true if this range contains another range.
Definition qgsrange.h:137
A rectangle specified with double values.
double width() const
Returns the width of the rectangle.
Class for storage of 3D vectors similar to QVector3D, with the difference that it uses double precisi...
Definition qgsvector3d.h:31
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
QVector< QVariantMap > operator()(IndexedPointCloudNode n)
QVector< QMap< QString, QVariant > > result_type
MapIndexedPointCloudNode(QgsPointCloudRequest &request, const QgsVector3D &indexScale, const QgsVector3D &indexOffset, const QgsGeometry &extentGeometry, const QgsDoubleRange &zRange, QgsPointCloudIndex *index, int pointsLimit)
Setting options for creating vector data providers.