Subgroup: Feature

Class: QgsFeatureRequest

class qgis.core.QgsFeatureRequest

Bases: sip.wrapper

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

QgsFeatureRequest(fid: int) construct a request with feature ID filter

QgsFeatureRequest(fids: object) construct a request with feature ID filter

QgsFeatureRequest(rectangle: QgsRectangle) 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.

QgsFeatureRequest(expr: QgsExpression, context: QgsExpressionContext = QgsExpressionContext()) construct a request with a filter expression

QgsFeatureRequest(rh: QgsFeatureRequest) copy constructor

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: - 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(QStringList(“myfield”), provider->fieldMap()) - 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)

Methods

acceptFeature Check if a feature is accepted by this requests filter
addOrderBy Adds a new OrderByClause, appending it as the least important one.
combineFilterExpression Modifies the existing filter expression to add an additional expression filter.
connectionTimeout The timeout for how long we should wait for a connection if none is available from the pool at this moment.
destinationCrs 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.
disableFilter Disables filter conditions.
expressionContext Returns the expression context used to evaluate filter expressions.
filterExpression Returns the filter expression if set.
filterFid Get the feature ID that should be fetched.
filterFids Get feature IDs that should be fetched.
filterRect Returns the rectangle from which features will be taken.
filterType Return the filter type which is currently set on this request
flags
invalidGeometryCheck Returns the invalid geometry checking behavior.
limit Returns the maximum number of features to request, or -1 if no limit set.
orderBy Return a list of order by clauses specified for this feature request.
setConnectionTimeout The timeout for how long we should wait for a connection if none is available from the pool at this moment.
setDestinationCrs Sets the destination crs for feature’s geometries.
setExpressionContext Sets the expression context used to evaluate filter expressions.
setFilterExpression Set the filter expression.
setFilterFid Set feature ID that should be fetched.
setFilterFids Set feature IDs that should be fetched.
setFilterRect Sets the rectangle from which features will be taken.
setFlags Set flags that affect how features will be fetched
setInvalidGeometryCallback Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is set to GeometryAbortOnInvalid or GeometrySkipInvalid.
setInvalidGeometryCheck Sets invalid geometry checking behavior.
setLimit Set the maximum number of features to request.
setOrderBy Set a list of order by clauses.
setSimplifyMethod Set a simplification method for geometries that will be fetched
setSubsetOfAttributes Set a subset of attributes that will be fetched.
setTransformErrorCallback Sets a callback function to use when encountering a transform error when iterating features and a destinationCrs() is set.
simplifyMethod Get simplification method for geometries that will be fetched
subsetOfAttributes Return the subset of attributes which at least need to be fetched
transformContext Returns the transform context, for use when a destinationCrs() has been set and reprojection is required

Signals

Attributes

ALL_ATTRIBUTES
ExactIntersect
FilterExpression
FilterFid
FilterFids
FilterNone
GeometryAbortOnInvalid
GeometryNoCheck
GeometrySkipInvalid
NoFlags
NoGeometry
SubsetOfAttributes
ALL_ATTRIBUTES = '#!allattributes!#'
ExactIntersect = 4
FilterExpression = 2
FilterFid = 1
FilterFids = 3
FilterNone = 0
class FilterType

Bases: int

class Flag

Bases: int

class Flags

Bases: sip.wrapper

QgsFeatureRequest.Flags(Union[QgsFeatureRequest.Flags, QgsFeatureRequest.Flag]) QgsFeatureRequest.Flags(QgsFeatureRequest.Flags)

GeometryAbortOnInvalid = 2
GeometryNoCheck = 0
GeometrySkipInvalid = 1
class InvalidGeometryCheck

Bases: int

NoFlags = 0
NoGeometry = 1
class OrderBy

Bases: sip.wrapper

Create a new empty order by

QgsFeatureRequest.OrderBy(other: object) Create a new order by from a list of clauses

QgsFeatureRequest.OrderBy(QgsFeatureRequest.OrderBy)

Represents a list of OrderByClauses, with the most important first and the least important last.

New in version 2.14.

dump(self) → str

Dumps the content to an SQL equivalent syntax

list(self) → List[QgsFeatureRequest.OrderByClause]

Get a copy as a list of OrderByClauses

This is only required in Python where the inheritance is not properly propagated and this makes it usable.

load(self, elem: QDomElement)

Deserialize from XML

save(self, elem: QDomElement)

Serialize to XML

usedAttributes(self) → Set[str]

Returns a set of used attributes

class OrderByClause(expression: str, ascending: bool = True)

Bases: sip.wrapper

Creates a new OrderByClause for a QgsFeatureRequest

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.OrderByClause(expression: str, ascending: bool, nullsfirst: bool) Creates a new OrderByClause for a QgsFeatureRequest

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.OrderByClause(expression: QgsExpression, ascending: bool = True) Creates a new OrderByClause for a QgsFeatureRequest

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.OrderByClause(expression: QgsExpression, ascending: bool, nullsfirst: bool) Creates a new OrderByClause for a QgsFeatureRequest

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.OrderByClause(QgsFeatureRequest.OrderByClause)

The OrderByClause class represents an order by clause for a QgsFeatureRequest.

It can be a simple field or an expression. Multiple order by clauses can be added to a QgsFeatureRequest to fine tune the behavior if a single field or expression is not enough to completely specify the required behavior.

If expression compilation is activated in the settings and the expression can be translated for the provider in question, it will be evaluated on provider side. If one of these two premises does not apply, the ordering will take place locally which results in increased memory and CPU usage.

If the ordering is done on strings, the order depends on the system’s locale if the local fallback implementation is used. The order depends on the server system’s locale and implementation if ordering is done on the server.

In case the fallback code needs to be used, a limit set on the request will be respected for the features returned by the iterator but internally all features will be requested from the provider.

New in version 2.14.

ascending(self) → bool

Order ascending

Returns:If ascending order is requested
dump(self) → str

Dumps the content to an SQL equivalent

expression(self) → QgsExpression

The expression

Returns:the expression
nullsFirst(self) → bool

Set if NULLS should be returned first

Returns:if NULLS should be returned first
prepare(self, context: QgsExpressionContext) → bool

Prepare the expression with the given context.

New in version 3.0.

setAscending(self, ascending: bool)

Set if ascending order is requested

setNullsFirst(self, nullsFirst: bool)

Set if NULLS should be returned first

SubsetOfAttributes = 2
acceptFeature(self, feature: QgsFeature) → bool

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

New in version 2.1.

addOrderBy(self, expression: str, ascending: bool = True) → QgsFeatureRequest

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

New in version 2.14.

addOrderBy(self, expression: str, ascending: bool, nullsfirst: bool) -> QgsFeatureRequest 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

New in version 2.14.

combineFilterExpression(self, expression: str) → QgsFeatureRequest

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.

New in version 2.14.

connectionTimeout(self) → int

The timeout 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.

Note

Only works if the provider supports this option.

New in version 3.0.

destinationCrs(self) → 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.

New in version 3.0.

disableFilter(self) → QgsFeatureRequest

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

Returns:The object the method is called on for chaining

New in version 2.12.

expressionContext(self) → QgsExpressionContext

Returns the expression context used to evaluate filter expressions.

New in version 2.12.

filterExpression(self) → QgsExpression

Returns the filter expression if set.

filterFid(self) → int

Get the feature ID that should be fetched.

filterFids(self) → object

Get feature IDs that should be fetched.

filterRect(self) → QgsRectangle

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.

See also

setFilterRect()

filterType(self) → QgsFeatureRequest.FilterType

Return the filter type which is currently set on this request

Returns:Filter type
flags(self) → QgsFeatureRequest.Flags
invalidGeometryCheck(self) → QgsFeatureRequest.InvalidGeometryCheck

Returns the invalid geometry checking behavior.

New in version 3.0.

limit(self) → int

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

See also

setLimit()

New in version 2.14.

orderBy(self) → QgsFeatureRequest.OrderBy

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

New in version 2.14.

setConnectionTimeout(self, connectionTimeout: int)

The timeout 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.

Note

Only works if the provider supports this option.

New in version 3.0.

setDestinationCrs(self, crs: QgsCoordinateReferenceSystem, context: QgsCoordinateTransformContext) → QgsFeatureRequest

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.

See also

destinationCrs()

New in version 3.0.

setExpressionContext(self, context: QgsExpressionContext) → QgsFeatureRequest

Sets the expression context used to evaluate filter expressions.

New in version 2.12.

setFilterExpression(self, expression: str) → QgsFeatureRequest

Set the filter expression. {QgsExpression}

Parameters:expression – expression string
setFilterFid(self, fid: int) → QgsFeatureRequest

Set feature ID that should be fetched.

setFilterFids(self, fids: object) → QgsFeatureRequest

Set feature IDs that should be fetched.

setFilterRect(self, rectangle: QgsRectangle) → QgsFeatureRequest

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.

See also

filterRect()

setFlags(self, flags: Union[QgsFeatureRequest.Flags, QgsFeatureRequest.Flag]) → QgsFeatureRequest

Set flags that affect how features will be fetched

setInvalidGeometryCallback(self, Callable[..., None]) → QgsFeatureRequest

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.

New in version 3.0.

See also

invalidGeometryCallback()

setInvalidGeometryCheck(self, check: QgsFeatureRequest.InvalidGeometryCheck) → QgsFeatureRequest

Sets invalid geometry checking behavior.

Note

Invalid geometry checking is not performed when retrieving features directly from a QgsVectorDataProvider.

New in version 3.0.

setLimit(self, limit: int) → QgsFeatureRequest

Set the maximum number of features to request.

Parameters:limit – maximum number of features, or -1 to request all features.

See also

limit()

New in version 2.14.

setOrderBy(self, orderBy: QgsFeatureRequest.OrderBy) → QgsFeatureRequest

Set a list of order by clauses.

New in version 2.14.

setSimplifyMethod(self, simplifyMethod: QgsSimplifyMethod) → QgsFeatureRequest

Set a simplification method for geometries that will be fetched

New in version 2.2.

setSubsetOfAttributes(self, attrs: Iterable[int]) → QgsFeatureRequest

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)

setSubsetOfAttributes(self, attrNames: Iterable[str], fields: QgsFields) -> QgsFeatureRequest Set a subset of attributes by names that will be fetched

setSubsetOfAttributes(self, attrNames: Iterable[str], fields: QgsFields) -> QgsFeatureRequest Set a subset of attributes by names that will be fetched

setTransformErrorCallback(self, Callable[..., None]) → QgsFeatureRequest

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.

New in version 3.0.

See also

transformErrorCallback()

simplifyMethod(self) → QgsSimplifyMethod

Get simplification method for geometries that will be fetched

New in version 2.2.

subsetOfAttributes(self) → List[int]

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

Returns:A list of attributes to be fetched
transformContext(self) → QgsCoordinateTransformContext

Returns the transform context, for use when a destinationCrs() has been set and reprojection is required

See also

destinationCrs()

New in version 3.0.