QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfeatureiterator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeatureiterator.cpp
3  ---------------------
4  begin : Juli 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 "qgsfeatureiterator.h"
16 #include "qgslogger.h"
17 
18 #include "qgsgeometrysimplifier.h"
19 #include "qgssimplifymethod.h"
20 
22  : mRequest( request )
23  , mClosed( false )
24  , refs( 0 )
25  , mGeometrySimplifier( NULL )
26  , mLocalSimplification( false )
27 {
28 }
29 
31 {
32  delete mGeometrySimplifier;
33  mGeometrySimplifier = NULL;
34 }
35 
37 {
38  bool dataOk = false;
39 
40  switch ( mRequest.filterType() )
41  {
43  dataOk = nextFeatureFilterExpression( f );
44  break;
45 
47  dataOk = nextFeatureFilterFids( f );
48  break;
49 
50  default:
51  dataOk = fetchFeature( f );
52  break;
53  }
54 
55  // simplify the geometry using the simplifier configured
56  if ( dataOk && mLocalSimplification )
57  {
58  const QgsGeometry* geometry = f.constGeometry();
59  if ( geometry )
60  simplify( f );
61  }
62  return dataOk;
63 }
64 
66 {
67  while ( fetchFeature( f ) )
68  {
69  if ( mRequest.filterExpression()->evaluate( f ).toBool() )
70  return true;
71  }
72  return false;
73 }
74 
76 {
77  while ( fetchFeature( f ) )
78  {
79  if ( mRequest.filterFids().contains( f.id() ) )
80  return true;
81  }
82  return false;
83 }
84 
86 {
87  // Prepare if required the simplification of geometries to fetch:
88  // This code runs here because of 'prepareSimplification()' is virtual and it can be overrided
89  // in inherited iterators who change the default behavior.
90  // It would be better to call this method in the constructor enabling virtual-calls as it is described by example at:
91  // http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-ctor-idiom.html
92  if ( refs == 0 )
93  {
95  }
96  refs++;
97 }
98 
100 {
101  refs--;
102  if ( !refs )
103  delete this;
104 }
105 
107 {
108  mLocalSimplification = false;
109 
110  delete mGeometrySimplifier;
111  mGeometrySimplifier = NULL;
112 
113  // setup the simplification of geometries to fetch
114  if ( !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) && simplifyMethod.methodType() != QgsSimplifyMethod::NoSimplification && ( simplifyMethod.forceLocalOptimization() || !providerCanSimplify( simplifyMethod.methodType() ) ) )
115  {
116  mGeometrySimplifier = QgsSimplifyMethod::createGeometrySimplifier( simplifyMethod );
117  mLocalSimplification = mGeometrySimplifier != NULL;
118  return mLocalSimplification;
119  }
120  return false;
121 }
122 
123 bool QgsAbstractFeatureIterator::providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const
124 {
125  Q_UNUSED( methodType )
126  return false;
127 }
128 
129 bool QgsAbstractFeatureIterator::simplify( QgsFeature& feature )
130 {
131  // simplify locally the geometry using the configured simplifier
132  if ( mGeometrySimplifier )
133  {
134  QgsGeometry* geometry = feature.geometry();
135 
136  QGis::GeometryType geometryType = geometry->type();
137  if ( geometryType == QGis::Line || geometryType == QGis::Polygon )
138  return mGeometrySimplifier->simplifyGeometry( geometry );
139  }
140  return false;
141 }
142 
144 
146 {
147  if ( this != &other )
148  {
149  if ( mIter )
150  mIter->deref();
151  mIter = other.mIter;
152  if ( mIter )
153  mIter->ref();
154  }
155  return *this;
156 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:51
Wrapper for iterator of features from vector data provider or vector layer.
const QgsSimplifyMethod & simplifyMethod() const
Get simplification method for geometries that will be fetched.
const Flags & flags() const
Filter using feature IDs.
const QgsAbstractGeometryV2 * geometry() const
Returns the underlying geometry store.
GeometryType
Definition: qgis.h:155
QVariant evaluate(const QgsFeature *f=NULL)
Evaluate the feature and return the result.
virtual bool fetchFeature(QgsFeature &f)=0
If you write a feature iterator for your provider, this is the method you need to implement!! ...
const QgsFeatureIds & filterFids() const
QGis::GeometryType type() const
Returns type of the geometry as a QGis::GeometryType.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:75
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:162
virtual bool simplifyGeometry(QgsGeometry *geometry) const =0
Simplifies the specified geometry.
virtual bool nextFeature(QgsFeature &f)
fetch next feature, return true on success
QgsFeatureIterator & operator=(const QgsFeatureIterator &other)
Internal feature iterator to be implemented within data providers.
virtual bool nextFeatureFilterExpression(QgsFeature &f)
By default, the iterator will fetch all features and check if the feature matches the expression...
This class wraps a request for features to a vector layer (or directly its vector data provider)...
No simplification is applied.
FilterType filterType() const
void deref()
remove reference, delete if refs == 0
virtual bool nextFeatureFilterFids(QgsFeature &f)
By default, the iterator will fetch all features and check if the id is in the request.
bool contains(const T &value) const
QgsFeatureRequest mRequest
A copy of the feature request.
QgsAbstractFeatureIterator * mIter
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:68
static QgsAbstractGeometrySimplifier * createGeometrySimplifier(const QgsSimplifyMethod &simplifyMethod)
Creates a geometry simplifier according to specified method.
bool toBool() const
This class contains information about how to simplify geometries fetched from a QgsFeatureIterator.
int refs
reference counting (to allow seamless copying of QgsFeatureIterator instances)
Geometry is not required. It may still be returned if e.g. required for a filter condition.
virtual bool prepareSimplification(const QgsSimplifyMethod &simplifyMethod)
Setup the simplification of geometries to fetch using the specified simplify method.
virtual ~QgsAbstractFeatureIterator()
destructor makes sure that the iterator is closed properly
QgsAbstractFeatureIterator(const QgsFeatureRequest &request)
base class constructor - stores the iteration parameters
MethodType methodType() const
Gets the simplification type.
bool forceLocalOptimization() const
Gets whether the simplification executes after fetch the geometries from provider, otherwise it executes, when supported, in provider before fetch the geometries.
QgsExpression * filterExpression() const