QGIS API Documentation  2.99.0-Master (f1383e2)
Classes | Public Types | Public Member Functions | Static Public Attributes | Protected Attributes | List of all members
QgsFeatureRequest Class Reference

This class wraps a request for features to a vector layer (or directly its vector data provider). More...

#include <qgsfeaturerequest.h>

Classes

class  OrderBy
 Represents a list of OrderByClauses, with the most important first and the least important last. More...
 
class  OrderByClause
 The OrderByClause class represents an order by clause for a QgsFeatureRequest. More...
 

Public Types

enum  FilterType { FilterNone, FilterFid, FilterExpression, FilterFids }
 Types of filters. More...
 
enum  Flag { NoFlags = 0, NoGeometry = 1, SubsetOfAttributes = 2, ExactIntersect = 4 }
 
enum  InvalidGeometryCheck { GeometryNoCheck = 0, GeometrySkipInvalid = 1, GeometryAbortOnInvalid = 2 }
 Handling of features with invalid geometries. More...
 

Public Member Functions

 QgsFeatureRequest ()
 construct a default request: for all features get attributes and geometries More...
 
 QgsFeatureRequest (QgsFeatureId fid)
 construct a request with feature ID filter More...
 
 QgsFeatureRequest (const QgsFeatureIds &fids)
 construct a request with feature ID filter More...
 
 QgsFeatureRequest (const QgsRectangle &rect)
 construct a request with rectangle filter More...
 
 QgsFeatureRequest (const QgsExpression &expr, const QgsExpressionContext &context=QgsExpressionContext())
 construct a request with a filter expression More...
 
 QgsFeatureRequest (const QgsFeatureRequest &rh)
 copy constructor More...
 
bool acceptFeature (const QgsFeature &feature)
 Check if a feature is accepted by this requests filter. More...
 
QgsFeatureRequestaddOrderBy (const QString &expression, bool ascending=true)
 Adds a new OrderByClause, appending it as the least important one. More...
 
QgsFeatureRequestaddOrderBy (const QString &expression, bool ascending, bool nullsfirst)
 Adds a new OrderByClause, appending it as the least important one. More...
 
QgsFeatureRequestcombineFilterExpression (const QString &expression)
 Modifies the existing filter expression to add an additional expression filter. More...
 
QgsFeatureRequestdisableFilter ()
 Disables filter conditions. More...
 
QgsExpressionContextexpressionContext ()
 Returns the expression context used to evaluate filter expressions. More...
 
QgsExpression * filterExpression () const
 Returns the filter expression if set. More...
 
QgsFeatureId filterFid () const
 Get the feature ID that should be fetched. More...
 
const QgsFeatureIdsfilterFids () const
 Get feature IDs that should be fetched. More...
 
const QgsRectanglefilterRect () const
 Get the rectangle from which features will be taken. More...
 
FilterType filterType () const
 Return the filter type which is currently set on this request. More...
 
const Flags & flags () const
 
std::function< void(const QgsFeature &) > invalidGeometryCallback () const
 Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid. More...
 
InvalidGeometryCheck invalidGeometryCheck () const
 Returns the invalid geometry checking behavior. More...
 
long limit () const
 Returns the maximum number of features to request, or -1 if no limit set. More...
 
QgsFeatureRequestoperator= (const QgsFeatureRequest &rh)
 Assignment operator. More...
 
OrderBy orderBy () const
 Return a list of order by clauses specified for this feature request. More...
 
QgsFeatureRequestsetExpressionContext (const QgsExpressionContext &context)
 Sets the expression context used to evaluate filter expressions. More...
 
QgsFeatureRequestsetFilterExpression (const QString &expression)
 Set the filter expression. More...
 
QgsFeatureRequestsetFilterFid (QgsFeatureId fid)
 Set feature ID that should be fetched. More...
 
QgsFeatureRequestsetFilterFids (const QgsFeatureIds &fids)
 Set feature IDs that should be fetched. More...
 
QgsFeatureRequestsetFilterRect (const QgsRectangle &rect)
 Set rectangle from which features will be taken. More...
 
QgsFeatureRequestsetFlags (QgsFeatureRequest::Flags flags)
 Set flags that affect how features will be fetched. More...
 
QgsFeatureRequestsetInvalidGeometryCallback (std::function< void(const QgsFeature &) > callback)
 Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid. More...
 
QgsFeatureRequestsetInvalidGeometryCheck (InvalidGeometryCheck check)
 Sets invalid geometry checking behavior. More...
 
QgsFeatureRequestsetLimit (long limit)
 Set the maximum number of features to request. More...
 
QgsFeatureRequestsetOrderBy (const OrderBy &orderBy)
 Set a list of order by clauses. More...
 
QgsFeatureRequestsetSimplifyMethod (const QgsSimplifyMethod &simplifyMethod)
 Set a simplification method for geometries that will be fetched. More...
 
QgsFeatureRequestsetSubsetOfAttributes (const QgsAttributeList &attrs)
 Set a subset of attributes that will be fetched. More...
 
QgsFeatureRequestsetSubsetOfAttributes (const QStringList &attrNames, const QgsFields &fields)
 Set a subset of attributes by names that will be fetched. More...
 
QgsFeatureRequestsetSubsetOfAttributes (const QSet< QString > &attrNames, const QgsFields &fields)
 Set a subset of attributes by names that will be fetched. More...
 
const QgsSimplifyMethodsimplifyMethod () const
 Get simplification method for geometries that will be fetched. More...
 
QgsAttributeList subsetOfAttributes () const
 Return the subset of attributes which at least need to be fetched. More...
 

Static Public Attributes

static const QString ALL_ATTRIBUTES = QStringLiteral( "#!allattributes!#" )
 A special attribute that if set matches all attributes. More...
 

Protected Attributes

QgsAttributeList mAttrs
 
QgsExpressionContext mExpressionContext
 
FilterType mFilter = FilterNone
 
std::unique_ptr< QgsExpression > mFilterExpression
 
QgsFeatureId mFilterFid = -1
 
QgsFeatureIds mFilterFids
 
QgsRectangle mFilterRect
 
Flags mFlags
 
std::function< void(const QgsFeature &) > mInvalidGeometryCallback
 
InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck
 
long mLimit = -1
 
OrderBy mOrderBy
 
QgsSimplifyMethod mSimplifyMethod
 

Detailed Description

This class wraps a request for features to a vector layer (or directly its vector data provider).

The request may apply a filter to fetch only a particular subset of features. Currently supported filters:

Additionally a spatial rectangle can be set in combination: Only features that intersect given rectangle should be fetched. For the sake of speed, the intersection is often done only using feature's bounding box. There is a flag ExactIntersect that makes sure that only intersecting features will be returned.

For efficiency, it is also possible to tell provider that some data is not required:

The options may be chained, e.g.: QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)).setFlags(QgsFeatureRequest::ExactIntersect)

Examples:

Definition at line 66 of file qgsfeaturerequest.h.

Member Enumeration Documentation

◆ FilterType

Types of filters.

Enumerator
FilterNone 

No filter is applied.

FilterFid 

Filter using feature ID.

FilterExpression 

Filter using expression.

FilterFids 

Filter using feature IDs.

Definition at line 81 of file qgsfeaturerequest.h.

◆ Flag

Enumerator
NoFlags 
NoGeometry 

Geometry is not required. It may still be returned if e.g. required for a filter condition.

SubsetOfAttributes 

Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)

ExactIntersect 

Use exact geometry intersection (slower) instead of bounding boxes.

Definition at line 69 of file qgsfeaturerequest.h.

◆ InvalidGeometryCheck

Handling of features with invalid geometries.

Enumerator
GeometryNoCheck 

No invalid geometry checking.

GeometrySkipInvalid 

Skip any features with invalid geometry. This requires a slow geometry validity check for every feature.

GeometryAbortOnInvalid 

Close iterator on encountering any features with invalid geometry. This requires a slow geometry validity check for every feature.

Definition at line 90 of file qgsfeaturerequest.h.

Constructor & Destructor Documentation

◆ QgsFeatureRequest() [1/6]

QgsFeatureRequest::QgsFeatureRequest ( )

construct a default request: for all features get attributes and geometries

Definition at line 25 of file qgsfeaturerequest.cpp.

◆ QgsFeatureRequest() [2/6]

QgsFeatureRequest::QgsFeatureRequest ( QgsFeatureId  fid)
explicit

construct a request with feature ID filter

Definition at line 30 of file qgsfeaturerequest.cpp.

◆ QgsFeatureRequest() [3/6]

QgsFeatureRequest::QgsFeatureRequest ( const QgsFeatureIds fids)
explicit

construct a request with feature ID filter

Definition at line 37 of file qgsfeaturerequest.cpp.

◆ QgsFeatureRequest() [4/6]

QgsFeatureRequest::QgsFeatureRequest ( const QgsRectangle rect)
explicit

construct a request with rectangle filter

Definition at line 45 of file qgsfeaturerequest.cpp.

◆ QgsFeatureRequest() [5/6]

QgsFeatureRequest::QgsFeatureRequest ( const QgsExpression &  expr,
const QgsExpressionContext context = QgsExpressionContext() 
)
explicit

construct a request with a filter expression

Definition at line 51 of file qgsfeaturerequest.cpp.

◆ QgsFeatureRequest() [6/6]

QgsFeatureRequest::QgsFeatureRequest ( const QgsFeatureRequest rh)

copy constructor

Definition at line 59 of file qgsfeaturerequest.cpp.

Member Function Documentation

◆ acceptFeature()

bool QgsFeatureRequest::acceptFeature ( const QgsFeature feature)

Check if a feature is accepted by this requests filter.

Parameters
featureThe feature which will be tested
Returns
true, if the filter accepts the feature
Since
QGIS 2.1

Definition at line 237 of file qgsfeaturerequest.cpp.

◆ addOrderBy() [1/2]

QgsFeatureRequest & QgsFeatureRequest::addOrderBy ( const QString &  expression,
bool  ascending = true 
)

Adds a new OrderByClause, appending it as the least important one.

Parameters
expressionThe expression to use for ordering
ascendingIf the order should be ascending (1,2,3) or descending (3,2,1) If the order is ascending, by default nulls are last If the order is descending, by default nulls are first
Since
QGIS 2.14

Definition at line 147 of file qgsfeaturerequest.cpp.

◆ addOrderBy() [2/2]

QgsFeatureRequest & QgsFeatureRequest::addOrderBy ( const QString &  expression,
bool  ascending,
bool  nullsfirst 
)

Adds a new OrderByClause, appending it as the least important one.

Parameters
expressionThe expression to use for ordering
ascendingIf the order should be ascending (1,2,3) or descending (3,2,1)
nullsfirstIf true, NULLS are at the beginning, if false, NULLS are at the end
Since
QGIS 2.14

Definition at line 153 of file qgsfeaturerequest.cpp.

◆ combineFilterExpression()

QgsFeatureRequest & QgsFeatureRequest::combineFilterExpression ( const QString &  expression)

Modifies the existing filter expression to add an additional expression filter.

The filter expressions are combined using AND, so only features matching both the existing expression and the additional expression will be returned.

Since
QGIS 2.14

Definition at line 128 of file qgsfeaturerequest.cpp.

◆ disableFilter()

QgsFeatureRequest& QgsFeatureRequest::disableFilter ( )
inline

Disables filter conditions.

The spatial filter (filterRect) will be kept in place.

Returns
The object the method is called on for chaining
Since
QGIS 2.12

Definition at line 405 of file qgsfeaturerequest.h.

◆ expressionContext()

QgsExpressionContext* QgsFeatureRequest::expressionContext ( )
inline

Returns the expression context used to evaluate filter expressions.

Since
QGIS 2.12
See also
setExpressionContext
filterExpression

Definition at line 388 of file qgsfeaturerequest.h.

◆ filterExpression()

QgsExpression* QgsFeatureRequest::filterExpression ( ) const
inline

Returns the filter expression if set.

See also
setFilterExpression
expressionContext

Definition at line 374 of file qgsfeaturerequest.h.

◆ filterFid()

QgsFeatureId QgsFeatureRequest::filterFid ( ) const
inline

Get the feature ID that should be fetched.

Definition at line 304 of file qgsfeaturerequest.h.

◆ filterFids()

const QgsFeatureIds& QgsFeatureRequest::filterFids ( ) const
inline

Get feature IDs that should be fetched.

Definition at line 309 of file qgsfeaturerequest.h.

◆ filterRect()

const QgsRectangle& QgsFeatureRequest::filterRect ( ) const
inline

Get the rectangle from which features will be taken.

If the returned rectangle is null, then no filter rectangle is set.

Definition at line 299 of file qgsfeaturerequest.h.

◆ filterType()

FilterType QgsFeatureRequest::filterType ( ) const
inline

Return the filter type which is currently set on this request.

Returns
Filter type

Definition at line 288 of file qgsfeaturerequest.h.

◆ flags()

const Flags& QgsFeatureRequest::flags ( ) const
inline

Definition at line 460 of file qgsfeaturerequest.h.

◆ invalidGeometryCallback()

std::function< void( const QgsFeature & ) > QgsFeatureRequest::invalidGeometryCallback ( ) const
inline

Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid.

Since
QGIS 3.0
Note
not available in Python bindings
See also
setInvalidGeometryCallback()

Definition at line 361 of file qgsfeaturerequest.h.

◆ invalidGeometryCheck()

InvalidGeometryCheck QgsFeatureRequest::invalidGeometryCheck ( ) const
inline

Returns the invalid geometry checking behavior.

See also
setInvalidGeometryCheck()
Since
QGIS 3.0

Definition at line 325 of file qgsfeaturerequest.h.

◆ limit()

long QgsFeatureRequest::limit ( ) const
inline

Returns the maximum number of features to request, or -1 if no limit set.

See also
setLimit
Since
QGIS 2.14

Definition at line 456 of file qgsfeaturerequest.h.

◆ operator=()

QgsFeatureRequest & QgsFeatureRequest::operator= ( const QgsFeatureRequest rh)

Assignment operator.

Definition at line 64 of file qgsfeaturerequest.cpp.

◆ orderBy()

QgsFeatureRequest::OrderBy QgsFeatureRequest::orderBy ( ) const

Return a list of order by clauses specified for this feature request.

Since
QGIS 2.14

Definition at line 159 of file qgsfeaturerequest.cpp.

◆ setExpressionContext()

QgsFeatureRequest & QgsFeatureRequest::setExpressionContext ( const QgsExpressionContext context)

Sets the expression context used to evaluate filter expressions.

Since
QGIS 2.12
See also
expressionContext
setFilterExpression

Definition at line 141 of file qgsfeaturerequest.cpp.

◆ setFilterExpression()

QgsFeatureRequest & QgsFeatureRequest::setFilterExpression ( const QString &  expression)

Set the filter expression.

{

See also
QgsExpression}
Parameters
expressionexpression string
See also
filterExpression
setExpressionContext

Definition at line 121 of file qgsfeaturerequest.cpp.

◆ setFilterFid()

QgsFeatureRequest & QgsFeatureRequest::setFilterFid ( QgsFeatureId  fid)

Set feature ID that should be fetched.

Definition at line 95 of file qgsfeaturerequest.cpp.

◆ setFilterFids()

QgsFeatureRequest & QgsFeatureRequest::setFilterFids ( const QgsFeatureIds fids)

Set feature IDs that should be fetched.

Definition at line 102 of file qgsfeaturerequest.cpp.

◆ setFilterRect()

QgsFeatureRequest & QgsFeatureRequest::setFilterRect ( const QgsRectangle rect)

Set rectangle from which features will be taken.

Empty rectangle removes the filter.

Definition at line 89 of file qgsfeaturerequest.cpp.

◆ setFlags()

QgsFeatureRequest & QgsFeatureRequest::setFlags ( QgsFeatureRequest::Flags  flags)

Set flags that affect how features will be fetched.

Definition at line 176 of file qgsfeaturerequest.cpp.

◆ setInvalidGeometryCallback()

QgsFeatureRequest & QgsFeatureRequest::setInvalidGeometryCallback ( std::function< void(const QgsFeature &) >  callback)

Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid.

This function will be called using the feature with invalid geometry as a parameter.

Since
QGIS 3.0
See also
invalidGeometryCallback()

Definition at line 115 of file qgsfeaturerequest.cpp.

◆ setInvalidGeometryCheck()

QgsFeatureRequest & QgsFeatureRequest::setInvalidGeometryCheck ( QgsFeatureRequest::InvalidGeometryCheck  check)

Sets invalid geometry checking behavior.

Note
Invalid geometry checking is not performed when retrieving features directly from a QgsVectorDataProvider.
See also
invalidGeometryCheck()
Since
QGIS 3.0

Definition at line 109 of file qgsfeaturerequest.cpp.

◆ setLimit()

QgsFeatureRequest & QgsFeatureRequest::setLimit ( long  limit)

Set the maximum number of features to request.

Parameters
limitmaximum number of features, or -1 to request all features.
See also
limit()
Since
QGIS 2.14

Definition at line 170 of file qgsfeaturerequest.cpp.

◆ setOrderBy()

QgsFeatureRequest & QgsFeatureRequest::setOrderBy ( const OrderBy orderBy)

Set a list of order by clauses.

Since
QGIS 2.14

Definition at line 164 of file qgsfeaturerequest.cpp.

◆ setSimplifyMethod()

QgsFeatureRequest & QgsFeatureRequest::setSimplifyMethod ( const QgsSimplifyMethod simplifyMethod)

Set a simplification method for geometries that will be fetched.

Since
QGIS 2.2

Definition at line 231 of file qgsfeaturerequest.cpp.

◆ setSubsetOfAttributes() [1/3]

QgsFeatureRequest & QgsFeatureRequest::setSubsetOfAttributes ( const QgsAttributeList attrs)

Set a subset of attributes that will be fetched.

Empty list means that all attributes are used. To disable fetching attributes, reset the FetchAttributes flag (which is set by default)

Definition at line 182 of file qgsfeaturerequest.cpp.

◆ setSubsetOfAttributes() [2/3]

QgsFeatureRequest & QgsFeatureRequest::setSubsetOfAttributes ( const QStringList &  attrNames,
const QgsFields fields 
)

Set a subset of attributes by names that will be fetched.

Definition at line 189 of file qgsfeaturerequest.cpp.

◆ setSubsetOfAttributes() [3/3]

QgsFeatureRequest & QgsFeatureRequest::setSubsetOfAttributes ( const QSet< QString > &  attrNames,
const QgsFields fields 
)

Set a subset of attributes by names that will be fetched.

Definition at line 210 of file qgsfeaturerequest.cpp.

◆ simplifyMethod()

const QgsSimplifyMethod& QgsFeatureRequest::simplifyMethod ( ) const
inline

Get simplification method for geometries that will be fetched.

Since
QGIS 2.2

Definition at line 483 of file qgsfeaturerequest.h.

◆ subsetOfAttributes()

QgsAttributeList QgsFeatureRequest::subsetOfAttributes ( ) const
inline

Return the subset of attributes which at least need to be fetched.

Returns
A list of attributes to be fetched

Definition at line 470 of file qgsfeaturerequest.h.

Member Data Documentation

◆ ALL_ATTRIBUTES

const QString QgsFeatureRequest::ALL_ATTRIBUTES = QStringLiteral( "#!allattributes!#" )
static

A special attribute that if set matches all attributes.

Definition at line 266 of file qgsfeaturerequest.h.

◆ mAttrs

QgsAttributeList QgsFeatureRequest::mAttrs
protected

Definition at line 504 of file qgsfeaturerequest.h.

◆ mExpressionContext

QgsExpressionContext QgsFeatureRequest::mExpressionContext
protected

Definition at line 502 of file qgsfeaturerequest.h.

◆ mFilter

FilterType QgsFeatureRequest::mFilter = FilterNone
protected

Definition at line 497 of file qgsfeaturerequest.h.

◆ mFilterExpression

std::unique_ptr< QgsExpression > QgsFeatureRequest::mFilterExpression
protected

Definition at line 501 of file qgsfeaturerequest.h.

◆ mFilterFid

QgsFeatureId QgsFeatureRequest::mFilterFid = -1
protected

Definition at line 499 of file qgsfeaturerequest.h.

◆ mFilterFids

QgsFeatureIds QgsFeatureRequest::mFilterFids
protected

Definition at line 500 of file qgsfeaturerequest.h.

◆ mFilterRect

QgsRectangle QgsFeatureRequest::mFilterRect
protected

Definition at line 498 of file qgsfeaturerequest.h.

◆ mFlags

Flags QgsFeatureRequest::mFlags
protected

Definition at line 503 of file qgsfeaturerequest.h.

◆ mInvalidGeometryCallback

std::function< void( const QgsFeature & ) > QgsFeatureRequest::mInvalidGeometryCallback
protected

Definition at line 509 of file qgsfeaturerequest.h.

◆ mInvalidGeometryFilter

InvalidGeometryCheck QgsFeatureRequest::mInvalidGeometryFilter = GeometryNoCheck
protected

Definition at line 508 of file qgsfeaturerequest.h.

◆ mLimit

long QgsFeatureRequest::mLimit = -1
protected

Definition at line 506 of file qgsfeaturerequest.h.

◆ mOrderBy

OrderBy QgsFeatureRequest::mOrderBy
protected

Definition at line 507 of file qgsfeaturerequest.h.

◆ mSimplifyMethod

QgsSimplifyMethod QgsFeatureRequest::mSimplifyMethod
protected

Definition at line 505 of file qgsfeaturerequest.h.


The documentation for this class was generated from the following files: