Skip to content

Commit d91a45c

Browse files
committedJul 19, 2016
Add shorthands to get features by fid, fids and expression
Adds new methods overloads to QgsVectorLayer getFeatures( expression ) getFeatures( ids ) getFeature( id ) These three methods to query features are by far the most used ones and with this patch it is much easier to write and read code.
1 parent 7343b36 commit d91a45c

File tree

5 files changed

+61
-0
lines changed

5 files changed

+61
-0
lines changed
 

‎python/core/qgsvectorlayer.sip

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,22 @@ class QgsVectorLayer : QgsMapLayer
616616
*/
617617
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;
618618

619+
/**
620+
* Query the provider for features matching a given expression.
621+
*/
622+
QgsFeatureIterator getFeatures( const QString& expression );
623+
624+
/**
625+
* Query the provider for the feature with the given id.
626+
* If there is no such feature, the returned feature will be invalid.
627+
*/
628+
QgsFeature getFeature( QgsFeatureId fid );
629+
630+
/**
631+
* Query the provider for the features with the given ids.
632+
*/
633+
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
634+
619635
/** Adds a feature
620636
@param f feature to add
621637
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.

‎src/core/qgsfeaturerequest.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ QgsFeatureRequest::QgsFeatureRequest( QgsFeatureId fid )
4040
{
4141
}
4242

43+
QgsFeatureRequest::QgsFeatureRequest( QgsFeatureIds fids )
44+
: mFilter( FilterFids )
45+
, mFilterFids( fids )
46+
, mFilterExpression( nullptr )
47+
, mFlags( nullptr )
48+
, mLimit( -1 )
49+
{
50+
51+
}
52+
4353
QgsFeatureRequest::QgsFeatureRequest( const QgsRectangle& rect )
4454
: mFilter( FilterRect )
4555
, mFilterRect( rect )

‎src/core/qgsfeaturerequest.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,8 @@ class CORE_EXPORT QgsFeatureRequest
227227
QgsFeatureRequest();
228228
//! construct a request with feature ID filter
229229
explicit QgsFeatureRequest( QgsFeatureId fid );
230+
//! construct a request with feature ID filter
231+
explicit QgsFeatureRequest( QgsFeatureIds fids );
230232
//! construct a request with rectangle filter
231233
explicit QgsFeatureRequest( const QgsRectangle& rect );
232234
//! construct a request with a filter expression

‎src/core/qgsvectorlayer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,23 @@ 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+
11151132

11161133
bool QgsVectorLayer::addFeature( QgsFeature& feature, bool alsoUpdateExtent )
11171134
{

‎src/core/qgsvectorlayer.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -987,6 +987,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
987987
*/
988988
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;
989989

990+
/**
991+
* Query the provider for features matching a given expression.
992+
*/
993+
QgsFeatureIterator getFeatures( const QString& expression );
994+
995+
/**
996+
* Query the provider for the feature with the given id.
997+
* If there is no such feature, the returned feature will be invalid.
998+
*/
999+
QgsFeature getFeature( QgsFeatureId fid );
1000+
1001+
/**
1002+
* Query the provider for the features with the given ids.
1003+
*/
1004+
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
1005+
9901006
/** Adds a feature
9911007
@param feature feature to add
9921008
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.

0 commit comments

Comments
 (0)
Please sign in to comment.