QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  , mFlags( 0 )
55 {
56 }
57 
59 {
60  operator=( rh );
61 }
62 
64 {
65  mFlags = rh.mFlags;
66  mFilter = rh.mFilter;
70  if ( rh.mFilterExpression )
71  {
73  }
74  else
75  {
77  }
78  mAttrs = rh.mAttrs;
80  return *this;
81 }
82 
84 {
85  delete mFilterExpression;
86 }
87 
89 {
90  mFilterRect = rect;
91  return *this;
92 }
93 
95 {
97  mFilterFid = fid;
98  return *this;
99 }
100 
102 {
104  mFilterFids = fids;
105  return *this;
106 }
107 
109 {
111  delete mFilterExpression;
112  mFilterExpression = new QgsExpression( expression );
113  return *this;
114 }
115 
116 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
117 {
118  mFlags = flags;
119  return *this;
120 }
121 
123 {
125  mAttrs = attrs;
126  return *this;
127 }
128 
130 {
131  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
132  {
133  //attribute string list contains the all attributes flag, so we must fetch all attributes
134  return *this;
135  }
136 
138  mAttrs.clear();
139 
140  foreach ( const QString& attrName, attrNames )
141  {
142  int attrNum = fields.fieldNameIndex( attrName );
143  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
144  mAttrs.append( attrNum );
145  }
146 
147  return *this;
148 }
149 
151 {
153  return *this;
154 }
155 
157 {
158  switch ( mFilter )
159  {
161  return true;
162  break;
163 
165  if ( feature.constGeometry() && feature.constGeometry()->intersects( mFilterRect ) )
166  return true;
167  else
168  return false;
169  break;
170 
172  if ( feature.id() == mFilterFid )
173  return true;
174  else
175  return false;
176  break;
177 
179  if ( mFilterExpression->evaluate( feature ).toBool() )
180  return true;
181  else
182  return false;
183  break;
184 
186  if ( mFilterFids.contains( feature.id() ) )
187  return true;
188  else
189  return false;
190  break;
191  }
192 
193  return true;
194 }
195 
196 #include "qgsfeatureiterator.h"
197 #include "qgslogger.h"
198 
200 {
201  while ( !mActiveIterators.empty() )
202  {
204  QgsDebugMsg( "closing active iterator" );
205  it->close();
206  }
207 }
208 
210 {
211  mActiveIterators.insert( it );
212 }
213 
215 {
216  mActiveIterators.remove( it );
217 }
218 
219 
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:86
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
void iteratorClosed(QgsAbstractFeatureIterator *it)
const QString expression() const
Alias for dump()
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
#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:354
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:173
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)
QSet< QgsAbstractFeatureIterator * > mActiveIterators
QgsFeatureRequest & setFilterFid(QgsFeatureId fid)
Set feature ID that should be fetched.
QgsFeatureRequest & setFilterExpression(const QString &expression)
Set 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.
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)