-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
qgsfeaturepool.h
138 lines (113 loc) · 4.41 KB
/
qgsfeaturepool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***************************************************************************
* qgsfeaturepool.h *
* ------------------- *
* copyright : (C) 2014 by Sandro Mani / Sourcepole AG *
* email : smani@sourcepole.ch *
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#define SIP_NO_FILE
#ifndef QGS_FEATUREPOOL_H
#define QGS_FEATUREPOOL_H
#include <QCache>
#include <QMutex>
#include <QPointer>
#include "qgis_analysis.h"
#include "qgsfeature.h"
#include "qgsspatialindex.h"
#include "qgsfeaturesink.h"
class QgsVectorLayer;
/**
* \ingroup analysis
* A feature pool is based on a vector layer and caches features.
*/
class ANALYSIS_EXPORT QgsFeaturePool : public QgsFeatureSink
{
public:
QgsFeaturePool( QgsVectorLayer *layer );
virtual ~QgsFeaturePool() = default;
/**
* Retrieve the feature with the specified \a id into \a feature.
* It will be retrieved from the cache or from the underlying layer if unavailable.
* If the feature is neither available from the cache nor from the layer it will return false.
*/
bool getFeature( QgsFeatureId id, QgsFeature &feature );
/**
* Updates a feature in this pool.
* Implementations will update the feature on the layer or on the data provider.
*/
virtual void updateFeature( QgsFeature &feature ) = 0;
/**
* Removes a feature from this pool.
* Implementations will remove the feature from the layer or from the data provider.
*/
virtual void deleteFeature( QgsFeatureId fid ) = 0;
/**
* Returns the complete set of feature ids in this pool.
* Note that this concerns the features governed by this pool, which are not necessarily all cached.
*/
QgsFeatureIds allFeatureIds() const;
/**
* Get all feature ids in the bounding box \a rect. It will use a spatial index to
* determine the ids.
*/
QgsFeatureIds getIntersects( const QgsRectangle &rect ) const;
/**
* Get a pointer to the underlying layer.
* May return a ``nullptr`` if the layer has been deleted.
* This must only be called from the main thread.
*/
QgsVectorLayer *layer() const;
QPointer<QgsVectorLayer> layerPtr() const;
/**
* The layer id of the layer.
*/
QString layerId() const;
/**
* The geometry type of this layer.
*/
QgsWkbTypes::GeometryType geometryType() const;
/**
* The coordinate reference system of this layer.
*/
QgsCoordinateReferenceSystem crs() const;
protected:
/**
* Inserts a feature into the cache and the spatial index.
* To be used by implementations of ``addFeature``.
*/
void insertFeature( const QgsFeature &feature );
/**
* Changes a feature in the cache and the spatial index.
* To be used by implementations of ``updateFeature``.
*/
void refreshCache( const QgsFeature &feature );
/**
* Removes a feature from the cache and the spatial index.
* To be used by implementations of ``deleteFeature``.
*/
void removeFeature( const QgsFeatureId featureId );
/**
* Set all the feature ids governed by this feature pool.
* Should be called by subclasses constructor and whenever
* they insert a new feature.
*/
void setFeatureIds( const QgsFeatureIds &ids );
private:
static const int CACHE_SIZE = 1000;
QCache<QgsFeatureId, QgsFeature> mFeatureCache;
QPointer<QgsVectorLayer> mLayer;
mutable QReadWriteLock mCacheLock;
QgsFeatureIds mFeatureIds;
QgsSpatialIndex mIndex;
QString mLayerId;
QgsWkbTypes::GeometryType mGeometryType;
QgsCoordinateReferenceSystem mCrs;
};
#endif // QGS_FEATUREPOOL_H