Clear and load in getFeatures()

This commit is contained in:
Matthias Kuhn 2019-03-02 21:58:43 +01:00
parent 9a839c97f3
commit 60512e92df
No known key found for this signature in database
GPG Key ID: 7A7F1A1C90C3E6A7
3 changed files with 18 additions and 7 deletions

View File

@ -83,7 +83,7 @@ The coordinate reference system of this layer.
protected:
void insertFeature( const QgsFeature &feature );
void insertFeature( const QgsFeature &feature, bool skipLock = false );
%Docstring
Inserts a feature into the cache and the spatial index.
To be used by implementations of ``addFeature``.

View File

@ -70,15 +70,22 @@ bool QgsFeaturePool::getFeature( QgsFeatureId id, QgsFeature &feature, QgsFeedba
QgsFeatureIds QgsFeaturePool::getFeatures( const QgsFeatureRequest &request, QgsFeedback *feedback )
{
QgsReadWriteLocker( mCacheLock, QgsReadWriteLocker::Write );
Q_UNUSED( feedback )
Q_ASSERT( QThread::currentThread() == qApp->thread() );
mFeatureCache.clear();
mIndex = QgsSpatialIndex();
QgsFeatureIds fids;
std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource( mLayer, feedback );
mFeatureSource = qgis::make_unique<QgsVectorLayerFeatureSource>( mLayer );
QgsFeatureIterator it = source->getFeatures( request );
QgsFeatureIterator it = mFeatureSource->getFeatures( request );
QgsFeature feature;
while ( it.nextFeature( feature ) )
{
insertFeature( feature );
insertFeature( feature, true );
fids << feature.id();
}
@ -109,9 +116,11 @@ QPointer<QgsVectorLayer> QgsFeaturePool::layerPtr() const
return mLayer;
}
void QgsFeaturePool::insertFeature( const QgsFeature &feature )
void QgsFeaturePool::insertFeature( const QgsFeature &feature, bool skipLock )
{
QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Write );
QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Unlocked );
if ( !skipLock )
locker.changeMode( QgsReadWriteLocker::Write );
mFeatureCache.insert( feature.id(), new QgsFeature( feature ) );
QgsFeature indexFeature( feature );
mIndex.addFeature( indexFeature );
@ -159,6 +168,7 @@ QString QgsFeaturePool::layerName() const
QgsCoordinateReferenceSystem QgsFeaturePool::crs() const
{
QgsReadWriteLocker( mCacheLock, QgsReadWriteLocker::Read );
return mFeatureSource->crs();
}
@ -169,5 +179,6 @@ QgsWkbTypes::GeometryType QgsFeaturePool::geometryType() const
QString QgsFeaturePool::layerId() const
{
QgsReadWriteLocker( mCacheLock, QgsReadWriteLocker::Read );
return mFeatureSource->id();
}

View File

@ -132,7 +132,7 @@ class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink SIP_ABSTRACT
* Inserts a feature into the cache and the spatial index.
* To be used by implementations of ``addFeature``.
*/
void insertFeature( const QgsFeature &feature );
void insertFeature( const QgsFeature &feature, bool skipLock = false );
/**
* Changes a feature in the cache and the spatial index.