Skip to content

Commit c8c623e

Browse files
committedJul 19, 2016
Add methods to vectorlayercache and tests
1 parent d91a45c commit c8c623e

File tree

6 files changed

+138
-27
lines changed

6 files changed

+138
-27
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -617,21 +617,26 @@ class QgsVectorLayer : QgsMapLayer
617617
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;
618618

619619
/**
620-
* Query the provider for features matching a given expression.
620+
* Query the layer for features matching a given expression.
621621
*/
622622
QgsFeatureIterator getFeatures( const QString& expression );
623623

624624
/**
625-
* Query the provider for the feature with the given id.
625+
* Query the layer for the feature with the given id.
626626
* If there is no such feature, the returned feature will be invalid.
627627
*/
628628
QgsFeature getFeature( QgsFeatureId fid );
629629

630630
/**
631-
* Query the provider for the features with the given ids.
631+
* Query the layer for the features with the given ids.
632632
*/
633633
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
634634

635+
/**
636+
* Query the layer for the features which intersect the specified rectangle.
637+
*/
638+
QgsFeatureIterator getFeatures( const QgsRectangle& rectangle );
639+
635640
/** Adds a feature
636641
@param f feature to add
637642
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.

‎python/core/qgsvectorlayercache.sip

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,27 @@ class QgsVectorLayerCache : QObject
8989
*/
9090
QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest = QgsFeatureRequest() );
9191

92+
/**
93+
* Query the layer for features matching a given expression.
94+
*/
95+
QgsFeatureIterator getFeatures( const QString& expression );
96+
97+
/**
98+
* Query the layer for the feature with the given id.
99+
* If there is no such feature, the returned feature will be invalid.
100+
*/
101+
QgsFeature getFeature( QgsFeatureId fid );
102+
103+
/**
104+
* Query the layer for the features with the given ids.
105+
*/
106+
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
107+
108+
/**
109+
* Query the layer for the features which intersect the specified rectangle.
110+
*/
111+
QgsFeatureIterator getFeatures( const QgsRectangle& rectangle );
112+
92113
/**
93114
* Check if a certain feature id is cached.
94115
* @param fid The feature id to look for

‎src/core/qgsvectorlayer.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,24 +1112,6 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest& request
11121112
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
11131113
}
11141114

1115-
QgsFeatureIterator QgsVectorLayer::getFeatures( const QString& expression )
1116-
{
1117-
return getFeatures( QgsFeatureRequest( expression ) );
1118-
}
1119-
1120-
QgsFeature QgsVectorLayer::getFeature( QgsFeatureId fid )
1121-
{
1122-
QgsFeature feature;
1123-
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
1124-
return feature;
1125-
}
1126-
1127-
QgsFeatureIterator QgsVectorLayer::getFeatures( QgsFeatureIds fids )
1128-
{
1129-
return getFeatures( QgsFeatureRequest( fids ) );
1130-
}
1131-
1132-
11331115
bool QgsVectorLayer::addFeature( QgsFeature& feature, bool alsoUpdateExtent )
11341116
{
11351117
Q_UNUSED( alsoUpdateExtent ); // TODO[MD]

‎src/core/qgsvectorlayer.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -988,20 +988,39 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
988988
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;
989989

990990
/**
991-
* Query the provider for features matching a given expression.
991+
* Query the layer for features matching a given expression.
992992
*/
993-
QgsFeatureIterator getFeatures( const QString& expression );
993+
inline QgsFeatureIterator getFeatures( const QString& expression )
994+
{
995+
return getFeatures( QgsFeatureRequest( expression ) );
996+
}
994997

995998
/**
996-
* Query the provider for the feature with the given id.
999+
* Query the layer for the feature with the given id.
9971000
* If there is no such feature, the returned feature will be invalid.
9981001
*/
999-
QgsFeature getFeature( QgsFeatureId fid );
1002+
inline QgsFeature getFeature( QgsFeatureId fid )
1003+
{
1004+
QgsFeature feature;
1005+
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
1006+
return feature;
1007+
}
10001008

10011009
/**
1002-
* Query the provider for the features with the given ids.
1010+
* Query the layer for the features with the given ids.
10031011
*/
1004-
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
1012+
inline QgsFeatureIterator getFeatures( const QgsFeatureIds& fids )
1013+
{
1014+
return getFeatures( QgsFeatureRequest( fids ) );
1015+
}
1016+
1017+
/**
1018+
* Query the layer for the features which intersect the specified rectangle.
1019+
*/
1020+
inline QgsFeatureIterator getFeatures( const QgsRectangle& rectangle )
1021+
{
1022+
return getFeatures( QgsFeatureRequest( rectangle ) );
1023+
}
10051024

10061025
/** Adds a feature
10071026
@param feature feature to add

‎src/core/qgsvectorlayercache.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,41 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
155155
*/
156156
QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest = QgsFeatureRequest() );
157157

158+
/**
159+
* Query the layer for features matching a given expression.
160+
*/
161+
inline QgsFeatureIterator getFeatures( const QString& expression )
162+
{
163+
return getFeatures( QgsFeatureRequest( expression ) );
164+
}
165+
166+
/**
167+
* Query the layer for the feature with the given id.
168+
* If there is no such feature, the returned feature will be invalid.
169+
*/
170+
inline QgsFeature getFeature( QgsFeatureId fid )
171+
{
172+
QgsFeature feature;
173+
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
174+
return feature;
175+
}
176+
177+
/**
178+
* Query the layer for the features with the given ids.
179+
*/
180+
inline QgsFeatureIterator getFeatures( const QgsFeatureIds& fids )
181+
{
182+
return getFeatures( QgsFeatureRequest( fids ) );
183+
}
184+
185+
/**
186+
* Query the layer for the features which intersect the specified rectangle.
187+
*/
188+
inline QgsFeatureIterator getFeatures( const QgsRectangle& rectangle )
189+
{
190+
return getFeatures( QgsFeatureRequest( rectangle ) );
191+
}
192+
158193
/**
159194
* Check if a certain feature id is cached.
160195
* @param fid The feature id to look for

‎tests/src/python/test_qgsvectorlayer.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ def createLayerWithTwoPoints():
7474
return layer
7575

7676

77+
def createLayerWithFivePoints():
78+
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
79+
"addfeat", "memory")
80+
pr = layer.dataProvider()
81+
f = QgsFeature()
82+
f.setAttributes(["test", 123])
83+
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100, 200)))
84+
f2 = QgsFeature()
85+
f2.setAttributes(["test2", 457])
86+
f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(200, 200)))
87+
f3 = QgsFeature()
88+
f3.setAttributes(["test2", 888])
89+
f3.setGeometry(QgsGeometry.fromPoint(QgsPoint(300, 200)))
90+
f4 = QgsFeature()
91+
f4.setAttributes(["test3", -1])
92+
f4.setGeometry(QgsGeometry.fromPoint(QgsPoint(400, 300)))
93+
f5 = QgsFeature()
94+
f5.setAttributes(["test4", 0])
95+
f5.setGeometry(QgsGeometry.fromPoint(QgsPoint(0, 0)))
96+
assert pr.addFeatures([f, f2, f3, f4, f5])
97+
assert layer.featureCount() == 5
98+
return layer
99+
100+
77101
def createJoinLayer():
78102
joinLayer = QgsVectorLayer(
79103
"Point?field=x:string&field=y:integer&field=z:integer",
@@ -1050,6 +1074,31 @@ def test_getFeatures(self):
10501074

10511075
self.assertFalse(fi.nextFeature(f))
10521076

1077+
layer2 = createLayerWithFivePoints()
1078+
1079+
# getFeature(fid)
1080+
feat = layer2.getFeature(4)
1081+
self.assertTrue(feat.isValid())
1082+
self.assertEqual(feat['fldtxt'], 'test3')
1083+
self.assertEqual(feat['fldint'], -1)
1084+
feat = layer2.getFeature(10)
1085+
self.assertFalse(feat.isValid())
1086+
1087+
# getFeatures(expression)
1088+
it = layer2.getFeatures("fldint <= 0")
1089+
fids = [f.id() for f in it]
1090+
self.assertEqual(set(fids), set([4, 5]))
1091+
1092+
# getFeatures(fids)
1093+
it = layer2.getFeatures([1, 2])
1094+
fids = [f.id() for f in it]
1095+
self.assertEqual(set(fids), set([1, 2]))
1096+
1097+
# getFeatures(rect)
1098+
it = layer2.getFeatures(QgsRectangle(99, 99, 201, 201))
1099+
fids = [f.id() for f in it]
1100+
self.assertEqual(set(fids), set([1, 2]))
1101+
10531102
def test_join(self):
10541103

10551104
joinLayer = createJoinLayer()

2 commit comments

Comments
 (2)

blazek commented on Jul 19, 2016

@blazek
Member

Here it is failing with

In file included from qgis/src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp:30: qgis/src/core/qgsvectorlayer.h:993:31: error: incomplete result type 'QgsFeatureIterator' in function definition inline QgsFeatureIterator getFeatures( const QString& expression )

etc. etc.

m-kuhn commented on Jul 19, 2016

@m-kuhn
MemberAuthor

Sorry

Please sign in to comment.