QGIS API Documentation 3.99.0-Master (a8f284845db)
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
19
20#include <mutex>
21
22#include "qgis.h"
23#include "qgsgeometry.h"
24#include "qgsgeos.h"
25#include "qgspointcloudindex.h"
28#include "qgsthreadingutils.h"
29
30#include <QDebug>
31#include <QString>
32#include <QtConcurrentMap>
33#include <QtMath>
34
35#include "moc_qgspointclouddataprovider.cpp"
36
37using namespace Qt::StringLiterals;
38
46
48
55
57{
59
60 QgsPointCloudIndex lIndex = index();
61 return lIndex.isValid();
62}
63
70
72{
74
75 return QVariantMap();
76}
77
79{
81
82 return nullptr;
83}
84
86{
87 static QMap< int, QString > sCodes
88 {
89 {0, u"Created, Never Classified"_s},
90 {1, u"Unclassified"_s},
91 {2, u"Ground"_s},
92 {3, u"Low Vegetation"_s},
93 {4, u"Medium Vegetation"_s},
94 {5, u"High Vegetation"_s},
95 {6, u"Building"_s},
96 {7, u"Low Point (Low Noise)"_s},
97 {8, u"Reserved"_s},
98 {9, u"Water"_s},
99 {10, u"Rail"_s},
100 {11, u"Road Surface"_s},
101 {12, u"Reserved"_s},
102 {13, u"Wire - Guard (Shield)"_s},
103 {14, u"Wire - Conductor (Phase)"_s},
104 {15, u"Transmission Tower"_s},
105 {16, u"Wire-Structure Connector (Insulator)"_s},
106 {17, u"Bridge Deck"_s},
107 {18, u"High Noise"_s},
108 };
109
110 static std::once_flag initialized;
111 std::call_once( initialized, []( )
112 {
113 for ( int i = 19; i <= 63; ++i )
114 sCodes.insert( i, u"Reserved"_s );
115 for ( int i = 64; i <= 255; ++i )
116 sCodes.insert( i, u"User Definable"_s );
117 } );
118
119 return sCodes;
120}
121
123{
124 static QMap< int, QString > sCodes
125 {
126 {0, QObject::tr( "Created, Never Classified" )},
127 {1, QObject::tr( "Unclassified" )},
128 {2, QObject::tr( "Ground" )},
129 {3, QObject::tr( "Low Vegetation" )},
130 {4, QObject::tr( "Medium Vegetation" )},
131 {5, QObject::tr( "High Vegetation" )},
132 {6, QObject::tr( "Building" )},
133 {7, QObject::tr( "Low Point (Noise)" )},
134 {8, QObject::tr( "Reserved" )},
135 {9, QObject::tr( "Water" )},
136 {10, QObject::tr( "Rail" )},
137 {11, QObject::tr( "Road Surface" )},
138 {12, QObject::tr( "Reserved" )},
139 {13, QObject::tr( "Wire - Guard (Shield)" )},
140 {14, QObject::tr( "Wire - Conductor (Phase)" )},
141 {15, QObject::tr( "Transmission Tower" )},
142 {16, QObject::tr( "Wire-Structure Connector (Insulator)" )},
143 {17, QObject::tr( "Bridge Deck" )},
144 {18, QObject::tr( "High Noise" )},
145 };
146
147 static std::once_flag initialized;
148 std::call_once( initialized, []( )
149 {
150 for ( int i = 19; i <= 63; ++i )
151 sCodes.insert( i, QObject::tr( "Reserved" ) );
152 for ( int i = 64; i <= 255; ++i )
153 sCodes.insert( i, QObject::tr( "User Definable" ) );
154 } );
155
156 return sCodes;
157}
158
160{
161 static const QMap< int, QString > sCodes
162 {
163 {0, u"No color or time stored"_s},
164 {1, u"Time is stored"_s},
165 {2, u"Color is stored"_s},
166 {3, u"Color and time are stored"_s},
167 {6, u"Time is stored"_s},
168 {7, u"Time and color are stored)"_s},
169 {8, u"Time, color and near infrared are stored"_s},
170 };
171
172 return sCodes;
173}
174
176{
177 static const QMap< int, QString > sCodes
178 {
179 {0, QObject::tr( "No color or time stored" )},
180 {1, QObject::tr( "Time is stored" )},
181 {2, QObject::tr( "Color is stored" )},
182 {3, QObject::tr( "Color and time are stored" )},
183 {6, QObject::tr( "Time is stored" )},
184 {7, QObject::tr( "Time and color are stored)" )},
185 {8, QObject::tr( "Time, color and near infrared are stored" )},
186 };
187
188 return sCodes;
189}
190
192{
194
195 QgsPointCloudIndex pcIndex = index();
196 if ( pcIndex )
197 {
198 return pcIndex.metadataStatistics();
199 }
201}
202
204{
205 return true;
206}
207
209{
210 return tr( "QGIS expression" );
211}
212
214{
215 // unfortunately we can't access QgsHelp here, that's a GUI class!
216 return QString();
217}
218
220{
221 typedef QVector<QMap<QString, QVariant>> result_type;
222
223 MapIndexedPointCloudNode( QgsPointCloudRequest &request, const QgsVector3D &indexScale, const QgsVector3D &indexOffset,
224 const QgsGeometry &extentGeometry, const QgsDoubleRange &zRange, QgsPointCloudIndex index, int pointsLimit )
225 : mRequest( request ), mIndexScale( indexScale ), mIndexOffset( indexOffset ), mExtentGeometry( extentGeometry ), mZRange( zRange ), mIndex( std::move( index ) ), mPointsLimit( pointsLimit )
226 { }
227
228 QVector<QVariantMap> operator()( QgsPointCloudNodeId n )
229 {
230 QVector<QVariantMap> acceptedPoints;
231 std::unique_ptr<QgsPointCloudBlock> block( mIndex.nodeData( n, mRequest ) );
232
233 if ( !block || pointsCount == mPointsLimit )
234 return acceptedPoints;
235
236 const char *ptr = block->data();
237 const QgsPointCloudAttributeCollection blockAttributes = block->attributes();
238 const std::size_t recordSize = blockAttributes.pointRecordSize();
239 int xOffset = 0, yOffset = 0, zOffset = 0;
240 const QgsPointCloudAttribute::DataType xType = blockAttributes.find( u"X"_s, xOffset )->type();
241 const QgsPointCloudAttribute::DataType yType = blockAttributes.find( u"Y"_s, yOffset )->type();
242 const QgsPointCloudAttribute::DataType zType = blockAttributes.find( u"Z"_s, zOffset )->type();
243 auto extentEngine = std::make_unique< QgsGeos >( mExtentGeometry.constGet() );
244 extentEngine->prepareGeometry();
245
246 std::optional<bool> copcTimeFlag = std::nullopt;
247 QVariantMap extraMetadata = mIndex.extraMetadata();
248 if ( extraMetadata.contains( u"CopcGpsTimeFlag"_s ) )
249 copcTimeFlag = extraMetadata[ u"CopcGpsTimeFlag"_s ].toBool();
250
251 for ( int i = 0; i < block->pointCount() && pointsCount < mPointsLimit; ++i )
252 {
253 double x, y, z;
254 QgsPointCloudAttribute::getPointXYZ( ptr, i, recordSize, xOffset, xType, yOffset, yType, zOffset, zType, block->scale(), block->offset(), x, y, z );
255
256 if ( mZRange.contains( z ) && extentEngine->contains( x, y ) )
257 {
258 QVariantMap pointAttr = QgsPointCloudAttribute::getAttributeMap( ptr, i * recordSize, blockAttributes );
259 pointAttr[ u"X"_s ] = x;
260 pointAttr[ u"Y"_s ] = y;
261 pointAttr[ u"Z"_s ] = z;
262
263
264 if ( copcTimeFlag.has_value() )
265 {
266 const QDateTime gpsBaseTime = QDateTime::fromSecsSinceEpoch( 315964809, Qt::UTC );
267 constexpr int numberOfSecsInWeek = 3600 * 24 * 7;
268 // here we check the flag set in header to determine if we need to
269 // parse the time as GPS week time or GPS adjusted standard time
270 // however often times the flag is set wrong, so we determine if the value is bigger than the maximum amount of seconds in week then it has to be adjusted standard time
271 if ( *copcTimeFlag || pointAttr[u"GpsTime"_s].toDouble() > numberOfSecsInWeek )
272 {
273 const QString utcTime = gpsBaseTime.addSecs( static_cast<qint64>( pointAttr[u"GpsTime"_s].toDouble() + 1e9 ) ).toString( Qt::ISODate );
274 pointAttr[ u"GpsTime (raw)"_s] = pointAttr[u"GpsTime"_s];
275 pointAttr[ u"GpsTime"_s] = utcTime;
276 }
277 else
278 {
279 const QString weekTime = gpsBaseTime.addSecs( pointAttr[u"GpsTime"_s].toLongLong() ).toString( "ddd hh:mm:ss" );
280 pointAttr[ u"GpsTime (raw)"_s] = pointAttr[u"GpsTime"_s];
281 pointAttr[ u"GpsTime"_s] = weekTime;
282 }
283 }
284 pointsCount++;
285 acceptedPoints.push_back( pointAttr );
286 }
287 }
288 return acceptedPoints;
289 }
290
298 int pointsCount = 0;
299};
300
302 double maxError,
303 const QgsGeometry &extentGeometry,
304 const QgsDoubleRange &extentZRange, int pointsLimit )
305{
306 QVector<QVariantMap> acceptedPoints;
307
308 // Try sub-indexes first
309 for ( QgsPointCloudSubIndex &subidx : subIndexes() )
310 {
311 // Check if the sub-index is relevant and if it is loaded. We shouldn't
312 // need to identify points in unloaded indices.
313 QgsPointCloudIndex index = subidx.index();
314 if ( !index
315 || ( !subidx.zRange().overlaps( extentZRange ) )
316 || !subidx.polygonBounds().intersects( extentGeometry ) )
317 continue;
318 acceptedPoints.append( identify( index, maxError, extentGeometry, extentZRange, pointsLimit ) );
319 }
320
321 // Then look at main index
322 QgsPointCloudIndex mainIndex = index();
323 acceptedPoints.append( identify( mainIndex, maxError, extentGeometry, extentZRange, pointsLimit ) );
324
325 return acceptedPoints;
326}
327
329 QgsPointCloudIndex &index, double maxError,
330 const QgsGeometry &extentGeometry,
331 const QgsDoubleRange &extentZRange, int pointsLimit )
332{
334
335 QVector<QVariantMap> acceptedPoints;
336
337 if ( !index.isValid() )
338 return acceptedPoints;
339
340 const QgsPointCloudNode root = index.getNode( index.root() );
341 const QVector<QgsPointCloudNodeId> nodes = traverseTree( index, root, maxError, root.error(), extentGeometry, extentZRange );
342
343 const QgsPointCloudAttributeCollection attributeCollection = index.attributes();
344 QgsPointCloudRequest request;
345 request.setAttributes( attributeCollection );
346
347 acceptedPoints = QtConcurrent::blockingMappedReduced( nodes,
348 MapIndexedPointCloudNode( request, index.scale(), index.offset(), extentGeometry, extentZRange, index, pointsLimit ),
349 qOverload<const QVector<QMap<QString, QVariant>>&>( &QVector<QMap<QString, QVariant>>::append ),
350 QtConcurrent::UnorderedReduce );
351
352 return acceptedPoints;
353}
354
355QVector<QgsPointCloudNodeId> QgsPointCloudDataProvider::traverseTree(
356 const QgsPointCloudIndex &pc,
358 double maxError,
359 double nodeError,
360 const QgsGeometry &extentGeometry,
361 const QgsDoubleRange &extentZRange )
362{
364
365 QVector<QgsPointCloudNodeId> nodes;
366
367 const QgsBox3D nodeBounds = node.bounds();
368 const QgsDoubleRange nodeZRange( nodeBounds.zMinimum(), nodeBounds.zMaximum() );
369 if ( !extentZRange.overlaps( nodeZRange ) )
370 return nodes;
371
372 if ( !extentGeometry.intersects( nodeBounds.toRectangle() ) )
373 return nodes;
374
375 nodes.append( node.id() );
376
377 const double childrenError = nodeError / 2.0;
378 if ( childrenError < maxError )
379 return nodes;
380
381 for ( const QgsPointCloudNodeId &nn : node.children() )
382 {
383 const QgsPointCloudNode childNode = pc.getNode( nn );
384 if ( extentGeometry.intersects( childNode.bounds().toRectangle() ) )
385 nodes += traverseTree( pc, childNode, maxError, childrenError, extentGeometry, extentZRange );
386 }
387
388 return nodes;
389}
390
391bool QgsPointCloudDataProvider::setSubsetString( const QString &subset, bool updateFeatureCount )
392{
394
395 Q_UNUSED( updateFeatureCount )
397 if ( !i )
398 return false;
399
400 if ( !i.setSubsetString( subset ) )
401 return false;
402 mSubsetString = subset;
403 emit dataChanged();
404 return true;
405}
406
413
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:507
A 3-dimensional box composed of x, y, z coordinates.
Definition qgsbox3d.h:45
double zMaximum() const
Returns the maximum z value.
Definition qgsbox3d.h:261
QgsRectangle toRectangle() const
Converts the box to a 2D rectangle.
Definition qgsbox3d.h:381
double zMinimum() const
Returns the minimum z value.
Definition qgsbox3d.h:254
virtual Qgis::DataProviderFlags flags() const
Returns the generic data provider flags.
void dataChanged()
Emitted whenever a change is made to the data provider which may have caused changes in the provider'...
QgsDataProvider(const QString &uri=QString(), const QgsDataProvider::ProviderOptions &providerOptions=QgsDataProvider::ProviderOptions(), Qgis::DataProviderReadFlags flags=Qgis::DataProviderReadFlags())
Create a new dataprovider with the specified in the uri.
QgsDataSourceUri uri() const
Gets the data source specification.
virtual QgsRectangle extent() const =0
Returns the extent of the layer.
QgsRange which stores a range of double values.
Definition qgsrange.h:236
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
bool intersects(const QgsRectangle &rectangle) const
Returns true if this geometry exactly intersects with a rectangle.
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.
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.
virtual QVector< QgsPointCloudSubIndex > subIndexes()
Returns a list of sub indexes available if the provider supports multiple indexes,...
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.
virtual QgsPointCloudIndex index() const
Returns the point cloud index associated with the provider.
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 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.
Smart pointer for QgsAbstractPointCloudIndex.
QgsPointCloudNode getNode(const QgsPointCloudNodeId &id) const
Returns object for a given node.
Represents an indexed point cloud node's position in octree.
Keeps metadata for an indexed point cloud node.
QList< QgsPointCloudNodeId > children() const
Returns IDs of child nodes.
QgsPointCloudNodeId id() const
Returns node's ID (unique in index).
float error() const
Returns node's error in map units (used to determine in whether the node has enough detail for the cu...
QgsBox3D bounds() const
Returns node's bounding cube in CRS coords.
Abstract base class for 2d point cloud renderers.
Point cloud data request.
void setAttributes(const QgsPointCloudAttributeCollection &attributes)
Set attributes filter in the request.
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:179
A 3D vector (similar to QVector3D) with the difference that it uses double precision instead of singl...
Definition qgsvector3d.h:33
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
QVector< QMap< QString, QVariant > > result_type
QVector< QVariantMap > operator()(QgsPointCloudNodeId n)
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.