QGIS API Documentation  2.11.0-Master
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  mFilterRect = rect;
93  return *this;
94 }
95 
97 {
99  mFilterFid = fid;
100  return *this;
101 }
102 
104 {
106  mFilterFids = fids;
107  return *this;
108 }
109 
111 {
113  delete mFilterExpression;
114  mFilterExpression = new QgsExpression( expression );
115  return *this;
116 }
117 
119 {
120  mExpressionContext = context;
121  return *this;
122 }
123 
124 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
125 {
126  mFlags = flags;
127  return *this;
128 }
129 
131 {
133  mAttrs = attrs;
134  return *this;
135 }
136 
138 {
139  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
140  {
141  //attribute string list contains the all attributes flag, so we must fetch all attributes
142  return *this;
143  }
144 
146  mAttrs.clear();
147 
148  foreach ( const QString& attrName, attrNames )
149  {
150  int attrNum = fields.fieldNameIndex( attrName );
151  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
152  mAttrs.append( attrNum );
153  }
154 
155  return *this;
156 }
157 
159 {
161  return *this;
162 }
163 
165 {
166  switch ( mFilter )
167  {
169  return true;
170  break;
171 
173  if ( feature.constGeometry() && feature.constGeometry()->intersects( mFilterRect ) )
174  return true;
175  else
176  return false;
177  break;
178 
180  if ( feature.id() == mFilterFid )
181  return true;
182  else
183  return false;
184  break;
185 
187  mExpressionContext.setFeature( feature );
189  return true;
190  else
191  return false;
192  break;
193 
195  if ( mFilterFids.contains( feature.id() ) )
196  return true;
197  else
198  return false;
199  break;
200  }
201 
202  return true;
203 }
204 
205 #include "qgsfeatureiterator.h"
206 #include "qgslogger.h"
207 
209 {
210  while ( !mActiveIterators.empty() )
211  {
213  QgsDebugMsg( "closing active iterator" );
214  it->close();
215  }
216 }
217 
219 {
220  mActiveIterators.insert( it );
221 }
222 
224 {
225  mActiveIterators.remove( it );
226 }
227 
228 
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:51
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:88
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 - case insensitive TODO: sort out case sensitive (indexFromName()) vs...
Definition: qgsfield.cpp:356
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:162
QgsFeatureRequest & operator=(const QgsFeatureRequest &rh)
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)
QgsFeatureRequest & setFilterFids(QgsFeatureIds fids)
Set feature ID that should be fetched.
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
Obsolete, will be ignored. If a filterRect is set it will be used anyway. Filter using a rectangle...
QgsRectangle mFilterRect
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.
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:68
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 & setFlags(Flags flags)
Set flags that affect how features will be fetched.
QgsFeatureRequest & setFilterRect(const QgsRectangle &rect)
Set rectangle from which features will be taken.
void iteratorOpened(QgsAbstractFeatureIterator *it)