Skip to content
Permalink
Browse files

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.
  • Loading branch information
m-kuhn committed Jul 19, 2016
1 parent 7343b36 commit d91a45ceaf7e88fddaadacb64bcb8df0126842ad
@@ -616,6 +616,22 @@ class QgsVectorLayer : QgsMapLayer
*/
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;

/**
* Query the provider for features matching a given expression.
*/
QgsFeatureIterator getFeatures( const QString& expression );

/**
* Query the provider for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
QgsFeature getFeature( QgsFeatureId fid );

/**
* Query the provider for the features with the given ids.
*/
QgsFeatureIterator getFeatures( QgsFeatureIds fids );

/** Adds a feature
@param f feature to add
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
@@ -40,6 +40,16 @@ QgsFeatureRequest::QgsFeatureRequest( QgsFeatureId fid )
{
}

QgsFeatureRequest::QgsFeatureRequest( QgsFeatureIds fids )
: mFilter( FilterFids )
, mFilterFids( fids )
, mFilterExpression( nullptr )
, mFlags( nullptr )
, mLimit( -1 )
{

}

QgsFeatureRequest::QgsFeatureRequest( const QgsRectangle& rect )
: mFilter( FilterRect )
, mFilterRect( rect )
@@ -227,6 +227,8 @@ class CORE_EXPORT QgsFeatureRequest
QgsFeatureRequest();
//! construct a request with feature ID filter
explicit QgsFeatureRequest( QgsFeatureId fid );
//! construct a request with feature ID filter
explicit QgsFeatureRequest( QgsFeatureIds fids );
//! construct a request with rectangle filter
explicit QgsFeatureRequest( const QgsRectangle& rect );
//! construct a request with a filter expression
@@ -1112,6 +1112,23 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest& request
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
}

QgsFeatureIterator QgsVectorLayer::getFeatures( const QString& expression )
{
return getFeatures( QgsFeatureRequest( expression ) );
}

QgsFeature QgsVectorLayer::getFeature( QgsFeatureId fid )
{
QgsFeature feature;
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
return feature;
}

QgsFeatureIterator QgsVectorLayer::getFeatures( QgsFeatureIds fids )
{
return getFeatures( QgsFeatureRequest( fids ) );
}


bool QgsVectorLayer::addFeature( QgsFeature& feature, bool alsoUpdateExtent )
{
@@ -987,6 +987,22 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
*/
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;

/**
* Query the provider for features matching a given expression.
*/
QgsFeatureIterator getFeatures( const QString& expression );

/**
* Query the provider for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
QgsFeature getFeature( QgsFeatureId fid );

/**
* Query the provider for the features with the given ids.
*/
QgsFeatureIterator getFeatures( QgsFeatureIds fids );

/** Adds a feature
@param feature feature to add
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.

0 comments on commit d91a45c

Please sign in to comment.
You can’t perform that action at this time.