Skip to content

Commit

Permalink
Support for setSubsetString() in memory provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wonder-sk committed Sep 13, 2014
1 parent b44ef92 commit 02a9bc8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/providers/memory/qgsmemoryfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
: QgsAbstractFeatureIteratorFromSource( source, ownSource, request )
, mSelectRectGeom( 0 )
, mSubsetExpression( 0 )
{
if ( !mSource->mSubsetString.isEmpty() )
{
mSubsetExpression = new QgsExpression( mSource->mSubsetString );
mSubsetExpression->prepare( mSource->mFields );
}

if ( mRequest.filterType() == QgsFeatureRequest::FilterRect && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
Expand Down Expand Up @@ -58,6 +64,8 @@ QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource* sour
QgsMemoryFeatureIterator::~QgsMemoryFeatureIterator()
{
close();

delete mSubsetExpression;
}


Expand Down Expand Up @@ -91,6 +99,9 @@ bool QgsMemoryFeatureIterator::nextFeatureUsingList( QgsFeature& feature )
else
hasFeature = true;

if ( mSubsetExpression && !mSubsetExpression->evaluate( mSource->mFeatures[*mFeatureIdListIterator] ).toBool() )
hasFeature = false;

if ( hasFeature )
break;

Expand Down Expand Up @@ -141,6 +152,9 @@ bool QgsMemoryFeatureIterator::nextFeatureTraverseAll( QgsFeature& feature )
}
}

if ( mSubsetExpression && !mSubsetExpression->evaluate( *mSelectIterator ).toBool() )
hasFeature = false;

if ( hasFeature )
break;

Expand Down Expand Up @@ -194,6 +208,7 @@ QgsMemoryFeatureSource::QgsMemoryFeatureSource( const QgsMemoryProvider* p )
: mFields( p->mFields )
, mFeatures( p->mFeatures )
, mSpatialIndex( p->mSpatialIndex ? new QgsSpatialIndex( *p->mSpatialIndex ) : 0 ) // just shallow copy
, mSubsetString( p->mSubsetString )
{
}

Expand Down
2 changes: 2 additions & 0 deletions src/providers/memory/qgsmemoryfeatureiterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class QgsMemoryFeatureSource : public QgsAbstractFeatureSource
QgsFields mFields;
QgsFeatureMap mFeatures;
QgsSpatialIndex* mSpatialIndex;
QString mSubsetString;

friend class QgsMemoryFeatureIterator;
};
Expand Down Expand Up @@ -67,6 +68,7 @@ class QgsMemoryFeatureIterator : public QgsAbstractFeatureIteratorFromSource<Qgs
bool mUsingFeatureIdList;
QList<QgsFeatureId> mFeatureIdList;
QList<QgsFeatureId>::const_iterator mFeatureIdListIterator;
QgsExpression* mSubsetExpression;

};

Expand Down
20 changes: 20 additions & 0 deletions src/providers/memory/qgsmemoryprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,26 @@ bool QgsMemoryProvider::changeGeometryValues( QgsGeometryMap & geometry_map )
return true;
}

QString QgsMemoryProvider::subsetString()
{
return mSubsetString;
}

bool QgsMemoryProvider::setSubsetString( QString theSQL, bool updateFeatureCount )
{
Q_UNUSED( updateFeatureCount );

if ( !theSQL.isEmpty() )
{
QgsExpression tempExpression( theSQL );
if ( tempExpression.hasParserError() )
return false;
}

mSubsetString = theSQL;
return true;
}

bool QgsMemoryProvider::createSpatialIndex()
{
if ( !mSpatialIndex )
Expand Down
10 changes: 10 additions & 0 deletions src/providers/memory/qgsmemoryprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@ class QgsMemoryProvider : public QgsVectorDataProvider
*/
virtual bool changeGeometryValues( QgsGeometryMap & geometry_map );

/** Accessor for sql where clause used to limit dataset */
QString subsetString();

/** mutator for sql where clause used to limit dataset size */
bool setSubsetString( QString theSQL, bool updateFeatureCount = true );

virtual bool supportsSubsetString() { return true; }

/**
* Creates a spatial index
* @return true in case of success
Expand Down Expand Up @@ -170,5 +178,7 @@ class QgsMemoryProvider : public QgsVectorDataProvider
// indexing
QgsSpatialIndex* mSpatialIndex;

QString mSubsetString;

friend class QgsMemoryFeatureSource;
};

0 comments on commit 02a9bc8

Please sign in to comment.