QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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 
23  : mFilter( FilterNone )
24  , mFilterExpression( 0 )
25  , mFlags( 0 )
26 {
27 }
28 
30  : mFilter( FilterFid )
31  , mFilterFid( fid )
32  , mFilterExpression( 0 )
33  , mFlags( 0 )
34 {
35 }
36 
38  : mFilter( FilterRect )
39  , mFilterRect( rect )
40  , mFilterExpression( 0 )
41  , mFlags( 0 )
42 {
43 }
44 
46  : mFilter( FilterExpression )
47  , mFilterExpression( new QgsExpression( expr.expression() ) )
48  , mFlags( 0 )
49 {
50 }
51 
53 {
54  operator=( rh );
55 }
56 
58 {
59  mFlags = rh.mFlags;
60  mFilter = rh.mFilter;
64  if ( rh.mFilterExpression )
65  {
67  }
68  else
69  {
71  }
72  mAttrs = rh.mAttrs;
74  return *this;
75 }
76 
78 {
79  delete mFilterExpression;
80 }
81 
83 {
85  mFilterRect = rect;
86  return *this;
87 }
88 
90 {
92  mFilterFid = fid;
93  return *this;
94 }
95 
97 {
99  mFilterFids = fids;
100  return *this;
101 }
102 
104 {
106  delete mFilterExpression;
107  mFilterExpression = new QgsExpression( expression );
108  return *this;
109 }
110 
111 QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
112 {
113  mFlags = flags;
114  return *this;
115 }
116 
118 {
120  mAttrs = attrs;
121  return *this;
122 }
123 
124 QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields )
125 {
127  mAttrs.clear();
128 
129  foreach ( const QString& attrName, attrNames )
130  {
131  int attrNum = fields.fieldNameIndex( attrName );
132  if ( attrNum != -1 && !mAttrs.contains( attrNum ) )
133  mAttrs.append( attrNum );
134  }
135 
136  return *this;
137 }
138 
140 {
142  return *this;
143 }
144 
146 {
147  switch ( mFilter )
148  {
150  return true;
151  break;
152 
154  if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
155  return true;
156  else
157  return false;
158  break;
159 
161  if ( feature.id() == mFilterFid )
162  return true;
163  else
164  return false;
165  break;
166 
168  if ( mFilterExpression->evaluate( feature ).toBool() )
169  return true;
170  else
171  return false;
172  break;
173 
175  if ( mFilterFids.contains( feature.id() ) )
176  return true;
177  else
178  return false;
179  break;
180  }
181 
182  return true;
183 }
184 
185 #include "qgsfeatureiterator.h"
186 #include "qgslogger.h"
187 
189 {
190  while ( !mActiveIterators.empty() )
191  {
193  QgsDebugMsg( "closing active iterator" );
194  it->close();
195  }
196 }
197 
199 {
200  mActiveIterators.insert( it );
201 }
202 
204 {
205  mActiveIterators.remove( it );
206 }
207 
208 
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:89
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
Return the expression string that was given when created.
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:326
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:172
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:164
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
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
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)