QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsfeaturerequest.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfeaturerequest.h
3 ---------------------
4 begin : Mai 2012
5 copyright : (C) 2012 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15#ifndef QGSFEATUREREQUEST_H
16#define QGSFEATUREREQUEST_H
17
18#include "qgis_core.h"
19#include "qgis_sip.h"
20#include <QFlags>
21#include <QList>
22#include <memory>
23
24#include "qgis.h"
25#include "qgsfeature.h"
26#include "qgsrectangle.h"
27#include "qgsexpression.h"
29#include "qgssimplifymethod.h"
33
83class CORE_EXPORT QgsFeatureRequest
84{
85 public:
86
109 class CORE_EXPORT OrderByClause
110 {
111 public:
112
121 OrderByClause( const QString &expression, bool ascending = true );
122
130 OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
131
140 OrderByClause( const QgsExpression &expression, bool ascending = true );
141
149 OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
150
155 QgsExpression expression() const;
156
163 bool prepare( QgsExpressionContext *context );
164
169 bool ascending() const;
170
174 void setAscending( bool ascending );
175
180 bool nullsFirst() const;
181
185 void setNullsFirst( bool nullsFirst );
186
190 QString dump() const;
191
192 // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
193
194 bool operator==( const OrderByClause &v ) const
195 {
196 return mExpression == v.mExpression &&
197 mAscending == v.mAscending &&
198 mNullsFirst == v.mNullsFirst;
199 }
200
201 bool operator!=( const OrderByClause &v ) const
202 {
203 return !( v == *this );
204 }
205
206 private:
207 QgsExpression mExpression;
208 bool mAscending;
209 bool mNullsFirst;
210 };
211
212
219 class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
220 {
221 public:
222
226 CORE_EXPORT OrderBy();
227
231 CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
232
237 CORE_EXPORT bool operator==( const OrderBy &v ) const;
238
243 CORE_EXPORT bool operator!=( const OrderBy &v ) const;
244
251 QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
252
256 void CORE_EXPORT save( QDomElement &elem ) const;
257
261 void CORE_EXPORT load( const QDomElement &elem );
262
267 QSet<QString> CORE_EXPORT usedAttributes() const;
268
273 QSet<int> CORE_EXPORT usedAttributeIndices( const QgsFields &fields ) const;
274
278 QString CORE_EXPORT dump() const;
279 };
280
284 static const QString ALL_ATTRIBUTES;
285
289 explicit QgsFeatureRequest( QgsFeatureId fid );
291 explicit QgsFeatureRequest( const QgsFeatureIds &fids );
292
300 explicit QgsFeatureRequest( const QgsRectangle &rectangle );
301
303 explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
305 QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
306
313 bool compare( const QgsFeatureRequest &other ) const;
314
316
328 Qgis::FeatureRequestFilterType filterType() const { return mFilter; }
329
342 Qgis::SpatialFilterType spatialFilterType() const { return mSpatialFilter; }
343
356 QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
357
373 QgsRectangle filterRect() const;
374
390 QgsFeatureRequest &setDistanceWithin( const QgsGeometry &geometry, double distance );
391
406 QgsGeometry referenceGeometry() const { return mReferenceGeometry; }
407
416 std::shared_ptr< QgsGeometryEngine > referenceGeometryEngine() const SIP_SKIP { return mReferenceGeometryEngine; }
417
429 double distanceWithin() const { return mDistanceWithin; }
430
439 QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
440
447 QgsFeatureId filterFid() const { return mFilterFid; }
448
457 QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
458
465 const QgsFeatureIds &filterFids() const { return mFilterFids; }
466
473 QgsFeatureRequest &setInvalidGeometryCheck( Qgis::InvalidGeometryCheck check );
474
479 Qgis::InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
480
487#ifndef SIP_RUN
488 QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
489#else
490 QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
491 % MethodCode
492 Py_BEGIN_ALLOW_THREADS
493
494 sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
495 {
496 SIP_BLOCK_THREADS
497 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
498 SIP_UNBLOCK_THREADS
499 } );
500
501 sipRes = sipCpp;
502
503 Py_END_ALLOW_THREADS
504 % End
505#endif
506
513 std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
514
524 QgsFeatureRequest &setFilterExpression( const QString &expression );
525
531 QgsExpression *filterExpression() const { return mFilterExpression.get(); }
532
541 QgsFeatureRequest &combineFilterExpression( const QString &expression );
542
548 QgsExpressionContext *expressionContext() { return &mExpressionContext; }
549
555 QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
556
567 QgsFeatureRequest &disableFilter() { mFilter = Qgis::FeatureRequestFilterType::NoFilter; mFilterExpression.reset(); return *this; }
568
579 QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
580
589 QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
590
595 OrderBy orderBy() const;
596
601 QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
602
608 QgsFeatureRequest &setLimit( long long limit );
609
614#ifndef SIP_RUN
615 long long limit() const { return mLimit; }
616#else
617 long long limit() const;
618#endif
619
626
632 Qgis::FeatureRequestFlags flags() const { return mFlags; }
633
650 QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
651
667 QgsFeatureRequest &setNoAttributes();
668
682 QgsAttributeList subsetOfAttributes() const { return mAttrs; }
683
695 QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
696
708 QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
709
715 QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
716
722 const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
723
736 QgsCoordinateTransform coordinateTransform() const;
737
751 QgsCoordinateReferenceSystem destinationCrs() const;
752
759 QgsCoordinateTransformContext transformContext() const;
760
770 QgsCoordinateTransform calculateTransform( const QgsCoordinateReferenceSystem &sourceCrs ) const;
771
802 QgsFeatureRequest &setCoordinateTransform( const QgsCoordinateTransform &transform );
803
830 QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
831
839#ifndef SIP_RUN
840 QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
841#else
842 QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
843 % MethodCode
844 Py_BEGIN_ALLOW_THREADS
845
846 sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
847 {
848 SIP_BLOCK_THREADS
849 Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
850 SIP_UNBLOCK_THREADS
851 } );
852
853 sipRes = sipCpp;
854
855 Py_END_ALLOW_THREADS
856 % End
857#endif
858
866 std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
867
868
877 bool acceptFeature( const QgsFeature &feature );
878
887 Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
888
897 Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
898
908 int timeout() const;
909
919 QgsFeatureRequest &setTimeout( int timeout );
920
934 bool requestMayBeNested() const;
935
949 QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
950
962 void setFeedback( QgsFeedback *feedback );
963
972 QgsFeedback *feedback() const;
973
974 protected:
975
979 Qgis::FeatureRequestFilterType mFilter = Qgis::FeatureRequestFilterType::NoFilter;
980
986 Qgis::SpatialFilterType mSpatialFilter = Qgis::SpatialFilterType::NoFilter;
987
991 QgsRectangle mFilterRect;
992
996 QgsGeometry mReferenceGeometry;
997
1001 std::shared_ptr< QgsGeometryEngine > mReferenceGeometryEngine;
1002
1006 double mDistanceWithin = 0;
1007
1008 QgsFeatureId mFilterFid = -1;
1009 QgsFeatureIds mFilterFids;
1010 std::unique_ptr< QgsExpression > mFilterExpression;
1011 QgsExpressionContext mExpressionContext;
1012 Qgis::FeatureRequestFlags mFlags;
1014 QgsSimplifyMethod mSimplifyMethod;
1015 long long mLimit = -1;
1016 OrderBy mOrderBy;
1017 Qgis::InvalidGeometryCheck mInvalidGeometryFilter = Qgis::InvalidGeometryCheck::NoCheck;
1018 std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
1019 std::function< void( const QgsFeature & ) > mTransformErrorCallback;
1023 int mTimeout = -1;
1024 int mRequestMayBeNested = false;
1025 QgsFeedback *mFeedback = nullptr;
1026};
1027
1028
1029class QgsFeatureIterator;
1031
1037{
1038 public:
1039 virtual ~QgsAbstractFeatureSource();
1040
1041
1042 // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
1043 // required for Python data providers to work correctly! Argh!
1044
1051
1052 // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
1053 // required for Python data providers to work correctly! Argh!
1054
1055 protected:
1056 void iteratorOpened( QgsAbstractFeatureIterator *it );
1057 void iteratorClosed( QgsAbstractFeatureIterator *it );
1058
1059 QSet< QgsAbstractFeatureIterator * > mActiveIterators;
1060
1061 template<typename> friend class QgsAbstractFeatureIteratorFromSource;
1062};
1063
1064#endif // QGSFEATUREREQUEST_H
The Qgis class provides global constants for use throughout the application.
Definition qgis.h:54
FeatureRequestFilterType
Types of feature request filters.
Definition qgis.h:2056
@ NoFilter
No filter is applied.
SpatialFilterType
Feature request spatial filter types.
Definition qgis.h:2085
QFlags< FeatureRequestFlag > FeatureRequestFlags
Flags for controlling feature requests.
Definition qgis.h:2045
InvalidGeometryCheck
Methods for handling of features with invalid geometries.
Definition qgis.h:2072
Helper template that cares of two things: 1.
Internal feature iterator to be implemented within data providers.
Base class that can be used for any class that is capable of returning features.
QSet< QgsAbstractFeatureIterator * > mActiveIterators
virtual QgsFeatureIterator getFeatures(const QgsFeatureRequest &request=QgsFeatureRequest())=0
Gets an iterator for features matching the specified request.
This class represents a coordinate reference system (CRS).
Contains information about the context in which a coordinate transform is executed.
Class for doing transforms between two map coordinate systems.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
Wrapper for iterator of features from vector data provider or vector layer.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
bool operator==(const OrderByClause &v) const
bool operator!=(const OrderByClause &v) const
Represents a list of OrderByClauses, with the most important first and the least important last.
CORE_EXPORT OrderBy()
Create a new empty order by.
This class wraps a request for features to a vector layer (or directly its vector data provider).
long long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
std::function< void(const QgsFeature &) > transformErrorCallback() const
Returns the callback function to use when encountering a transform error when iterating features and ...
std::function< void(const QgsFeature &) > invalidGeometryCallback() const
Returns the callback function to use when encountering an invalid geometry and invalidGeometryCheck()...
Qgis::FeatureRequestFilterType filterType() const
Returns the attribute/ID filter type which is currently set on this request.
QgsGeometry referenceGeometry() const
Returns the reference geometry used for spatial filtering of features.
static const QString ALL_ATTRIBUTES
A special attribute that if set matches all attributes.
Qgis::FeatureRequestFlags flags() const
Returns the flags which affect how features are fetched.
QgsFeatureRequest & disableFilter()
Disables any attribute/ID filtering.
QgsExpression * filterExpression() const
Returns the filter expression (if set).
const QgsSimplifyMethod & simplifyMethod() const
Returns the simplification method for geometries that will be fetched.
std::shared_ptr< QgsGeometryEngine > referenceGeometryEngine() const
Returns the reference geometry engine used for spatial filtering of features.
Qgis::SpatialFilterType spatialFilterType() const
Returns the spatial filter type which is currently set on this request.
double distanceWithin() const
Returns the maximum distance from the referenceGeometry() of fetched features, if spatialFilterType()...
QgsAttributeList subsetOfAttributes() const
Returns the subset of attributes which at least need to be fetched.
Qgis::InvalidGeometryCheck invalidGeometryCheck() const
Returns the invalid geometry checking behavior.
const QgsFeatureIds & filterFids() const
Returns the feature IDs that should be fetched.
QgsFeatureId filterFid() const
Returns the feature ID that should be fetched.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
Container of fields for a vector layer.
Definition qgsfields.h:46
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
A geometry is the spatial representation of a feature.
A rectangle specified with double values.
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFERBACK
Definition qgis_sip.h:48
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition qgsfield.h:27
const QgsCoordinateReferenceSystem & crs