Skip to content

Commit

Permalink
#8725-R: refactoring of simplification API in FeatureIterators
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuarte47 authored and m-kuhn committed Jan 15, 2014
1 parent 1e781ff commit 4cc82e6
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 30 deletions.
31 changes: 20 additions & 11 deletions src/core/qgsfeatureiterator.cpp
Expand Up @@ -55,16 +55,11 @@ bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )
break;
}

// simplify locally the geometry using the simplifier defined in constructor
if ( dataOk && mGeometrySimplifier )
// simplify the geometry using the simplifier configured
if ( dataOk )
{
QgsGeometry* geometry = f.geometry();

if ( geometry )
{
QGis::GeometryType geometryType = geometry->type();
if ( geometryType == QGis::Line || geometryType == QGis::Polygon ) mGeometrySimplifier->simplifyGeometry( geometry );
}
if ( geometry ) simplify( f );
}
return dataOk;
}
Expand Down Expand Up @@ -101,10 +96,8 @@ void QgsAbstractFeatureIterator::deref()
delete this;
}

bool QgsAbstractFeatureIterator::prepareLocalSimplification()
bool QgsAbstractFeatureIterator::prepareSimplification( const QgsSimplifyMethod& simplifyMethod )
{
const QgsSimplifyMethod& simplifyMethod = mRequest.simplifyMethod();

if ( mGeometrySimplifier )
{
delete mGeometrySimplifier;
Expand Down Expand Up @@ -136,6 +129,22 @@ bool QgsAbstractFeatureIterator::prepareLocalSimplification()
return false;
}

bool QgsAbstractFeatureIterator::simplify( QgsFeature& feature )
{
// simplify locally the geometry using the configured simplifier
if ( mGeometrySimplifier )
{
QgsGeometry* geometry = feature.geometry();

if ( geometry )
{
QGis::GeometryType geometryType = geometry->type();
if ( geometryType == QGis::Line || geometryType == QGis::Polygon ) return mGeometrySimplifier->simplifyGeometry( geometry );
}
}
return false;
}

///////

QgsFeatureIterator& QgsFeatureIterator::operator=( const QgsFeatureIterator & other )
Expand Down
7 changes: 5 additions & 2 deletions src/core/qgsfeatureiterator.h
Expand Up @@ -87,12 +87,15 @@ class CORE_EXPORT QgsAbstractFeatureIterator
void deref(); //!< remove reference, delete if refs == 0
friend class QgsFeatureIterator;

//! setup if required the local simplification of geometries to fetch, it uses the settings of current FeatureRequest
virtual bool prepareLocalSimplification();
//! Setup the simplification of geometries to fetch using the specified simplify method
virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );

private:
//! optional object to locally simplify geometries fetched by this feature iterator
QgsAbstractGeometrySimplifier* mGeometrySimplifier;

//! simplify the specified geometry if it was configured
virtual bool simplify( QgsFeature& feature );
};


Expand Down
13 changes: 11 additions & 2 deletions src/core/qgsvectorlayerfeatureiterator.cpp
Expand Up @@ -68,6 +68,15 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayer* la
}
else // no filter or filter by rect
{
QgsSimplifyMethod simplifyMethod = request.simplifyMethod();

// if required, local simplification will be configured for all providers, then avoid simplify twice (this iterator and provider iterator)
if ( simplifyMethod.methodType() != QgsSimplifyMethod::NoSimplification && simplifyMethod.forceLocalOptimization() )
{
simplifyMethod.setMethodType( QgsSimplifyMethod::NoSimplification );
if ( L->editBuffer() ) mChangedFeaturesRequest.setSimplifyMethod( simplifyMethod ); else mProviderRequest.setSimplifyMethod( simplifyMethod );
}

if ( L->editBuffer() )
{
mChangedFeaturesIterator = L->dataProvider()->getFeatures( mChangedFeaturesRequest );
Expand All @@ -77,8 +86,8 @@ QgsVectorLayerFeatureIterator::QgsVectorLayerFeatureIterator( QgsVectorLayer* la
mProviderIterator = L->dataProvider()->getFeatures( mProviderRequest );
}

// setup if required the local simplification of geometries to fetch
prepareLocalSimplification();
// prepare if required the local simplification of geometries to fetch
prepareSimplification( request.simplifyMethod() );

rewindEditBuffer();
}
Expand Down
12 changes: 6 additions & 6 deletions src/providers/ogr/qgsogrfeatureiterator.cpp
Expand Up @@ -81,7 +81,7 @@ QgsOgrFeatureIterator::QgsOgrFeatureIterator( QgsOgrProvider* p, const QgsFeatur
}

//setup if required the simplification of OGR-geometries fetched
prepareProviderSimplification();
prepareSimplification( request.simplifyMethod() );

//start with first feature
rewind();
Expand All @@ -105,9 +105,9 @@ void QgsOgrFeatureIterator::ensureRelevantFields()
P->mRelevantFieldsForNextFeature = true;
}

bool QgsOgrFeatureIterator::prepareProviderSimplification()
bool QgsOgrFeatureIterator::prepareSimplification( const QgsSimplifyMethod& simplifyMethod )
{
const QgsSimplifyMethod& simplifyMethod = mRequest.simplifyMethod();
bool providerSimplification = false;

if ( mGeometrySimplifier )
{
Expand All @@ -124,20 +124,20 @@ bool QgsOgrFeatureIterator::prepareProviderSimplification()
{
int simplifyFlags = QgsMapToPixelSimplifier::SimplifyGeometry | QgsMapToPixelSimplifier::SimplifyEnvelope;
mGeometrySimplifier = new QgsOgrMapToPixelSimplifier( simplifyFlags, simplifyMethod.tolerance() );
return true;
providerSimplification = true;
}
else
if ( methodType == QgsSimplifyMethod::PreserveTopology )
{
mGeometrySimplifier = new QgsOgrTopologyPreservingSimplifier( simplifyMethod.tolerance() );
return true;
providerSimplification = true;
}
else
{
QgsDebugMsg( QString( "Simplification method type (%1) is not recognised by OgrFeatureIterator class" ).arg( methodType ) );
}
}
return false;
return QgsAbstractFeatureIterator::prepareSimplification( simplifyMethod ) || providerSimplification;
}

bool QgsOgrFeatureIterator::fetchFeature( QgsFeature& feature )
Expand Down
4 changes: 2 additions & 2 deletions src/providers/ogr/qgsogrfeatureiterator.h
Expand Up @@ -39,8 +39,8 @@ class QgsOgrFeatureIterator : public QgsAbstractFeatureIterator
//! fetch next feature, return true on success
virtual bool fetchFeature( QgsFeature& feature );

//! setup if required the simplification of OGR-geometries fetched, it uses the settings of current FeatureRequest
virtual bool prepareProviderSimplification();
//! Setup the simplification of geometries to fetch using the specified simplify method
virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );

QgsOgrProvider* P;

Expand Down
10 changes: 5 additions & 5 deletions src/providers/postgres/qgspostgresfeatureiterator.cpp
Expand Up @@ -81,7 +81,7 @@ QgsPostgresFeatureIterator::QgsPostgresFeatureIterator( QgsPostgresProvider* p,
}

//setup if required the simplification of geometries to fetch
prepareProviderSimplification();
prepareSimplification( request.simplifyMethod() );

mFetched = 0;
}
Expand Down Expand Up @@ -173,9 +173,9 @@ bool QgsPostgresFeatureIterator::fetchFeature( QgsFeature& feature )
return true;
}

bool QgsPostgresFeatureIterator::prepareProviderSimplification()
bool QgsPostgresFeatureIterator::prepareSimplification( const QgsSimplifyMethod& simplifyMethod )
{
const QgsSimplifyMethod& simplifyMethod = mRequest.simplifyMethod();
bool providerSimplification = false;

// validate settings of simplification of geometries to fetch
if ( simplifyMethod.methodType() != QgsSimplifyMethod::NoSimplification && !simplifyMethod.forceLocalOptimization() && !( mRequest.flags() & QgsFeatureRequest::NoGeometry ) )
Expand All @@ -184,14 +184,14 @@ bool QgsPostgresFeatureIterator::prepareProviderSimplification()

if ( methodType == QgsSimplifyMethod::OptimizeForRendering || methodType == QgsSimplifyMethod::PreserveTopology )
{
return true;
providerSimplification = true;
}
else
{
QgsDebugMsg( QString( "Simplification method type (%1) is not recognised by PostgresFeatureIterator" ).arg( methodType ) );
}
}
return false;
return QgsAbstractFeatureIterator::prepareSimplification( simplifyMethod ) || providerSimplification;
}

bool QgsPostgresFeatureIterator::rewind()
Expand Down
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresfeatureiterator.h
Expand Up @@ -40,8 +40,8 @@ class QgsPostgresFeatureIterator : public QgsAbstractFeatureIterator
//! fetch next feature, return true on success
virtual bool fetchFeature( QgsFeature& feature );

//! setup if required the simplification of geometries to fetch, it uses the settings of current FeatureRequest
virtual bool prepareProviderSimplification();
//! Setup the simplification of geometries to fetch using the specified simplify method
virtual bool prepareSimplification( const QgsSimplifyMethod& simplifyMethod );

QgsPostgresProvider* P;

Expand Down

0 comments on commit 4cc82e6

Please sign in to comment.