QGIS API Documentation  3.3.0-Master (1192f94)
qgsfeaturerequest.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeaturerequest.cpp
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 #include "qgsfeaturerequest.h"
16 
17 #include "qgsfields.h"
18 #include "qgsgeometry.h"
19 
20 #include <QStringList>
21 
22 //constants
23 const QString QgsFeatureRequest::ALL_ATTRIBUTES = QStringLiteral( "#!allattributes!#" );
24 
26  : mFlags( nullptr )
27 {
28 }
29 
31  : mFilter( FilterFid )
32  , mFilterFid( fid )
33  , mFlags( nullptr )
34 {
35 }
36 
38  : mFilter( FilterFids )
39  , mFilterFids( fids )
40  , mFlags( nullptr )
41 {
42 
43 }
44 
46  : mFilterRect( rect )
47  , mFlags( nullptr )
48 {
49 }
50 
53  , mFilterExpression( new QgsExpression( expr ) )
54  , mExpressionContext( context )
55  , mFlags( nullptr )
56 {
57 }
58 
60 {
61  operator=( rh );
62 }
63 
65 {
66  mFlags = rh.mFlags;
67  mFilter = rh.mFilter;
71  if ( rh.mFilterExpression )
72  {
74  }
75  else
76  {
77  mFilterExpression.reset( nullptr );
78  }
82  mAttrs = rh.mAttrs;
84  mLimit = rh.mLimit;
85  mOrderBy = rh.mOrderBy;
86  mCrs = rh.mCrs;
88  mTimeout = rh.mTimeout;
90  return *this;
91 }
92 
94 {
95  mFilterRect = rect;
96  return *this;
97 }
98 
100 {
101  mFilter = FilterFid;
102  mFilterFid = fid;
103  return *this;
104 }
105 
107 {
109  mFilterFids = fids;
110  return *this;
111 }
112 
114 {
115  mInvalidGeometryFilter = check;
116  return *this;
117 }
118 
119 QgsFeatureRequest &QgsFeatureRequest::setInvalidGeometryCallback( const std::function<void ( const QgsFeature & )> &callback )
120 {
121  mInvalidGeometryCallback = callback;
122  return *this;
123 }
124 
126 {
128  mFilterExpression.reset( new QgsExpression( expression ) );
129  return *this;
130 }
131 
133 {
134  if ( mFilterExpression )
135  {
136  setFilterExpression( QStringLiteral( "(%1) AND (%2)" ).arg( mFilterExpression->expression(), expression ) );
137  }
138  else
139  {
140  setFilterExpression( expression );
141  }
142  return *this;
143 }
144 
146 {
147  mExpressionContext = context;
148  return *this;
149 }
150 
151 QgsFeatureRequest &QgsFeatureRequest::addOrderBy( const QString &expression, bool ascending )
152 {
153  mOrderBy.append( OrderByClause( expression, ascending ) );
154  return *this;
155 }
156 
157 QgsFeatureRequest &QgsFeatureRequest::addOrderBy( const QString &expression, bool ascending, bool nullsfirst )
158 {
159  mOrderBy.append( OrderByClause( expression, ascending, nullsfirst ) );
160  return *this;
161 }
162 
164 {
165  return mOrderBy;
166 }
167 
169 {
170  mOrderBy = orderBy;
171  return *this;
172 }
173 
175 {
176  mLimit = limit;
177  return *this;
178 }
179 
181 {
182  mFlags = flags;
183  return *this;
184 }
185 
187 {
189  mAttrs = attrs;
190  return *this;
191 }
192 
193 QgsFeatureRequest &QgsFeatureRequest::setSubsetOfAttributes( const QStringList &attrNames, const QgsFields &fields )
194 {
195  if ( attrNames.contains( QgsFeatureRequest::ALL_ATTRIBUTES ) )
196  {
197  //attribute string list contains the all attributes flag, so we must fetch all attributes
198  return *this;
199  }
200 
202  mAttrs.clear();
203 
204  Q_FOREACH ( const QString &attrName, attrNames )
205  {
206  int attrNum = fields.lookupField( attrName );
207  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
208  mAttrs.append( attrNum );
209  }
210 
211  return *this;
212 }
213 
214 QgsFeatureRequest &QgsFeatureRequest::setSubsetOfAttributes( const QSet<QString> &attrNames, const QgsFields &fields )
215 {
216  if ( attrNames.contains( QgsFeatureRequest::ALL_ATTRIBUTES ) )
217  {
218  //attribute string list contains the all attributes flag, so we must fetch all attributes
219  return *this;
220  }
221 
223  mAttrs.clear();
224 
225  Q_FOREACH ( const QString &attrName, attrNames )
226  {
227  int attrNum = fields.lookupField( attrName );
228  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
229  mAttrs.append( attrNum );
230  }
231 
232  return *this;
233 }
234 
236 {
238  return *this;
239 }
240 
241 
243 {
244  return mCrs;
245 }
246 
248 {
249  return mTransformContext;
250 }
251 
253 {
254  mCrs = crs;
255  mTransformContext = context;
256  return *this;
257 }
258 
259 QgsFeatureRequest &QgsFeatureRequest::setTransformErrorCallback( const std::function<void ( const QgsFeature & )> &callback )
260 {
261  mTransformErrorCallback = callback;
262  return *this;
263 }
264 
266 {
267  if ( !mFilterRect.isNull() )
268  {
269  if ( !feature.hasGeometry() || !feature.geometry().intersects( mFilterRect ) )
270  return false;
271  }
272 
273  switch ( mFilter )
274  {
276  return true;
277 
279  return ( feature.id() == mFilterFid );
280 
282  mExpressionContext.setFeature( feature );
283  return ( mFilterExpression->evaluate( &mExpressionContext ).toBool() );
284 
286  return ( mFilterFids.contains( feature.id() ) );
287  }
288 
289  return true;
290 }
291 
293 {
294  return mTimeout;
295 }
296 
298 {
300  return *this;
301 }
302 
304 {
305  return mTimeout;
306 }
307 
309 {
310  mTimeout = timeout;
311  return *this;
312 }
313 
315 {
316  return mRequestMayBeNested;
317 }
318 
320 {
322  return *this;
323 }
324 
325 
326 #include "qgsfeatureiterator.h"
327 #include "qgslogger.h"
328 
330 {
331  while ( !mActiveIterators.empty() )
332  {
333  QgsAbstractFeatureIterator *it = *mActiveIterators.begin();
334  QgsDebugMsg( "closing active iterator" );
335  it->close();
336  }
337 }
338 
340 {
341  mActiveIterators.insert( it );
342 }
343 
345 {
346  mActiveIterators.remove( it );
347 }
348 
349 
350 
351 QgsFeatureRequest::OrderByClause::OrderByClause( const QString &expression, bool ascending )
352  : mExpression( expression )
353  , mAscending( ascending )
354 {
355  // postgres behavior: default for ASC: NULLS LAST, default for DESC: NULLS FIRST
356  mNullsFirst = !ascending;
357 }
358 
360  : mExpression( expression )
361  , mAscending( ascending )
362  , mNullsFirst( nullsfirst )
363 {
364 }
365 
367  : mExpression( expression )
368  , mAscending( ascending )
369 {
370  // postgres behavior: default for ASC: NULLS LAST, default for DESC: NULLS FIRST
371  mNullsFirst = !ascending;
372 }
373 
375  : mExpression( expression )
376  , mAscending( ascending )
377  , mNullsFirst( nullsfirst )
378 {
379 
380 }
381 
383 {
384  return mAscending;
385 }
386 
388 {
389  mAscending = ascending;
390 }
391 
393 {
394  return mNullsFirst;
395 }
396 
398 {
399  mNullsFirst = nullsFirst;
400 }
401 
403 {
404  return QStringLiteral( "%1 %2 %3" )
405  .arg( mExpression.expression(),
406  mAscending ? "ASC" : "DESC",
407  mNullsFirst ? "NULLS FIRST" : "NULLS LAST" );
408 }
409 
411 {
412  return mExpression;
413 }
414 
416 {
417  return mExpression.prepare( context );
418 }
419 
421 
422 QgsFeatureRequest::OrderBy::OrderBy( const QList<QgsFeatureRequest::OrderByClause> &other )
423 {
424  Q_FOREACH ( const QgsFeatureRequest::OrderByClause &clause, other )
425  {
426  append( clause );
427  }
428 }
429 
430 QList<QgsFeatureRequest::OrderByClause> QgsFeatureRequest::OrderBy::list() const
431 {
432  return *this;
433 }
434 
435 void QgsFeatureRequest::OrderBy::save( QDomElement &elem ) const
436 {
437  QDomDocument doc = elem.ownerDocument();
438  QList<OrderByClause>::ConstIterator it;
439  for ( it = constBegin(); it != constEnd(); ++it )
440  {
441  const OrderByClause &clause = *it;
442  QDomElement clauseElem = doc.createElement( QStringLiteral( "orderByClause" ) );
443  clauseElem.setAttribute( QStringLiteral( "asc" ), clause.ascending() );
444  clauseElem.setAttribute( QStringLiteral( "nullsFirst" ), clause.nullsFirst() );
445  clauseElem.appendChild( doc.createTextNode( clause.expression().expression() ) );
446 
447  elem.appendChild( clauseElem );
448  }
449 }
450 
451 void QgsFeatureRequest::OrderBy::load( const QDomElement &elem )
452 {
453  clear();
454 
455  QDomNodeList clauses = elem.childNodes();
456 
457  for ( int i = 0; i < clauses.size(); ++i )
458  {
459  QDomElement clauseElem = clauses.at( i ).toElement();
460  QString expression = clauseElem.text();
461  bool asc = clauseElem.attribute( QStringLiteral( "asc" ) ).toInt() != 0;
462  bool nullsFirst = clauseElem.attribute( QStringLiteral( "nullsFirst" ) ).toInt() != 0;
463 
464  append( OrderByClause( expression, asc, nullsFirst ) );
465  }
466 }
467 
469 {
470  QSet<QString> usedAttributes;
471 
472  QList<OrderByClause>::ConstIterator it;
473  for ( it = constBegin(); it != constEnd(); ++it )
474  {
475  const OrderByClause &clause = *it;
476 
477  usedAttributes.unite( clause.expression().referencedColumns() );
478  }
479 
480  return usedAttributes;
481 }
482 
484 {
485  QStringList results;
486 
487  QList<OrderByClause>::ConstIterator it;
488  for ( it = constBegin(); it != constEnd(); ++it )
489  {
490  const OrderByClause &clause = *it;
491 
492  results << clause.dump();
493  }
494 
495  return results.join( QStringLiteral( ", " ) );
496 }
int lookupField(const QString &fieldName) const
Look up field&#39;s index from the field name.
Definition: qgsfields.cpp:299
Class for parsing and evaluation of expressions (formerly called "search strings").
QgsFeatureRequest & setDestinationCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets the destination crs for feature&#39;s geometries.
QgsFeatureId id
Definition: qgsfeature.h:71
QgsCoordinateReferenceSystem mCrs
QgsFeatureIds mFilterFids
virtual bool close()=0
end of iterating: free the resources / lock
A rectangle specified with double values.
Definition: qgsrectangle.h:40
long limit() const
Returns the maximum number of features to request, or -1 if no limit set.
bool nullsFirst() const
Set if NULLS should be returned first.
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
QSet< QString > CORE_EXPORT usedAttributes() const
Returns a set of used attributes.
Filter using feature ID.
Filter using feature IDs.
QgsSimplifyMethod mSimplifyMethod
QgsFeatureRequest & setInvalidGeometryCallback(const std::function< void(const QgsFeature &)> &callback)
Sets a callback function to use when encountering an invalid geometry and invalidGeometryCheck() is s...
bool ascending() const
Order ascending.
const Flags & flags() const
QString dump() const
Dumps the content to an SQL equivalent.
void iteratorClosed(QgsAbstractFeatureIterator *it)
QgsFeatureRequest & setInvalidGeometryCheck(InvalidGeometryCheck check)
Sets invalid geometry checking behavior.
InvalidGeometryCheck mInvalidGeometryFilter
QList< QgsFeatureRequest::OrderByClause > CORE_EXPORT list() const
Gets a copy as a list of OrderByClauses.
QString CORE_EXPORT dump() const
Dumps the content to an SQL equivalent syntax.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
QgsCoordinateReferenceSystem destinationCrs() const
Returns the destination coordinate reference system for feature&#39;s geometries, or an invalid QgsCoordi...
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
Definition: qgsfields.h:42
void CORE_EXPORT load(const QDomElement &elem)
Deserialize from XML.
QgsFeatureRequest & addOrderBy(const QString &expression, bool ascending=true)
Adds a new OrderByClause, appending it as the least important one.
Q_DECL_DEPRECATED QgsFeatureRequest & setConnectionTimeout(int connectionTimeout)
Sets the timeout (in milliseconds) for how long we should wait for a connection if none is available ...
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
const QgsCoordinateReferenceSystem & crs
QgsFeatureRequest & operator=(const QgsFeatureRequest &rh)
Assignment operator.
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:190
int timeout() const
Returns the timeout (in milliseconds) for the maximum time we should wait during feature requests bef...
QgsExpressionContext mExpressionContext
InvalidGeometryCheck
Handling of features with invalid geometries.
bool intersects(const QgsRectangle &rectangle) const
Returns true if this geometry exactly intersects with a rectangle.
QgsFeatureRequest & combineFilterExpression(const QString &expression)
Modifies the existing filter expression to add an additional expression filter.
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Sets feature ID that should be fetched.
CORE_EXPORT OrderBy()
Create a new empty order by.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
OrderByClause(const QString &expression, bool ascending=true)
Creates a new OrderByClause for a QgsFeatureRequest.
std::unique_ptr< QgsExpression > mFilterExpression
Internal feature iterator to be implemented within data providers.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
bool requestMayBeNested() const
In case this request may be run nested within another already running iteration on the same connectio...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
QgsFeatureRequest()
construct a default request: for all features get attributes and geometries
QgsFeatureRequest & setFilterRect(const QgsRectangle &rectangle)
Sets the rectangle from which features will be taken.
static const QString ALL_ATTRIBUTES
A special attribute that if set matches all attributes.
QgsRectangle mFilterRect
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
Contains information about the context in which a coordinate transform is executed.
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:101
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 des...
QString expression() const
Returns the original, unmodified expression string.
QgsFeatureRequest & setSimplifyMethod(const QgsSimplifyMethod &simplifyMethod)
Set a simplification method for geometries that will be fetched.
bool prepare(QgsExpressionContext *context)
Prepare the expression with the given context.
The OrderByClause class represents an order by clause for a QgsFeatureRequest.
QgsFeatureId mFilterFid
QgsExpression expression() const
The expression.
No filter is applied.
std::function< void(const QgsFeature &) > mInvalidGeometryCallback
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Sets feature IDs that should be fetched.
QgsCoordinateTransformContext mTransformContext
std::function< void(const QgsFeature &) > mTransformErrorCallback
bool prepare(const QgsExpressionContext *context)
Gets the expression ready for evaluation - find out column indexes.
void setAscending(bool ascending)
Set if ascending order is requested.
QgsFeatureRequest & setTimeout(int timeout)
Sets the timeout (in milliseconds) for the maximum time we should wait during feature requests before...
QgsAttributeList mAttrs
This class represents a coordinate reference system (CRS).
void CORE_EXPORT save(QDomElement &elem) const
Serialize to XML.
bool isNull() const
Test if the rectangle is null (all coordinates zero or after call to setMinimal()).
Definition: qgsrectangle.h:429
QgsFeatureRequest & setLimit(long limit)
Set the maximum number of features to request.
qint64 QgsFeatureId
Definition: qgsfeature.h:37
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
QgsFeatureRequest & setOrderBy(const OrderBy &orderBy)
Set a list of order by clauses.
QgsFeatureRequest & setRequestMayBeNested(bool requestMayBeNested)
In case this request may be run nested within another already running iteration on the same connectio...
QList< int > QgsAttributeList
Definition: qgsfield.h:27
Q_DECL_DEPRECATED int connectionTimeout() const
Returns the timeout (in milliseconds) for how long we should wait for a connection if none is availab...
OrderBy orderBy() const
Returns a list of order by clauses specified for this feature request.
QgsCoordinateTransformContext transformContext() const
Returns the transform context, for use when a destinationCrs() has been set and reprojection is requi...
const QgsSimplifyMethod & simplifyMethod() const
Gets simplification method for geometries that will be fetched.
Represents a list of OrderByClauses, with the most important first and the least important last...
QgsFeatureRequest & setFlags(QgsFeatureRequest::Flags flags)
Sets flags that affect how features will be fetched.
void iteratorOpened(QgsAbstractFeatureIterator *it)
void setNullsFirst(bool nullsFirst)
Set if NULLS should be returned first.