QGIS API Documentation  2.12.0-Lyon
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  {
71  return true;
72  }
73  return false;
74 }
75 
77 {
78  while ( fetchFeature( f ) )
79  {
80  if ( mRequest.filterFids().contains( f.id() ) )
81  return true;
82  }
83  return false;
84 }
85 
87 {
88  // Prepare if required the simplification of geometries to fetch:
89  // This code runs here because of 'prepareSimplification()' is virtual and it can be overrided
90  // in inherited iterators who change the default behavior.
91  // It would be better to call this method in the constructor enabling virtual-calls as it is described by example at:
92  // http://www.parashift.com/c%2B%2B-faq-lite/calling-virtuals-from-ctor-idiom.html
93  if ( refs == 0 )
94  {
96  }
97  refs++;
98 }
99 
101 {
102  refs--;
103  if ( !refs )
104  delete this;
105 }
106 
108 {
109  mLocalSimplification = false;
110 
111  delete mGeometrySimplifier;
112  mGeometrySimplifier = NULL;
113 
114  // setup the simplification of geometries to fetch
115  if ( !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) && simplifyMethod.methodType() != QgsSimplifyMethod::NoSimplification && ( simplifyMethod.forceLocalOptimization() || !providerCanSimplify( simplifyMethod.methodType() ) ) )
116  {
117  mGeometrySimplifier = QgsSimplifyMethod::createGeometrySimplifier( simplifyMethod );
118  mLocalSimplification = mGeometrySimplifier != NULL;
119  return mLocalSimplification;
120  }
121  return false;
122 }
123 
124 bool QgsAbstractFeatureIterator::providerCanSimplify( QgsSimplifyMethod::MethodType methodType ) const
125 {
126  Q_UNUSED( methodType )
127  return false;
128 }
129 
130 bool QgsAbstractFeatureIterator::simplify( QgsFeature& feature )
131 {
132  // simplify locally the geometry using the configured simplifier
133  if ( mGeometrySimplifier )
134  {
135  QgsGeometry* geometry = feature.geometry();
136 
137  QGis::GeometryType geometryType = geometry->type();
138  if ( geometryType == QGis::Line || geometryType == QGis::Polygon )
139  return mGeometrySimplifier->simplifyGeometry( geometry );
140  }
141  return false;
142 }
143 
145 
147 {
148  if ( this != &other )
149  {
150  if ( mIter )
151  mIter->deref();
152  mIter = other.mIter;
153  if ( mIter )
154  mIter->ref();
155  }
156  return *this;
157 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
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.
Q_DECL_DEPRECATED QVariant evaluate(const QgsFeature *f)
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!! ...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
QgsAbstractGeometryV2 * geometry() const
Returns the underlying geometry store.
QgsExpressionContext * expressionContext()
Returns the expression context used to evaluate filter expressions.
const QgsFeatureIds & filterFids() const
Get feature IDs that should be fetched.
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:76
GeometryType
Definition: qgis.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
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
Return the filter type which is currently set on this request.
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:70
static QgsAbstractGeometrySimplifier * createGeometrySimplifier(const QgsSimplifyMethod &simplifyMethod)
Creates a geometry simplifier according to specified method.
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
Returns the filter expression if set.