QGIS API Documentation 3.43.0-Master (b60ef06885e)
qgspointcloudeditingindex.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspointcloudeditingindex.cpp
3 ---------------------
4 begin : December 2024
5 copyright : (C) 2024 by Stefanos Natsis
6 email : uclaros at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgspointcloudlayer.h"
21#include "qgscopcupdate.h"
22#include "qgslazdecoder.h"
23
24#include <QDir>
25#include <QFileInfo>
26#include <QMutex>
27
28
30{
31 if ( !layer ||
32 !layer->dataProvider() ||
33 !layer->dataProvider()->hasValidIndex() ||
35 return;
36
37 mUri = layer->source();
38 mIndex = layer->dataProvider()->index();
39
40 mAttributes = mIndex.attributes();
41 mScale = mIndex.scale();
42 mOffset = mIndex.offset();
43 mExtent = mIndex.extent();
44 mZMin = mIndex.zMin();
45 mZMax = mIndex.zMax();
46 mRootBounds = mIndex.rootNodeBounds();
47 mSpan = mIndex.span();
48 mIsValid = true;
49}
50
51void QgsPointCloudEditingIndex::load( const QString & )
52{
53 return;
54}
55
57{
58 return mIsValid && mIndex.isValid();
59}
60
65
70
72{
73 return mIndex.pointCount();
74}
75
77{
78 return mIndex.originalMetadata();
79}
80
82{
83 return mIndex.hasNode( n );
84}
85
87{
88 return mIndex.getNode( id );
89}
90
91bool QgsPointCloudEditingIndex::setSubsetString( const QString &subset )
92{
93 return mIndex.setSubsetString( subset );
94}
95
97{
98 return mIndex.subsetString();
99}
100
101std::unique_ptr< QgsPointCloudBlock > QgsPointCloudEditingIndex::nodeData( const QgsPointCloudNodeId &n, const QgsPointCloudRequest &request )
102{
103 mEditedNodeDataMutex.lock(); // Unlocked in both branches!
104 if ( mEditedNodeData.contains( n ) )
105 {
106 // we need to create a copy of the expression to pass to the decoder
107 // as the same QgsPointCloudExpression object mighgt be concurrently
108 // used on another thread, for example in a 3d view
109 QgsPointCloudExpression filterExpression = QgsPointCloudExpression( request.ignoreIndexFilterEnabled() ? QString() : subsetString() );
110 QgsPointCloudAttributeCollection requestAttributes = request.attributes();
111 requestAttributes.extend( attributes(), filterExpression.referencedAttributes() );
112
113 QgsRectangle filterRect = request.filterRect();
114
115 QByteArray rawBlockData = mEditedNodeData[n];
116 mEditedNodeDataMutex.unlock();
117
118 QgsCopcPointCloudIndex *copcIndex = static_cast<QgsCopcPointCloudIndex *>( mIndex.get() );
119
120 int pointCount = copcIndex->mHierarchy.value( n );
121
122 return QgsLazDecoder::decompressCopc( rawBlockData, *copcIndex->mLazInfo.get(), pointCount, requestAttributes, filterExpression, filterRect );
123 }
124 else
125 {
126 mEditedNodeDataMutex.unlock();
127 return mIndex.nodeData( n, request );
128 }
129}
130
132{
133 Q_ASSERT( false );
134 return nullptr;
135}
136
141
143{
144 QMutexLocker locker( &mEditedNodeDataMutex );
145
146 return mEditedNodeData.value( n );
147}
148
150{
151 QMutexLocker locker( &mEditedNodeDataMutex );
152
153 mEditedNodeData.remove( n );
154}
155
156bool QgsPointCloudEditingIndex::commitChanges( QString *errorMessage )
157{
158 QMutexLocker locker( &mEditedNodeDataMutex );
159
160 if ( mEditedNodeData.isEmpty() )
161 return true;
162
163 QHash<QgsPointCloudNodeId, QgsCopcUpdate::UpdatedChunk> updatedChunks;
164 for ( auto it = mEditedNodeData.constBegin(); it != mEditedNodeData.constEnd(); ++it )
165 {
166 QgsPointCloudNodeId n = it.key();
167 // right now we're assuming there's no change of point count
168 qint32 nodePointCount = static_cast<qint32>( getNode( n ).pointCount() );
169 updatedChunks[n] = QgsCopcUpdate::UpdatedChunk{ nodePointCount, it.value() };
170 }
171
172 QFileInfo fileInfo( mUri );
173 const QString outputFilename = fileInfo.dir().filePath( fileInfo.baseName() + QStringLiteral( "-update.copc.laz" ) );
174
175 if ( !QgsCopcUpdate::writeUpdatedFile( mUri, outputFilename, updatedChunks, errorMessage ) )
176 {
177 return false;
178 }
179
180 // reset the underlying index - we will reload it at the end
181 QgsCopcPointCloudIndex *copcIndex = static_cast<QgsCopcPointCloudIndex *>( mIndex.get() );
182 copcIndex->reset();
183
184 const QString originalFilename = fileInfo.dir().filePath( fileInfo.baseName() + QStringLiteral( "-original.copc.laz" ) );
185 if ( !QFile::rename( mUri, originalFilename ) )
186 {
187 if ( errorMessage )
188 *errorMessage = QStringLiteral( "Rename of the old COPC failed!" );
189 QFile::remove( outputFilename );
190 return false;
191 }
192
193 if ( !QFile::rename( outputFilename, mUri ) )
194 {
195 if ( errorMessage )
196 *errorMessage = QStringLiteral( "Rename of the new COPC failed!" );
197 QFile::rename( originalFilename, mUri );
198 QFile::remove( outputFilename );
199 return false;
200 }
201
202 if ( !QFile::remove( originalFilename ) )
203 {
204 if ( errorMessage )
205 *errorMessage = QStringLiteral( "Removal of the old COPC failed!" );
206 // TODO: cleanup here as well?
207 return false;
208 }
209
210 mEditedNodeData.clear();
211
212 // now let's reload
213 copcIndex->load( mUri );
214
215 return true;
216}
217
219{
220 QMutexLocker locker( &mEditedNodeDataMutex );
221
222 return !mEditedNodeData.isEmpty();
223}
224
226{
227 QMutexLocker locker( &mEditedNodeDataMutex );
228
229 return mEditedNodeData.contains( n );
230}
231
232QList<QgsPointCloudNodeId> QgsPointCloudEditingIndex::updatedNodes() const
233{
234 QMutexLocker locker( &mEditedNodeDataMutex );
235
236 return mEditedNodeData.keys();
237}
238
239bool QgsPointCloudEditingIndex::updateNodeData( const QHash<QgsPointCloudNodeId, QByteArray> &data )
240{
241 QMutexLocker locker( &mEditedNodeDataMutex );
242
243 for ( auto it = data.constBegin(); it != data.constEnd(); ++it )
244 {
245 mEditedNodeData[it.key()] = it.value();
246 }
247
248 // get rid of cached keys that got modified
249 {
250 QMutexLocker locker( &sBlockCacheMutex );
251 const QList<QgsPointCloudCacheKey> cacheKeys = sBlockCache.keys();
252 for ( const QgsPointCloudCacheKey &cacheKey : cacheKeys )
253 {
254 if ( cacheKey.uri() == mUri && data.contains( cacheKey.node() ) )
255 sBlockCache.remove( cacheKey );
256 }
257 }
258
259 return true;
260}
PointCloudAccessType
The access type of the data, local is for local files and remote for remote files (over HTTP).
Definition qgis.h:5850
QgsPointCloudAttributeCollection mAttributes
QgsBox3D mRootBounds
Bounds of the root node's cube (in int32 coordinates)
QgsVector3D mOffset
Offset of our int32 coordinates compared to CRS coords.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
static QCache< QgsPointCloudCacheKey, QgsPointCloudBlock > sBlockCache
int mSpan
All native attributes stored in the file.
double mZMax
Vertical extent of data.
QgsRectangle mExtent
2D extent of data
QgsVector3D mScale
Scale of our int32 coordinates compared to CRS coords.
Represents a coordinate reference system (CRS).
static bool writeUpdatedFile(const QString &inputFilename, const QString &outputFilename, const QHash< QgsPointCloudNodeId, UpdatedChunk > &updatedChunks, QString *errorMessage=nullptr)
Convenience function to do the whole process in one go: load a COPC file, then write a new COPC file ...
QString source() const
Returns the source for the layer.
A collection of point cloud attributes.
void extend(const QgsPointCloudAttributeCollection &otherCollection, const QSet< QString > &matchingNames)
Adds specific missing attributes from another QgsPointCloudAttributeCollection.
Base class for handling loading QgsPointCloudBlock asynchronously.
Container class for QgsPointCloudBlock cache keys.
@ ChangeAttributeValues
Provider can modify the values of point attributes.
virtual QgsPointCloudIndex index() const
Returns the point cloud index associated with the provider.
virtual QgsPointCloudDataProvider::Capabilities capabilities() const
Returns flags containing the supported capabilities for the data provider.
bool hasValidIndex() const
Returns whether provider has index which is valid.
QList< QgsPointCloudNodeId > updatedNodes() const
Returns a list of node IDs that have been modified.
bool hasNode(const QgsPointCloudNodeId &n) const override
Returns whether the octree contain given node.
bool isModified() const
Returns true if there are uncommitted changes, false otherwise.
QgsPointCloudIndex backingIndex() const
Returns index for the underlying non-edited data.
QVariantMap originalMetadata() const override
Returns the original metadata map.
qint64 pointCount() const override
Returns the number of points in the point cloud.
bool commitChanges(QString *errorMessage=nullptr)
Tries to store pending changes to the data provider.
bool setSubsetString(const QString &subset) override
Sets the string used to define a subset of the point cloud.
void load(const QString &fileName) override
Loads the index from the file.
void resetNodeEdits(QgsPointCloudNodeId n)
Removes node edits from index, returning it to its original state.
const QByteArray rawEditedNodeData(QgsPointCloudNodeId n) const
Returns the raw, encoded, compressed data for a node or empty if missing.
QgsPointCloudBlockRequest * asyncNodeData(const QgsPointCloudNodeId &n, const QgsPointCloudRequest &request) override
Returns a handle responsible for loading a node data block.
QgsPointCloudEditingIndex(QgsPointCloudLayer *layer)
Ctor.
bool isNodeModified(QgsPointCloudNodeId n) const
Returns true if this node was modified.
QString subsetString() const override
Returns the string used to define a subset of the point cloud.
bool isValid() const override
Returns whether index is loaded and valid.
std::unique_ptr< QgsPointCloudBlock > nodeData(const QgsPointCloudNodeId &n, const QgsPointCloudRequest &request) override
Returns node data block.
QgsPointCloudNode getNode(const QgsPointCloudNodeId &id) const override
Returns object for a given node.
Qgis::PointCloudAccessType accessType() const override
Returns the access type of the data If the access type is Remote, data will be fetched from an HTTP s...
bool updateNodeData(const QHash< QgsPointCloudNodeId, QByteArray > &data) override
Tries to update the data for the specified nodes.
QgsCoordinateReferenceSystem crs() const override
Returns the coordinate reference system of the point cloud index.
Smart pointer for QgsAbstractPointCloudIndex.
int span() const
Returns the number of points in one direction in a single node.
double zMax() const
Returns z max.
QgsBox3D rootNodeBounds() const
Returns bounding box of root node in CRS coords.
QString subsetString() const
Returns the string used to define a subset of the point cloud.
double zMin() const
Returns z min.
QgsVector3D offset() const
Returns offset of data from CRS.
QgsVector3D scale() const
Returns scale of data relative to CRS.
bool setSubsetString(const QString &subset)
Sets the string used to define a subset of the point cloud.
bool isValid() const
Returns whether index is loaded and valid.
QgsRectangle extent() const
Returns extent of the data.
QgsCoordinateReferenceSystem crs() const
Returns the coordinate reference system of the point cloud index.
qint64 pointCount() const
Returns the number of points in the point cloud.
std::unique_ptr< QgsPointCloudBlock > nodeData(const QgsPointCloudNodeId &n, const QgsPointCloudRequest &request)
Returns node data block.
QgsPointCloudNode getNode(const QgsPointCloudNodeId &id) const
Returns object for a given node.
bool hasNode(const QgsPointCloudNodeId &id) const
Returns whether the octree contain given node.
QgsAbstractPointCloudIndex * get()
Returns pointer to the implementation class.
QVariantMap originalMetadata() const
Returns the original metadata map.
QgsPointCloudAttributeCollection attributes() const
Returns all attributes that are stored in the file.
Qgis::PointCloudAccessType accessType() const
Returns the access type of the data If the access type is Remote, data will be fetched from an HTTP s...
Represents a map layer supporting display of point clouds.
QgsPointCloudDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
Represents an indexed point cloud node's position in octree.
Keeps metadata for an indexed point cloud node.
qint64 pointCount() const
Returns number of points contained in node data.
Point cloud data request.
bool ignoreIndexFilterEnabled() const
Returns whether the request will ignore the point cloud index's filter expression,...
QgsPointCloudAttributeCollection attributes() const
Returns attributes.
QgsRectangle filterRect() const
Returns the rectangle from which points will be taken, in point cloud's crs.
A rectangle specified with double values.
Keeps information how points of a single chunk has been modified.