QGIS API Documentation  2.12.0-Lyon
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 "qgsfield.h"
18 #include "qgsgeometry.h"
19 
20 #include <QStringList>
21 
22 //constants
23 const QString QgsFeatureRequest::AllAttributes = QString( "#!allattributes!#" );
24 
26  : mFilter( FilterNone )
27  , mFilterFid( -1 )
28  , mFilterExpression( 0 )
29  , mFlags( 0 )
30 {
31 }
32 
34  : mFilter( FilterFid )
35  , mFilterFid( fid )
36  , mFilterExpression( 0 )
37  , mFlags( 0 )
38 {
39 }
40 
42  : mFilter( FilterRect )
43  , mFilterRect( rect )
44  , mFilterFid( -1 )
45  , mFilterExpression( 0 )
46  , mFlags( 0 )
47 {
48 }
49 
51  : mFilter( FilterExpression )
52  , mFilterFid( -1 )
53  , mFilterExpression( new QgsExpression( expr.expression() ) )
54  , mExpressionContext( context )
55  , mFlags( 0 )
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  {
78  }
80  mAttrs = rh.mAttrs;
82  return *this;
83 }
84 
86 {
87  delete mFilterExpression;
88 }
89 
91 {
92  if ( mFilter == FilterNone )
94  mFilterRect = rect;
95  return *this;
96 }
97 
99 {
100  mFilter = FilterFid;
101  mFilterFid = fid;
102  return *this;
103 }
104 
106 {
108  mFilterFids = fids;
109  return *this;
110 }
111 
113 {
115  delete mFilterExpression;
116  mFilterExpression = new QgsExpression( expression );
117  return *this;
118 }
119 
121 {
122  mExpressionContext = context;
123  return *this;
124 }
125 
126 QgsFeatureRequest& QgsFeatureRequest::setFlags( const QgsFeatureRequest::Flags& flags )
127 {
128  mFlags = flags;
129  return *this;
130 }
131 
133 {
135  mAttrs = attrs;
136  return *this;
137 }
138 
140 {
141  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
142  {
143  //attribute string list contains the all attributes flag, so we must fetch all attributes
144  return *this;
145  }
146 
148  mAttrs.clear();
149 
150  Q_FOREACH ( const QString& attrName, attrNames )
151  {
152  int attrNum = fields.fieldNameIndex( attrName );
153  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
154  mAttrs.append( attrNum );
155  }
156 
157  return *this;
158 }
159 
161 {
163  return *this;
164 }
165 
167 {
168  switch ( mFilter )
169  {
171  return true;
172  break;
173 
175  if ( feature.constGeometry() && feature.constGeometry()->intersects( mFilterRect ) )
176  return true;
177  else
178  return false;
179  break;
180 
182  if ( feature.id() == mFilterFid )
183  return true;
184  else
185  return false;
186  break;
187 
189  mExpressionContext.setFeature( feature );
191  return true;
192  else
193  return false;
194  break;
195 
197  if ( mFilterFids.contains( feature.id() ) )
198  return true;
199  else
200  return false;
201  break;
202  }
203 
204  return true;
205 }
206 
207 #include "qgsfeatureiterator.h"
208 #include "qgslogger.h"
209 
211 {
212  while ( !mActiveIterators.empty() )
213  {
215  QgsDebugMsg( "closing active iterator" );
216  it->close();
217  }
218 }
219 
221 {
222  mActiveIterators.insert( it );
223 }
224 
226 {
227  mActiveIterators.remove( it );
228 }
229 
230 
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:92
void clear()
QgsFeatureIds mFilterFids
const QgsSimplifyMethod & simplifyMethod() const
Get simplification method for geometries that will be fetched.
virtual bool close()=0
end of iterating: free the resources / lock
A rectangle specified with double values.
Definition: qgsrectangle.h:35
bool acceptFeature(const QgsFeature &feature)
Check if a feature is accepted by this requests filter.
Filter using feature ID.
const Flags & flags() const
Filter using feature IDs.
QgsSimplifyMethod mSimplifyMethod
QgsExpression * mFilterExpression
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
Evaluate the feature and return the result.
void iteratorClosed(QgsAbstractFeatureIterator *it)
const QString expression() const
Alias for dump()
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
#define QgsDebugMsg(str)
Definition: qgslogger.h:33
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
Definition: qgsfield.cpp:382
bool contains(const QString &str, Qt::CaseSensitivity cs) const
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
Definition: qgsfield.h:177
const_iterator insert(const T &value)
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
QgsFeatureRequest & operator=(const QgsFeatureRequest &rh)
Assignment operator.
QgsExpressionContext mExpressionContext
QSet< QgsAbstractFeatureIterator * > mActiveIterators
QgsFeatureRequest & setExpressionContext(const QgsExpressionContext &context)
Sets the expression context used to evaluate filter expressions.
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Set feature ID that should be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set the filter expression.
void append(const T &value)
Internal feature iterator to be implemented within data providers.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
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
static const QString AllAttributes
A special attribute that if set matches all attributes.
Obsolete, will be ignored. If a filterRect is set it will be used anyway. Filter using a rectangle...
QgsRectangle mFilterRect
QgsFeatureRequest & setFlags(const QgsFeatureRequest::Flags &flags)
Set flags that affect how features will be fetched.
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
iterator begin()
bool contains(const T &value) const
QgsFeatureRequest & setSimplifyMethod(const QgsSimplifyMethod &simplifyMethod)
Set a simplification method for geometries that will be fetched.
QgsFeatureId mFilterFid
bool contains(const T &value) const
bool empty() const
No filter is applied.
QgsFeatureRequest & setFilterFids(const QgsFeatureIds &fids)
Set feature IDs that should be fetched.
bool remove(const T &value)
QgsAttributeList mAttrs
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:70
bool toBool() const
qint64 QgsFeatureId
Definition: qgsfeature.h:31
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
bool intersects(const QgsRectangle &r) const
Test for intersection with a rectangle (uses GEOS)
QgsFeatureRequest & setFilterRect(const QgsRectangle &rect)
Set rectangle from which features will be taken.
void iteratorOpened(QgsAbstractFeatureIterator *it)