QGIS API Documentation  3.6.0-Noosa (5873452)
qgsfeaturepool.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * qgsfeaturepool.cpp *
3  * ------------------- *
4  * copyright : (C) 2014 by Sandro Mani / Sourcepole AG *
5  * email : [email protected] *
6  ***************************************************************************/
7 
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsfeaturepool.h"
18 #include "qgsfeature.h"
19 #include "qgsfeatureiterator.h"
20 #include "qgsgeometry.h"
21 #include "qgsvectorlayer.h"
22 #include "qgsvectordataprovider.h"
23 #include "qgsvectorlayerutils.h"
24 #include "qgsreadwritelocker.h"
25 
26 #include <QMutexLocker>
27 
28 
30  : mFeatureCache( CACHE_SIZE )
31  , mLayer( layer )
32  , mLayerId( layer->id() )
33  , mGeometryType( layer->geometryType() )
34  , mCrs( layer->crs() )
35 {
36 
37 }
38 
40 {
41  QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Read );
42  QgsFeature *cachedFeature = mFeatureCache.object( id );
43  if ( cachedFeature )
44  {
45  //feature was cached
46  feature = *cachedFeature;
47  }
48  else
49  {
50  std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource( mLayer, feedback );
51 
52  // Feature not in cache, retrieve from layer
53  // TODO: avoid always querying all attributes (attribute values are needed when merging by attribute)
54  if ( !source || !source->getFeatures( QgsFeatureRequest( id ) ).nextFeature( feature ) )
55  {
56  return false;
57  }
59  mFeatureCache.insert( id, new QgsFeature( feature ) );
60  mIndex.addFeature( feature );
61  }
62  return true;
63 }
64 
66 {
67  QgsFeatureIds fids;
68 
69  std::unique_ptr<QgsVectorLayerFeatureSource> source = QgsVectorLayerUtils::getFeatureSource( mLayer, feedback );
70 
71  QgsFeatureIterator it = source->getFeatures( request );
72  QgsFeature feature;
73  while ( it.nextFeature( feature ) )
74  {
75  insertFeature( feature );
76  fids << feature.id();
77  }
78 
79  return fids;
80 }
81 
83 {
84  return mFeatureIds;
85 }
86 
88 {
89  QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Read );
90  QgsFeatureIds ids = QgsFeatureIds::fromList( mIndex.intersects( rect ) );
91  return ids;
92 }
93 
95 {
96  Q_ASSERT( QThread::currentThread() == qApp->thread() );
97 
98  return mLayer.data();
99 }
100 
101 QPointer<QgsVectorLayer> QgsFeaturePool::layerPtr() const
102 {
103  return mLayer;
104 }
105 
107 {
108  QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Write );
109  mFeatureCache.insert( feature.id(), new QgsFeature( feature ) );
110  QgsFeature indexFeature( feature );
111  mIndex.addFeature( indexFeature );
112 }
113 
115 {
116  QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Write );
117  mFeatureCache.remove( feature.id() );
118  mIndex.deleteFeature( feature );
119  locker.unlock();
120 
121  QgsFeature tempFeature;
122  getFeature( feature.id(), tempFeature );
123 }
124 
126 {
127  QgsFeature origFeature;
128  QgsReadWriteLocker locker( mCacheLock, QgsReadWriteLocker::Unlocked );
129  if ( getFeature( featureId, origFeature ) )
130  {
132  mIndex.deleteFeature( origFeature );
133  }
135  mFeatureCache.remove( origFeature.id() );
136 }
137 
139 {
140  mFeatureIds = ids;
141 }
142 
144 {
145  return mFeatureCache.contains( fid );
146 }
147 
149 {
150  return mCrs;
151 }
152 
154 {
155  return mGeometryType;
156 }
157 
158 QString QgsFeaturePool::layerId() const
159 {
160  return mLayerId;
161 }
bool isFeatureCached(QgsFeatureId fid)
Checks if the feature fid is cached.
QgsFeatureId id
Definition: qgsfeature.h:64
Wrapper for iterator of features from vector data provider or vector layer.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsFeatureIds getIntersects(const QgsRectangle &rect) const
Gets all feature ids in the bounding box rect.
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:34
bool getFeature(QgsFeatureId id, QgsFeature &feature, QgsFeedback *feedback=nullptr)
Retrieves the feature with the specified id into feature.
static std::unique_ptr< QgsVectorLayerFeatureSource > getFeatureSource(QPointer< QgsVectorLayer > layer, QgsFeedback *feedback=nullptr)
Gets the feature source from a QgsVectorLayer pointer.
QgsFeaturePool(QgsVectorLayer *layer)
Creates a new feature pool for layer.
QPointer< QgsVectorLayer > layerPtr() const
Gets a QPointer to the underlying layer.
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:55
const QgsCoordinateReferenceSystem & crs
Base class for feedback objects to be used for cancelation of something running in a worker thread...
Definition: qgsfeedback.h:44
The QgsReadWriteLocker class is a convenience class that simplifies locking and unlocking QReadWriteL...
QgsVectorLayer * layer() const
Gets a pointer to the underlying layer.
void insertFeature(const QgsFeature &feature)
Inserts a feature into the cache and the spatial index.
QgsFeatureIds getFeatures(const QgsFeatureRequest &request, QgsFeedback *feedback=nullptr)
Gets features for the provided request.
void refreshCache(const QgsFeature &feature)
Changes a feature in the cache and the spatial index.
This class wraps a request for features to a vector layer (or directly its vector data provider)...
GeometryType
The geometry types are used to group QgsWkbTypes::Type in a coarse way.
Definition: qgswkbtypes.h:138
void unlock()
Unlocks the lock.
bool deleteFeature(const QgsFeature &feature)
Removes a feature from the index.
void setFeatureIds(const QgsFeatureIds &ids)
Sets all the feature ids governed by this feature pool.
This class represents a coordinate reference system (CRS).
void changeMode(Mode mode)
Change the mode of the lock to mode.
QgsCoordinateReferenceSystem crs() const
The coordinate reference system of this layer.
bool addFeature(QgsFeature &feature, QgsFeatureSink::Flags flags=nullptr) override
Adds a feature to the index.
bool nextFeature(QgsFeature &f)
QgsWkbTypes::GeometryType geometryType() const
The geometry type of this layer.
Represents a vector layer which manages a vector based data sets.
QgsFeatureIds allFeatureIds() const
Returns the complete set of feature ids in this pool.
QList< QgsFeatureId > intersects(const QgsRectangle &rectangle) const
Returns a list of features with a bounding box which intersects the specified rectangle.
void removeFeature(const QgsFeatureId featureId)
Removes a feature from the cache and the spatial index.
QString layerId() const
The layer id of the layer.