QGIS API Documentation  2.7.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  , mFilterExpression( 0 )
28  , mFlags( 0 )
29 {
30 }
31 
33  : mFilter( FilterFid )
34  , mFilterFid( fid )
35  , mFilterExpression( 0 )
36  , mFlags( 0 )
37 {
38 }
39 
41  : mFilter( FilterRect )
42  , mFilterRect( rect )
43  , mFilterExpression( 0 )
44  , mFlags( 0 )
45 {
46 }
47 
49  : mFilter( FilterExpression )
50  , mFilterExpression( new QgsExpression( expr.expression() ) )
51  , mFlags( 0 )
52 {
53 }
54 
56 {
57  operator=( rh );
58 }
59 
61 {
62  mFlags = rh.mFlags;
63  mFilter = rh.mFilter;
67  if ( rh.mFilterExpression )
68  {
70  }
71  else
72  {
74  }
75  mAttrs = rh.mAttrs;
77  return *this;
78 }
79 
81 {
82  delete mFilterExpression;
83 }
84 
86 {
88  mFilterRect = rect;
89  return *this;
90 }
91 
93 {
95  mFilterFid = fid;
96  return *this;
97 }
98 
100 {
102  mFilterFids = fids;
103  return *this;
104 }
105 
107 {
109  delete mFilterExpression;
110  mFilterExpression = new QgsExpression( expression );
111  return *this;
112 }
113 
114 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
115 {
116  mFlags = flags;
117  return *this;
118 }
119 
121 {
123  mAttrs = attrs;
124  return *this;
125 }
126 
127 QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields )
128 {
129  if ( attrNames.contains( QgsFeatureRequest::AllAttributes ) )
130  {
131  //attribute string list contains the all attributes flag, so we must fetch all attributes
132  return *this;
133  }
134 
136  mAttrs.clear();
137 
138  foreach ( const QString& attrName, attrNames )
139  {
140  int attrNum = fields.fieldNameIndex( attrName );
141  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
142  mAttrs.append( attrNum );
143  }
144 
145  return *this;
146 }
147 
149 {
151  return *this;
152 }
153 
155 {
156  switch ( mFilter )
157  {
159  return true;
160  break;
161 
163  if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
164  return true;
165  else
166  return false;
167  break;
168 
170  if ( feature.id() == mFilterFid )
171  return true;
172  else
173  return false;
174  break;
175 
177  if ( mFilterExpression->evaluate( feature ).toBool() )
178  return true;
179  else
180  return false;
181  break;
182 
184  if ( mFilterFids.contains( feature.id() ) )
185  return true;
186  else
187  return false;
188  break;
189  }
190 
191  return true;
192 }
193 
194 #include "qgsfeatureiterator.h"
195 #include "qgslogger.h"
196 
198 {
199  while ( !mActiveIterators.empty() )
200  {
202  QgsDebugMsg( "closing active iterator" );
203  it->close();
204  }
205 }
206 
208 {
209  mActiveIterators.insert( it );
210 }
211 
213 {
214  mActiveIterators.remove( it );
215 }
216 
217 
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:87
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:208
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)