QgsFeatureRequest class
This class wraps a request for features to a vector layer (or directly its vector data provider).
Contents
The request may apply a filter to fetch only a particular subset of features. Currently supported filters:
- no filter - all features are returned
- feature id - only feature that matches given feature id is returned
- feature ids - only features that match any of the given feature ids are returned
- filter expression - only features that match the given filter expression are returned
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:
- NoGeometry flag
- SubsetOfAttributes flag
- SimplifyMethod for geometries to fetch
The options may be chained, e.g.:
QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)).setFlags(QgsFeatureRequest.ExactIntersect)
Examples:
# fetch all features: QgsFeatureRequest() # fetch all features, only one attribute QgsFeatureRequest().setSubsetOfAttributes(['myfield'], layer.fields()) # fetch all features, without geometries QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry) # fetch only features from particular extent QgsFeatureRequest().setFilterRect(QgsRectangle(0,0,1,1)) # fetch only one feature QgsFeatureRequest().setFilterFid(45)
Public types
- class OrderBy
- Represents a list of OrderByClauses, with the most important first and the least important last.
- class OrderByClause
- The OrderByClause class represents an order by clause for a QgsFeatureRequest.
- enum FilterType { FilterNone, FilterFid, FilterExpression, FilterFids }
- Types of filters.
- enum Flag { NoFlags = 0, NoGeometry = 1, SubsetOfAttributes = 2, ExactIntersect = 4 }
- enum InvalidGeometryCheck { GeometryNoCheck = 0, GeometrySkipInvalid = 1, GeometryAbortOnInvalid = 2 }
- Handling of features with invalid geometries.
Public static variables
- static const QString ALL_ATTRIBUTES
- A special attribute that if set matches all attributes.
Constructors, destructors, conversion operators
- QgsFeatureRequest()
- construct a default request: for all features get attributes and geometries
- QgsFeatureRequest(QgsFeatureId fid) explicit
- construct a request with feature ID filter
- QgsFeatureRequest(const QgsFeatureIds& fids) explicit
- construct a request with feature ID filter
- QgsFeatureRequest(const QgsRectangle& rectangle) explicit
- Construct a request with rectangle bounding box filter.
- QgsFeatureRequest(const QgsExpression& expr, const QgsExpressionContext& context = QgsExpressionContext()) explicit
- construct a request with a filter expression
- QgsFeatureRequest(const QgsFeatureRequest& rh)
- copy constructor
Public functions
- auto acceptFeature(const QgsFeature& feature) -> bool
- Check if a feature is accepted by this requests filter.
- auto addOrderBy(const QString& expression, bool ascending = true) -> QgsFeatureRequest&
- Adds a new OrderByClause, appending it as the least important one.
- auto addOrderBy(const QString& expression, bool ascending, bool nullsfirst) -> QgsFeatureRequest&
- Adds a new OrderByClause, appending it as the least important one.
- auto combineFilterExpression(const QString& expression) -> QgsFeatureRequest&
- Modifies the existing filter expression to add an additional expression filter.
- auto connectionTimeout() const -> Q_DECL_DEPRECATED int deprecated
- Returns the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment.
- auto destinationCrs() const -> QgsCoordinateReferenceSystem
- Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordinateReferenceSystem if no reprojection will be done and all features will be left with their original geometry.
- auto disableFilter() -> QgsFeatureRequest&
- Disables filter conditions.
- auto expressionContext() -> QgsExpressionContext*
- Returns the expression context used to evaluate filter expressions.
- auto filterExpression() const -> QgsExpression*
- Returns the filter expression if set.
- auto filterFid() const -> QgsFeatureId
- Gets the feature ID that should be fetched.
- auto filterFids() const -> const QgsFeatureIds&
- Gets feature IDs that should be fetched.
- auto filterRect() const -> const QgsRectangle&
- Returns the rectangle from which features will be taken.
- auto filterType() const -> FilterType
- Returns the filter type which is currently set on this request.
- auto invalidGeometryCallback() const -> std::function<void(const QgsFeature&)>
- Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid.
- auto invalidGeometryCheck() const -> InvalidGeometryCheck
- Returns the invalid geometry checking behavior.
- auto limit() const -> long
- Returns the maximum number of features to request, or -1 if no limit set.
- auto operator=(const QgsFeatureRequest& rh) -> QgsFeatureRequest&
- Assignment operator.
- auto orderBy() const -> OrderBy
- Returns a list of order by clauses specified for this feature request.
- auto requestMayBeNested() const -> bool
- In case this request may be run nested within another already running iteration on the same connection, set this to true.
- auto setConnectionTimeout(int connectionTimeout) -> Q_DECL_DEPRECATED QgsFeatureRequest& deprecated
- Sets the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment.
- auto setDestinationCrs(const QgsCoordinateReferenceSystem& crs, const QgsCoordinateTransformContext& context) -> QgsFeatureRequest&
- Sets the destination crs for feature's geometries.
- auto setExpressionContext(const QgsExpressionContext& context) -> QgsFeatureRequest&
- Sets the expression context used to evaluate filter expressions.
- auto setFilterExpression(const QString& expression) -> QgsFeatureRequest&
- Set the filter expression.
- auto setFilterFid(QgsFeatureId fid) -> QgsFeatureRequest&
- Sets feature ID that should be fetched.
- auto setFilterFids(const QgsFeatureIds& fids) -> QgsFeatureRequest&
- Sets feature IDs that should be fetched.
- auto setFilterRect(const QgsRectangle& rectangle) -> QgsFeatureRequest&
- Sets the rectangle from which features will be taken.
- auto setFlags(QgsFeatureRequest::Flags flags) -> QgsFeatureRequest&
- Sets flags that affect how features will be fetched.
- auto setInvalidGeometryCallback(const std::function<void(const QgsFeature&)>& callback) -> QgsFeatureRequest&
- Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid.
- auto setInvalidGeometryCheck(InvalidGeometryCheck check) -> QgsFeatureRequest&
- Sets invalid geometry checking behavior.
- auto setLimit(long limit) -> QgsFeatureRequest&
- Set the maximum number of features to request.
- auto setNoAttributes() -> QgsFeatureRequest&
- Set that no attributes will be fetched.
- auto setOrderBy(const OrderBy& orderBy) -> QgsFeatureRequest&
- Set a list of order by clauses.
- auto setRequestMayBeNested(bool requestMayBeNested) -> QgsFeatureRequest&
- In case this request may be run nested within another already running iteration on the same connection, set this to true.
- auto setSimplifyMethod(const QgsSimplifyMethod& simplifyMethod) -> QgsFeatureRequest&
- Set a simplification method for geometries that will be fetched.
- auto setSubsetOfAttributes(const QgsAttributeList& attrs) -> QgsFeatureRequest&
- Set a subset of attributes that will be fetched.
- auto setSubsetOfAttributes(const QStringList& attrNames, const QgsFields& fields) -> QgsFeatureRequest&
- Sets a subset of attributes by names that will be fetched.
- auto setSubsetOfAttributes(const QSet<QString>& attrNames, const QgsFields& fields) -> QgsFeatureRequest&
- Sets a subset of attributes by names that will be fetched.
- auto setTimeout(int timeout) -> QgsFeatureRequest&
- Sets the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned.
- auto setTransformErrorCallback(const std::function<void(const QgsFeature&)>& callback) -> QgsFeatureRequest&
- Sets a callback function to use when encountering a transform error when iterating features and a destinationCrs() is set.
- auto simplifyMethod() const -> const QgsSimplifyMethod&
- Gets simplification method for geometries that will be fetched.
- auto subsetOfAttributes() const -> QgsAttributeList
- Returns the subset of attributes which at least need to be fetched.
- auto timeout() const -> int
- Returns the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned.
- auto transformContext() const -> QgsCoordinateTransformContext
- Returns the transform context, for use when a destinationCrs() has been set and reprojection is required.
- auto transformErrorCallback() const -> std::function<void(const QgsFeature&)>
- Returns the callback function to use when encountering a transform error when iterating features and a destinationCrs() is set.
Enum documentation
enum QgsFeatureRequest:: FilterType
Types of filters.
| Enumerators | |
|---|---|
| FilterNone |
No filter is applied. |
| FilterFid |
Filter using feature ID. |
| FilterExpression |
Filter using expression. |
| FilterFids |
Filter using feature IDs. |
enum QgsFeatureRequest:: Flag
| Enumerators | |
|---|---|
| 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. |
enum QgsFeatureRequest:: InvalidGeometryCheck
Handling of features with invalid geometries.
| Enumerators | |
|---|---|
| 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. |
Function documentation
QgsFeatureRequest:: QgsFeatureRequest(const QgsRectangle& rectangle) explicit
Construct a request with rectangle bounding box filter.
When a destination CRS is set using setDestinationCrs(), rectangle is expected to be in the same CRS as the destinationCrs(). Otherwise, rectangle should use the same CRS as the source layer/provider.
bool QgsFeatureRequest:: acceptFeature(const QgsFeature& feature)
Check if a feature is accepted by this requests filter.
| Parameters | |
|---|---|
| feature | The feature which will be tested |
| Returns | true, if the filter accepts the feature |
QgsFeatureRequest& QgsFeatureRequest:: addOrderBy(const QString& expression,
bool ascending = true)
Adds a new OrderByClause, appending it as the least important one.
| Parameters | |
|---|---|
| expression | The expression to use for ordering |
| ascending | If 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 |
QgsFeatureRequest& QgsFeatureRequest:: addOrderBy(const QString& expression,
bool ascending,
bool nullsfirst)
Adds a new OrderByClause, appending it as the least important one.
| Parameters | |
|---|---|
| expression | The expression to use for ordering |
| ascending | If the order should be ascending (1,2,3) or descending (3,2,1) |
| nullsfirst | If true, NULLS are at the beginning, if false, NULLS are at the end |
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.
Q_DECL_DEPRECATED int QgsFeatureRequest:: connectionTimeout() const
Returns the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment.
A negative value (which is set by default) will wait forever.
QgsCoordinateReferenceSystem QgsFeatureRequest:: destinationCrs() const
Returns the destination coordinate reference system for feature's geometries, or an invalid QgsCoordinateReferenceSystem if no reprojection will be done and all features will be left with their original geometry.
QgsFeatureRequest& QgsFeatureRequest:: disableFilter()
Disables filter conditions.
| Returns | The object the method is called on for chaining |
|---|
The spatial filter (filterRect) will be kept in place.
QgsExpressionContext* QgsFeatureRequest:: expressionContext()
Returns the expression context used to evaluate filter expressions.
QgsExpression* QgsFeatureRequest:: filterExpression() const
Returns the filter expression if set.
const QgsRectangle& QgsFeatureRequest:: filterRect() const
Returns the rectangle from which features will be taken.
If the returned rectangle is null, then no filter rectangle is set.
When a destination CRS is set using setDestinationCrs(), the rectangle will be in the same CRS as the destinationCrs(). Otherwise, the rectangle will use the same CRS as the source layer/provider.
FilterType QgsFeatureRequest:: filterType() const
Returns the filter type which is currently set on this request.
| Returns | Filter type |
|---|
std::function<void(const QgsFeature&)> QgsFeatureRequest:: invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid.
InvalidGeometryCheck QgsFeatureRequest:: invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
long QgsFeatureRequest:: limit() const
Returns the maximum number of features to request, or -1 if no limit set.
bool QgsFeatureRequest:: requestMayBeNested() const
In case this request may be run nested within another already running iteration on the same connection, set this to true.
If this flag is true, this request will be able to make use of "spare" connections to avoid deadlocks.
For example, this should be set on requests that are issued from an expression function.
Q_DECL_DEPRECATED QgsFeatureRequest& QgsFeatureRequest:: setConnectionTimeout(int connectionTimeout)
Sets the timeout (in milliseconds) for how long we should wait for a connection if none is available from the pool at this moment.
A negative value (which is set by default) will wait forever.
QgsFeatureRequest& QgsFeatureRequest:: setDestinationCrs(const QgsCoordinateReferenceSystem& crs,
const QgsCoordinateTransformContext& context)
Sets the destination crs for feature's geometries.
If set, all geometries will be reprojected from their original coordinate reference system to this desired reference system. If crs is an invalid QgsCoordinateReferenceSystem then no reprojection will be done and all features will be left with their original geometry.
When a crs is set using setDestinationCrs(), then any filterRect() set on the request is expected to be in the same CRS as the destination CRS.
The feature geometry transformation to the destination CRS is performed after all filter expressions are tested and any virtual fields are calculated. Accordingly, any geometric expressions used in filterExpression() will be performed in the original source CRS. This ensures consistent results are returned regardless of the destination CRS. Similarly, virtual field values will be calculated using the original geometry in the source CRS, so these values are not affected by any destination CRS transform present in the feature request.
QgsFeatureRequest& QgsFeatureRequest:: setExpressionContext(const QgsExpressionContext& context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest& QgsFeatureRequest:: setFilterExpression(const QString& expression)
Set the filter expression.
| Parameters | |
|---|---|
| expression | expression string |
{
QgsFeatureRequest& QgsFeatureRequest:: setFilterRect(const QgsRectangle& rectangle)
Sets the rectangle from which features will be taken.
An empty rectangle removes the filter.
When a destination CRS is set using setDestinationCrs(), rectangle is expected to be in the same CRS as the destinationCrs(). Otherwise, rectangle should use the same CRS as the source layer/provider.
QgsFeatureRequest& QgsFeatureRequest:: setInvalidGeometryCallback(const std::function<void(const QgsFeature&)>& callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid.
This function will be called using the feature with invalid geometry as a parameter.
QgsFeatureRequest& QgsFeatureRequest:: setInvalidGeometryCheck(InvalidGeometryCheck check)
Sets invalid geometry checking behavior.
QgsFeatureRequest& QgsFeatureRequest:: setLimit(long limit)
Set the maximum number of features to request.
| Parameters | |
|---|---|
| limit | maximum number of features, or -1 to request all features. |
QgsFeatureRequest& QgsFeatureRequest:: setNoAttributes()
Set that no attributes will be fetched.
To revert a call to setNoAttributes and fetch all or some available attributes, the SubsetOfAttributes flag should be removed from the request.
QgsFeatureRequest& QgsFeatureRequest:: setOrderBy(const OrderBy& orderBy)
Set a list of order by clauses.
QgsFeatureRequest& QgsFeatureRequest:: setRequestMayBeNested(bool requestMayBeNested)
In case this request may be run nested within another already running iteration on the same connection, set this to true.
If this flag is true, this request will be able to make use of "spare" connections to avoid deadlocks.
For example, this should be set on requests that are issued from an expression function.
QgsFeatureRequest& QgsFeatureRequest:: setSimplifyMethod(const QgsSimplifyMethod& simplifyMethod)
Set a simplification method for geometries that will be fetched.
QgsFeatureRequest& QgsFeatureRequest:: setSubsetOfAttributes(const QgsAttributeList& attrs)
Set a subset of attributes that will be fetched.
An empty attributes list indicates that no attributes will be fetched. To revert a call to setSubsetOfAttributes and fetch all available attributes, the SubsetOfAttributes flag should be removed from the request.
QgsFeatureRequest& QgsFeatureRequest:: setTimeout(int timeout)
Sets the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned.
A negative value (which is set by default) will wait forever.
QgsFeatureRequest& QgsFeatureRequest:: setTransformErrorCallback(const std::function<void(const QgsFeature&)>& callback)
Sets a callback function to use when encountering a transform error when iterating features and a destinationCrs() is set.
This function will be called using the feature which encountered the transform error as a parameter.
const QgsSimplifyMethod& QgsFeatureRequest:: simplifyMethod() const
Gets simplification method for geometries that will be fetched.
QgsAttributeList QgsFeatureRequest:: subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
| Returns | A list of attributes to be fetched |
|---|
int QgsFeatureRequest:: timeout() const
Returns the timeout (in milliseconds) for the maximum time we should wait during feature requests before a feature is returned.
A negative value (which is set by default) will wait forever.
QgsCoordinateTransformContext QgsFeatureRequest:: transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is required.
std::function<void(const QgsFeature&)> QgsFeatureRequest:: transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features and a destinationCrs() is set.