QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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"
28 #include "qgsexpressioncontext.h"
29 #include "qgssimplifymethod.h"
32 
33 
83 class CORE_EXPORT QgsFeatureRequest
84 {
85  public:
86  enum Flag
87  {
88  NoFlags = 0,
89  NoGeometry = 1,
90  SubsetOfAttributes = 2,
91  ExactIntersect = 4,
92  IgnoreStaticNodesDuringExpressionCompilation = 8,
93  EmbeddedSymbols = 16,
94  };
95  Q_DECLARE_FLAGS( Flags, Flag )
96 
97 
100  enum FilterType
101  {
102  FilterNone,
103  FilterFid,
104  FilterExpression,
105  FilterFids
106  };
107 
110  {
111  GeometryNoCheck = 0,
112  GeometrySkipInvalid = 1,
113  GeometryAbortOnInvalid = 2,
114  };
115 
139  class CORE_EXPORT OrderByClause
140  {
141  public:
142 
151  OrderByClause( const QString &expression, bool ascending = true );
152 
160  OrderByClause( const QString &expression, bool ascending, bool nullsfirst );
161 
170  OrderByClause( const QgsExpression &expression, bool ascending = true );
171 
179  OrderByClause( const QgsExpression &expression, bool ascending, bool nullsfirst );
180 
185  QgsExpression expression() const;
186 
194  bool prepare( QgsExpressionContext *context );
195 
200  bool ascending() const;
201 
205  void setAscending( bool ascending );
206 
211  bool nullsFirst() const;
212 
216  void setNullsFirst( bool nullsFirst );
217 
221  QString dump() const;
222 
223  // friend inline int qHash(const OrderByClause &a) { return qHash(a.mExpression.expression()) ^ qHash(a.mAscending) ^ qHash( a.mNullsFirst); }
224 
225  bool operator==( const OrderByClause &v ) const
226  {
227  return mExpression == v.mExpression &&
228  mAscending == v.mAscending &&
229  mNullsFirst == v.mNullsFirst;
230  }
231 
232  bool operator!=( const OrderByClause &v ) const
233  {
234  return !( v == *this );
235  }
236 
237  private:
238  QgsExpression mExpression;
239  bool mAscending;
240  bool mNullsFirst;
241  };
242 
243 
251  class OrderBy : public QList<QgsFeatureRequest::OrderByClause>
252  {
253  public:
254 
258  CORE_EXPORT OrderBy();
259 
263  CORE_EXPORT OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other );
264 
271  QList<QgsFeatureRequest::OrderByClause> CORE_EXPORT list() const;
272 
276  void CORE_EXPORT save( QDomElement &elem ) const;
277 
281  void CORE_EXPORT load( const QDomElement &elem );
282 
287  QSet<QString> CORE_EXPORT usedAttributes() const;
288 
293  QSet<int> CORE_EXPORT usedAttributeIndices( const QgsFields &fields ) const;
294 
298  QString CORE_EXPORT dump() const;
299  };
300 
304  static const QString ALL_ATTRIBUTES;
305 
309  explicit QgsFeatureRequest( QgsFeatureId fid );
311  explicit QgsFeatureRequest( const QgsFeatureIds &fids );
312 
320  explicit QgsFeatureRequest( const QgsRectangle &rectangle );
321 
323  explicit QgsFeatureRequest( const QgsExpression &expr, const QgsExpressionContext &context = QgsExpressionContext() );
327  QgsFeatureRequest &operator=( const QgsFeatureRequest &rh );
328 
330 
342  FilterType filterType() const { return mFilter; }
343 
356  Qgis::SpatialFilterType spatialFilterType() const { return mSpatialFilter; }
357 
370  QgsFeatureRequest &setFilterRect( const QgsRectangle &rectangle );
371 
387  QgsRectangle filterRect() const;
388 
404  QgsFeatureRequest &setDistanceWithin( const QgsGeometry &geometry, double distance );
405 
420  QgsGeometry referenceGeometry() const { return mReferenceGeometry; }
421 
430  std::shared_ptr< QgsGeometryEngine > referenceGeometryEngine() const SIP_SKIP { return mReferenceGeometryEngine; }
431 
443  double distanceWithin() const { return mDistanceWithin; }
444 
453  QgsFeatureRequest &setFilterFid( QgsFeatureId fid );
454 
461  QgsFeatureId filterFid() const { return mFilterFid; }
462 
471  QgsFeatureRequest &setFilterFids( const QgsFeatureIds &fids );
472 
479  const QgsFeatureIds &filterFids() const { return mFilterFids; }
480 
488  QgsFeatureRequest &setInvalidGeometryCheck( InvalidGeometryCheck check );
489 
495  InvalidGeometryCheck invalidGeometryCheck() const { return mInvalidGeometryFilter; }
496 
504 #ifndef SIP_RUN
505  QgsFeatureRequest &setInvalidGeometryCallback( const std::function< void( const QgsFeature & )> &callback );
506 #else
507  QgsFeatureRequest &setInvalidGeometryCallback( SIP_PYCALLABLE / AllowNone / );
508  % MethodCode
509  Py_BEGIN_ALLOW_THREADS
510 
511  sipCpp->setInvalidGeometryCallback( [a0]( const QgsFeature &arg )
512  {
513  SIP_BLOCK_THREADS
514  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
515  SIP_UNBLOCK_THREADS
516  } );
517 
518  sipRes = sipCpp;
519 
520  Py_END_ALLOW_THREADS
521  % End
522 #endif
523 
531  std::function< void( const QgsFeature & ) > invalidGeometryCallback() const { return mInvalidGeometryCallback; } SIP_SKIP
532 
542  QgsFeatureRequest &setFilterExpression( const QString &expression );
543 
549  QgsExpression *filterExpression() const { return mFilterExpression.get(); }
550 
560  QgsFeatureRequest &combineFilterExpression( const QString &expression );
561 
568  QgsExpressionContext *expressionContext() { return &mExpressionContext; }
569 
576  QgsFeatureRequest &setExpressionContext( const QgsExpressionContext &context );
577 
589  QgsFeatureRequest &disableFilter() { mFilter = FilterNone; mFilterExpression.reset(); return *this; }
590 
602  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending = true );
603 
613  QgsFeatureRequest &addOrderBy( const QString &expression, bool ascending, bool nullsfirst );
614 
620  OrderBy orderBy() const;
621 
627  QgsFeatureRequest &setOrderBy( const OrderBy &orderBy );
628 
635  QgsFeatureRequest &setLimit( long long limit );
636 
642 #ifndef SIP_RUN
643  long long limit() const { return mLimit; }
644 #else
645  long long limit() const;
646 #endif
647 
653  QgsFeatureRequest &setFlags( QgsFeatureRequest::Flags flags );
654 
660  Flags flags() const { return mFlags; }
661 
678  QgsFeatureRequest &setSubsetOfAttributes( const QgsAttributeList &attrs );
679 
695  QgsFeatureRequest &setNoAttributes();
696 
710  QgsAttributeList subsetOfAttributes() const { return mAttrs; }
711 
723  QgsFeatureRequest &setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields );
724 
736  QgsFeatureRequest &setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields );
737 
744  QgsFeatureRequest &setSimplifyMethod( const QgsSimplifyMethod &simplifyMethod );
745 
752  const QgsSimplifyMethod &simplifyMethod() const { return mSimplifyMethod; }
753 
762  QgsCoordinateReferenceSystem destinationCrs() const;
763 
771  QgsCoordinateTransformContext transformContext() const;
772 
796  QgsFeatureRequest &setDestinationCrs( const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context );
797 
806 #ifndef SIP_RUN
807  QgsFeatureRequest &setTransformErrorCallback( const std::function< void( const QgsFeature & )> &callback );
808 #else
809  QgsFeatureRequest &setTransformErrorCallback( SIP_PYCALLABLE / AllowNone / );
810  % MethodCode
811  Py_BEGIN_ALLOW_THREADS
812 
813  sipCpp->setTransformErrorCallback( [a0]( const QgsFeature &arg )
814  {
815  SIP_BLOCK_THREADS
816  Py_XDECREF( sipCallMethod( NULL, a0, "D", &arg, sipType_QgsFeature, NULL ) );
817  SIP_UNBLOCK_THREADS
818  } );
819 
820  sipRes = sipCpp;
821 
822  Py_END_ALLOW_THREADS
823  % End
824 #endif
825 
834  std::function< void( const QgsFeature & ) > transformErrorCallback() const { return mTransformErrorCallback; } SIP_SKIP
835 
836 
846  bool acceptFeature( const QgsFeature &feature );
847 
857  Q_DECL_DEPRECATED int connectionTimeout() const SIP_DEPRECATED;
858 
868  Q_DECL_DEPRECATED QgsFeatureRequest &setConnectionTimeout( int connectionTimeout ) SIP_DEPRECATED;
869 
879  int timeout() const;
880 
890  QgsFeatureRequest &setTimeout( int timeout );
891 
905  bool requestMayBeNested() const;
906 
920  QgsFeatureRequest &setRequestMayBeNested( bool requestMayBeNested );
921 
933  void setFeedback( QgsFeedback *feedback );
934 
943  QgsFeedback *feedback() const;
944 
945  protected:
946 
950  FilterType mFilter = FilterNone;
951 
957  Qgis::SpatialFilterType mSpatialFilter = Qgis::SpatialFilterType::NoFilter;
958 
962  QgsRectangle mFilterRect;
963 
967  QgsGeometry mReferenceGeometry;
968 
972  std::shared_ptr< QgsGeometryEngine > mReferenceGeometryEngine;
973 
977  double mDistanceWithin = 0;
978 
979  QgsFeatureId mFilterFid = -1;
980  QgsFeatureIds mFilterFids;
981  std::unique_ptr< QgsExpression > mFilterExpression;
982  QgsExpressionContext mExpressionContext;
983  Flags mFlags = Flags();
984  QgsAttributeList mAttrs;
985  QgsSimplifyMethod mSimplifyMethod;
986  long long mLimit = -1;
987  OrderBy mOrderBy;
988  InvalidGeometryCheck mInvalidGeometryFilter = GeometryNoCheck;
989  std::function< void( const QgsFeature & ) > mInvalidGeometryCallback;
990  std::function< void( const QgsFeature & ) > mTransformErrorCallback;
992  QgsCoordinateTransformContext mTransformContext;
993  int mTimeout = -1;
994  int mRequestMayBeNested = false;
995  QgsFeedback *mFeedback = nullptr;
996 };
997 
999 
1009 class CORE_EXPORT QgsAbstractFeatureSource
1010 {
1011  public:
1012  virtual ~QgsAbstractFeatureSource();
1013 
1014 
1015  // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
1016  // required for Python data providers to work correctly! Argh!
1017 
1023  virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest &request = QgsFeatureRequest() ) = 0 SIP_TRANSFERBACK;
1024 
1025  // IMPORTANT -- do NOT remove the /TransferBack/ annotation here -- while it looks completely wrong, it's
1026  // required for Python data providers to work correctly! Argh!
1027 
1028  protected:
1029  void iteratorOpened( QgsAbstractFeatureIterator *it );
1030  void iteratorClosed( QgsAbstractFeatureIterator *it );
1031 
1032  QSet< QgsAbstractFeatureIterator * > mActiveIterators;
1033 
1034  template<typename> friend class QgsAbstractFeatureIteratorFromSource;
1035 };
1036 
1037 #endif // QGSFEATUREREQUEST_H
QgsFeatureRequest::Flag
Flag
Definition: qgsfeaturerequest.h:99
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsFeatureRequest::OrderByClause
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
Definition: qgsfeaturerequest.h:152
QgsCoordinateTransformContext
Contains information about the context in which a coordinate transform is executed.
Definition: qgscoordinatetransformcontext.h:57
QgsSimplifyMethod
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
Definition: qgssimplifymethod.h:28
qgsrectangle.h
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:425
qgsexpression.h
qgscoordinatetransformcontext.h
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsFields
Container of fields for a vector layer.
Definition: qgsfields.h:44
qgsfeature.h
QgsAbstractFeatureSource::mActiveIterators
QSet< QgsAbstractFeatureIterator * > mActiveIterators
Definition: qgsfeaturerequest.h:1032
qgis.h
QgsFeatureRequest::InvalidGeometryCheck
InvalidGeometryCheck
Handling of features with invalid geometries.
Definition: qgsfeaturerequest.h:122
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:430
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
SIP_TRANSFERBACK
#define SIP_TRANSFERBACK
Definition: qgis_sip.h:48
QgsAbstractFeatureIteratorFromSource
Helper template that cares of two things: 1. automatic deletion of source if owned by iterator,...
Definition: qgsfeatureiterator.h:259
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_DEPRECATED
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
QgsFeatureRequest
This class wraps a request for features to a vector layer (or directly its vector data provider).
Definition: qgsfeaturerequest.h:83
qgsexpressioncontext.h
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsFeedback
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition: qgsfeedback.h:44
qgis_sip.h
QgsAbstractFeatureSource
Base class that can be used for any class that is capable of returning features.
Definition: qgsfeaturerequest.h:1009
Q_DECLARE_OPERATORS_FOR_FLAGS
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
QgsFeatureIds
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeatureid.h:37
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsFeatureRequest::FilterType
FilterType
Types of filters.
Definition: qgsfeaturerequest.h:113
QgsFeatureRequest::OrderBy
Represents a list of OrderByClauses, with the most important first and the least important last.
Definition: qgsfeaturerequest.h:264
QgsGeometry
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
Qgis
The Qgis class provides global constants for use throughout the application.
Definition: qgis.h:71
Qgis::SpatialFilterType
SpatialFilterType
Feature request spatial filter types.
Definition: qgis.h:992
QgsFeature
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:55
QgsExpression
Class for parsing and evaluation of expressions (formerly called "search strings")....
Definition: qgsexpression.h:102
QgsFeatureIterator
Wrapper for iterator of features from vector data provider or vector layer.
Definition: qgsfeatureiterator.h:289
QgsGeometryEngine
A geometry engine is a low-level representation of a QgsAbstractGeometry object, optimised for use wi...
Definition: qgsgeometryengine.h:68
qgscoordinatereferencesystem.h
QgsAbstractFeatureIterator
Internal feature iterator to be implemented within data providers.
Definition: qgsfeatureiterator.h:28
qgssimplifymethod.h
QgsFeatureId
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28