Skip to content
Permalink
Browse files

Updated memory provider to support feature source (untested yet)

  • Loading branch information
wonder-sk committed Dec 3, 2013
1 parent 5386324 commit b99e93caea00563d3ccfd5ef4becc6de8b7379c8
@@ -21,12 +21,11 @@
#include "qgsmessagelog.h"


QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryProvider* p, const QgsFeatureRequest& request )
: QgsAbstractFeatureIterator( request )
, P( p )

QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryFeatureSource* source, bool ownSource, const QgsFeatureRequest& request )
: QgsAbstractFeatureIteratorFromSource( source, ownSource, request )
, mSelectRectGeom( 0 )
{
P->mActiveIterators << this;

if ( mRequest.filterType() == QgsFeatureRequest::FilterRect && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
@@ -35,17 +34,17 @@ QgsMemoryFeatureIterator::QgsMemoryFeatureIterator( QgsMemoryProvider* p, const

// if there's spatial index, use it!
// (but don't use it when selection rect is not specified)
if ( mRequest.filterType() == QgsFeatureRequest::FilterRect && P->mSpatialIndex )
if ( mRequest.filterType() == QgsFeatureRequest::FilterRect && mSource->mSpatialIndex )
{
mUsingFeatureIdList = true;
mFeatureIdList = P->mSpatialIndex->intersects( mRequest.filterRect() );
mFeatureIdList = mSource->mSpatialIndex->intersects( mRequest.filterRect() );
QgsDebugMsg( "Features returned by spatial index: " + QString::number( mFeatureIdList.count() ) );
}
else if ( mRequest.filterType() == QgsFeatureRequest::FilterFid )
{
mUsingFeatureIdList = true;
QgsFeatureMap::iterator it = P->mFeatures.find( mRequest.filterFid() );
if ( it != P->mFeatures.end() )
QgsFeatureMap::const_iterator it = mSource->mFeatures.find( mRequest.filterFid() );
if ( it != mSource->mFeatures.end() )
mFeatureIdList.append( mRequest.filterFid() );
}
else
@@ -81,12 +80,12 @@ bool QgsMemoryFeatureIterator::nextFeatureUsingList( QgsFeature& feature )
bool hasFeature = false;

// option 1: we have a list of features to traverse
while ( mFeatureIdListIterator != mFeatureIdList.end() )
while ( mFeatureIdListIterator != mFeatureIdList.constEnd() )
{
if ( mRequest.filterType() == QgsFeatureRequest::FilterRect && mRequest.flags() & QgsFeatureRequest::ExactIntersect )
{
// do exact check in case we're doing intersection
if ( P->mFeatures[*mFeatureIdListIterator].geometry()->intersects( mSelectRectGeom ) )
if ( mSource->mFeatures[*mFeatureIdListIterator].geometry()->intersects( mSelectRectGeom ) )
hasFeature = true;
}
else
@@ -101,14 +100,14 @@ bool QgsMemoryFeatureIterator::nextFeatureUsingList( QgsFeature& feature )
// copy feature
if ( hasFeature )
{
feature = P->mFeatures[*mFeatureIdListIterator];
feature = mSource->mFeatures[*mFeatureIdListIterator];
mFeatureIdListIterator++;
}
else
close();

if ( hasFeature )
feature.setFields( &P->mFields ); // allow name-based attribute lookups
feature.setFields( &mSource->mFields ); // allow name-based attribute lookups

return hasFeature;
}
@@ -119,7 +118,7 @@ bool QgsMemoryFeatureIterator::nextFeatureTraverseAll( QgsFeature& feature )
bool hasFeature = false;

// option 2: traversing the whole layer
while ( mSelectIterator != P->mFeatures.end() )
while ( mSelectIterator != mSource->mFeatures.constEnd() )
{
if ( mRequest.filterType() != QgsFeatureRequest::FilterRect )
{
@@ -154,7 +153,7 @@ bool QgsMemoryFeatureIterator::nextFeatureTraverseAll( QgsFeature& feature )
feature = mSelectIterator.value();
mSelectIterator++;
feature.setValid( true );
feature.setFields( &P->mFields ); // allow name-based attribute lookups
feature.setFields( &mSource->mFields ); // allow name-based attribute lookups
}
else
close();
@@ -168,9 +167,9 @@ bool QgsMemoryFeatureIterator::rewind()
return false;

if ( mUsingFeatureIdList )
mFeatureIdListIterator = mFeatureIdList.begin();
mFeatureIdListIterator = mFeatureIdList.constBegin();
else
mSelectIterator = P->mFeatures.begin();
mSelectIterator = mSource->mFeatures.constBegin();

return true;
}
@@ -180,11 +179,30 @@ bool QgsMemoryFeatureIterator::close()
if ( mClosed )
return false;

P->mActiveIterators.remove( this );
iteratorClosed();

delete mSelectRectGeom;
mSelectRectGeom = NULL;

mClosed = true;
return true;
}

// -------------------------

QgsMemoryFeatureSource::QgsMemoryFeatureSource( const QgsMemoryProvider* p )
: mFields( p->mFields )
, mFeatures( p->mFeatures )
, mSpatialIndex( p->mSpatialIndex ? new QgsSpatialIndex( *p->mSpatialIndex ) : 0 ) // just shallow copy
{
}

QgsMemoryFeatureSource::~QgsMemoryFeatureSource()
{
delete mSpatialIndex;
}

QgsFeatureIterator QgsMemoryFeatureSource::getFeatures( const QgsFeatureRequest& request )
{
return QgsFeatureIterator( new QgsMemoryFeatureIterator( this, false, request ) );
}
@@ -21,11 +21,30 @@ class QgsMemoryProvider;

typedef QMap<QgsFeatureId, QgsFeature> QgsFeatureMap;

class QgsSpatialIndex;

class QgsMemoryFeatureIterator : public QgsAbstractFeatureIterator

class QgsMemoryFeatureSource : public QgsAbstractFeatureSource
{
public:
QgsMemoryFeatureSource( const QgsMemoryProvider* p );
~QgsMemoryFeatureSource();

virtual QgsFeatureIterator getFeatures( const QgsFeatureRequest& request );

protected:
QgsFields mFields;
QgsFeatureMap mFeatures;
QgsSpatialIndex* mSpatialIndex;

friend class QgsMemoryFeatureIterator;
};


class QgsMemoryFeatureIterator : public QgsAbstractFeatureIteratorFromSource<QgsMemoryFeatureSource>
{
public:
QgsMemoryFeatureIterator( QgsMemoryProvider* p, const QgsFeatureRequest& request );
QgsMemoryFeatureIterator( QgsMemoryFeatureSource* source, bool ownSource, const QgsFeatureRequest& request );

~QgsMemoryFeatureIterator();

@@ -43,13 +62,11 @@ class QgsMemoryFeatureIterator : public QgsAbstractFeatureIterator
bool nextFeatureUsingList( QgsFeature& feature );
bool nextFeatureTraverseAll( QgsFeature& feature );

QgsMemoryProvider* P;

QgsGeometry* mSelectRectGeom;
QgsFeatureMap::iterator mSelectIterator;
QgsFeatureMap::const_iterator mSelectIterator;
bool mUsingFeatureIdList;
QList<QgsFeatureId> mFeatureIdList;
QList<QgsFeatureId>::iterator mFeatureIdListIterator;
QList<QgsFeatureId>::const_iterator mFeatureIdListIterator;

};

@@ -151,16 +151,14 @@ QgsMemoryProvider::QgsMemoryProvider( QString uri )

QgsMemoryProvider::~QgsMemoryProvider()
{
while ( !mActiveIterators.empty() )
{
QgsMemoryFeatureIterator *it = *mActiveIterators.begin();
QgsDebugMsg( "closing active iterator" );
it->close();
}

delete mSpatialIndex;
}

QgsAbstractFeatureSource* QgsMemoryProvider::featureSource() const
{
return new QgsMemoryFeatureSource( this );
}

QString QgsMemoryProvider::dataSourceUri() const
{
QUrl uri( "memory" );
@@ -238,7 +236,7 @@ QString QgsMemoryProvider::storageType() const

QgsFeatureIterator QgsMemoryProvider::getFeatures( const QgsFeatureRequest& request )
{
return QgsFeatureIterator( new QgsMemoryFeatureIterator( this, request ) );
return QgsFeatureIterator( new QgsMemoryFeatureIterator( new QgsMemoryFeatureSource( this ), true, request ) );
}


@@ -34,6 +34,8 @@ class QgsMemoryProvider : public QgsVectorDataProvider

/* Implementation of functions from QgsVectorDataProvider */

virtual QgsAbstractFeatureSource* featureSource() const;

/**
* Returns the permanent storage type for this layer as a friendly name.
*/
@@ -168,6 +170,5 @@ class QgsMemoryProvider : public QgsVectorDataProvider
// indexing
QgsSpatialIndex* mSpatialIndex;

friend class QgsMemoryFeatureIterator;
QSet< QgsMemoryFeatureIterator *> mActiveIterators;
friend class QgsMemoryFeatureSource;
};

0 comments on commit b99e93c

Please sign in to comment.
You can’t perform that action at this time.