QGIS API Documentation  2.9.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 {
91  mFilterRect = rect;
92  return *this;
93 }
94 
96 {
98  mFilterFid = fid;
99  return *this;
100 }
101 
103 {
105  mFilterFids = fids;
106  return *this;
107 }
108 
110 {
112  delete mFilterExpression;
113  mFilterExpression = new QgsExpression( expression );
114  return *this;
115 }
116 
117 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
118 {
119  mFlags = flags;
120  return *this;
121 }
122 
124 {
126  mAttrs = attrs;
127  return *this;
128 }
129 
130 QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields )
131 {
132  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
133  {
134  //attribute string list contains the all attributes flag, so we must fetch all attributes
135  return *this;
136  }
137 
139  mAttrs.clear();
140 
141  foreach ( const QString& attrName, attrNames )
142  {
143  int attrNum = fields.fieldNameIndex( attrName );
144  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
145  mAttrs.append( attrNum );
146  }
147 
148  return *this;
149 }
150 
152 {
154  return *this;
155 }
156 
158 {
159  switch ( mFilter )
160  {
162  return true;
163  break;
164 
166  if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
167  return true;
168  else
169  return false;
170  break;
171 
173  if ( feature.id() == mFilterFid )
174  return true;
175  else
176  return false;
177  break;
178 
180  if ( mFilterExpression->evaluate( feature ).toBool() )
181  return true;
182  else
183  return false;
184  break;
185 
187  if ( mFilterFids.contains( feature.id() ) )
188  return true;
189  else
190  return false;
191  break;
192  }
193 
194  return true;
195 }
196 
197 #include "qgsfeatureiterator.h"
198 #include "qgslogger.h"
199 
201 {
202  while ( !mActiveIterators.empty() )
203  {
205  QgsDebugMsg( "closing active iterator" );
206  it->close();
207  }
208 }
209 
211 {
212  mActiveIterators.insert( it );
213 }
214 
216 {
217  mActiveIterators.remove( it );
218 }
219 
220 
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:100
Class for parsing and evaluation of expressions (formerly called "search strings").
Definition: qgsexpression.h:86
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
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:317
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:234
QgsGeometry * geometry() const
Get the geometry object associated with this feature.
Definition: qgsfeature.cpp:112
QgsFeatureRequest & setSubsetOfAttributes(const QgsAttributeList &attrs)
Set a subset of attributes that will be fetched.
Container of fields for a vector layer.
Definition: qgsfield.h:172
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
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. {.
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
QList< int > QgsAttributeList
static const QString AllAttributes
Filter using a rectangle, no need to set NoGeometry.
QgsRectangle mFilterRect
Fetch only a subset of attributes (setSubsetOfAttributes sets this flag)
QgsFeatureRequest & setSimplifyMethod(const QgsSimplifyMethod &simplifyMethod)
Set a simplification method for geometries that will be fetched.
QgsFeatureId mFilterFid
No filter is applied.
QgsAttributeList mAttrs
qint64 QgsFeatureId
Definition: qgsfeature.h:30
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)