Skip to content
Permalink
Browse files

QgsFeatureRequest: New filters FilterExpression and FilterFids

  • Loading branch information
m-kuhn committed May 20, 2013
1 parent 6d485d9 commit 0c90f32007a8ac8db58f731a5c9a262e1eadd12e
Showing with 341 additions and 78 deletions.
  1. +6 −1 python/core/qgsfeatureiterator.sip
  2. +13 −3 python/core/qgsfeaturerequest.sip
  3. +2 −2 src/core/qgscachedfeatureiterator.cpp
  4. +2 −2 src/core/qgscachedfeatureiterator.h
  5. +2 −0 src/core/qgsexpression.h
  6. +39 −1 src/core/qgsfeatureiterator.cpp
  7. +9 −1 src/core/qgsfeatureiterator.h
  8. +78 −0 src/core/qgsfeaturerequest.cpp
  9. +31 −3 src/core/qgsfeaturerequest.h
  10. +68 −15 src/core/qgsvectorlayerfeatureiterator.cpp
  11. +10 −3 src/core/qgsvectorlayerfeatureiterator.h
  12. +5 −0 src/gui/attributetable/qgsattributetablemodel.cpp
  13. +2 −0 src/gui/attributetable/qgsattributetablemodel.h
  14. +9 −3 src/gui/attributetable/qgsdualview.cpp
  15. +5 −2 src/gui/attributetable/qgsdualview.h
  16. +2 −1 src/gui/qgsattributedialog.cpp
  17. +1 −1 src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.cpp
  18. +3 −3 src/providers/delimitedtext/qgsdelimitedtextfeatureiterator.h
  19. +1 −1 src/providers/gpx/qgsgpxfeatureiterator.cpp
  20. +3 −3 src/providers/gpx/qgsgpxfeatureiterator.h
  21. +1 −1 src/providers/grass/qgsgrassfeatureiterator.cpp
  22. +1 −1 src/providers/grass/qgsgrassfeatureiterator.h
  23. +1 −1 src/providers/memory/qgsmemoryfeatureiterator.cpp
  24. +3 −3 src/providers/memory/qgsmemoryfeatureiterator.h
  25. +1 −1 src/providers/mssql/qgsmssqlfeatureiterator.cpp
  26. +3 −3 src/providers/mssql/qgsmssqlfeatureiterator.h
  27. +1 −1 src/providers/ogr/qgsogrfeatureiterator.cpp
  28. +3 −3 src/providers/ogr/qgsogrfeatureiterator.h
  29. +1 −0 src/providers/ogr/qgsogrprovider.cpp
  30. +6 −1 src/providers/postgres/qgspostgresfeatureiterator.cpp
  31. +5 −3 src/providers/postgres/qgspostgresfeatureiterator.h
  32. +9 −6 src/providers/postgres/qgspostgresprovider.cpp
  33. +1 −0 src/providers/postgres/qgspostgresprovider.h
  34. +1 −1 src/providers/spatialite/qgsspatialitefeatureiterator.cpp
  35. +4 −3 src/providers/spatialite/qgsspatialitefeatureiterator.h
  36. +1 −1 src/providers/sqlanywhere/qgssqlanywherefeatureiterator.cpp
  37. +4 −2 src/providers/sqlanywhere/qgssqlanywherefeatureiterator.h
  38. +1 −1 src/providers/wfs/qgswfsfeatureiterator.cpp
  39. +3 −1 src/providers/wfs/qgswfsfeatureiterator.h
@@ -11,13 +11,18 @@ class QgsAbstractFeatureIterator
virtual ~QgsAbstractFeatureIterator();

//! fetch next feature, return true on success
virtual bool nextFeature( QgsFeature& f ) = 0;
virtual bool nextFeature( QgsFeature& f );
//! reset the iterator to the starting position
virtual bool rewind() = 0;
//! end of iterating: free the resources / lock
virtual bool close() = 0;

protected:
virtual bool nextFeatureFilterExpression( QgsFeature &f );

virtual bool nextFeatureFilterFids( QgsFeature & f );

virtual bool fetchFeature( QgsFeature& f ) = 0;
void ref(); // add reference
void deref(); // remove reference, delete if refs == 0
};
@@ -17,9 +17,11 @@ class QgsFeatureRequest

enum FilterType
{
FilterNone, //!< No filter is applied
FilterRect, //!< Filter using a rectangle
FilterFid //!< Filter using feature ID
FilterNone, //!< No filter is applied
FilterRect, //!< Filter using a rectangle
FilterFid, //!< Filter using feature ID
FilterExpression, //!< Filter using expression
FilterFids //!< Filter using feature IDs
};

//! construct a default request: for all features get attributes and geometries
@@ -40,6 +42,14 @@ class QgsFeatureRequest
QgsFeatureRequest& setFilterFid( qint64 fid );
qint64 filterFid() const;

//! Set feature ID that should be fetched.
QgsFeatureRequest& setFilterFids( QgsFeatureIds fids );
const QgsFeatureIds& filterFids() const;

//! Set filter expression. Ownership is taken.
QgsFeatureRequest& setFilterExpression( const QString& expression );
QgsExpression* filterExpression() const;

//! Set flags that affect how features will be fetched
QgsFeatureRequest& setFlags( Flags flags );
const Flags& flags() const;
@@ -24,7 +24,7 @@ QgsCachedFeatureIterator::QgsCachedFeatureIterator( QgsVectorLayerCache *vlCache
mFeatureIdIterator = featureIds.begin();
}

bool QgsCachedFeatureIterator::nextFeature( QgsFeature& f )
bool QgsCachedFeatureIterator::fetchFeature( QgsFeature& f )
{
mFeatureIdIterator++;

@@ -58,7 +58,7 @@ QgsCachedFeatureWriterIterator::QgsCachedFeatureWriterIterator( QgsVectorLayerCa
mFeatIt = vlCache->layer()->getFeatures( featureRequest );
}

bool QgsCachedFeatureWriterIterator::nextFeature( QgsFeature& f )
bool QgsCachedFeatureWriterIterator::fetchFeature( QgsFeature& f )
{
if ( mFeatIt.nextFeature( f ) )
{
@@ -46,7 +46,7 @@ class CORE_EXPORT QgsCachedFeatureIterator : public QgsAbstractFeatureIterator
* @param f
* @return bool
*/
virtual bool nextFeature( QgsFeature& f );
virtual bool fetchFeature( QgsFeature& f );

/**
* @brief
@@ -91,7 +91,7 @@ class CORE_EXPORT QgsCachedFeatureWriterIterator : public QgsAbstractFeatureIter
* @param f
* @return bool
*/
virtual bool nextFeature( QgsFeature& f );
virtual bool fetchFeature( QgsFeature& f );

/**
* @brief
@@ -29,6 +29,7 @@ class QgsFeature;
class QgsGeometry;
class QgsOgcUtils;
class QgsVectorLayer;
class QgsVectorDataProvider;

class QDomElement;

@@ -535,6 +536,7 @@ class CORE_EXPORT QgsExpression

virtual QStringList referencedColumns() const { return QStringList( mName ); }
virtual bool needsGeometry() const { return false; }

virtual void accept( Visitor& v ) const { v.visit( *this ); }

protected:
@@ -13,7 +13,7 @@
* *
***************************************************************************/
#include "qgsfeatureiterator.h"

#include "qgslogger.h"

QgsAbstractFeatureIterator::QgsAbstractFeatureIterator( const QgsFeatureRequest& request )
: mRequest( request )
@@ -26,6 +26,44 @@ QgsAbstractFeatureIterator::~QgsAbstractFeatureIterator()
{
}

bool QgsAbstractFeatureIterator::nextFeature( QgsFeature& f )
{
switch ( mRequest.filterType() )
{
case QgsFeatureRequest::FilterExpression:
return nextFeatureFilterExpression( f );
break;

case QgsFeatureRequest::FilterFids:
return nextFeatureFilterFids( f );
break;

default:
return fetchFeature( f );
break;
}
}

bool QgsAbstractFeatureIterator::nextFeatureFilterExpression( QgsFeature& f )
{
while ( fetchFeature( f ) )
{
if ( mRequest.filterExpression()->evaluate( f ).toBool() )
return true;
}
return false;
}

bool QgsAbstractFeatureIterator::nextFeatureFilterFids( QgsFeature& f )
{
while ( fetchFeature( f ) )
{
if ( mRequest.filterFids().contains( f.id() ) )
return true;
}
return false;
}

void QgsAbstractFeatureIterator::ref()
{
refs++;
@@ -16,6 +16,7 @@
#define QGSFEATUREITERATOR_H

#include "qgsfeaturerequest.h"
#include "qgslogger.h"


/** \ingroup core
@@ -31,13 +32,20 @@ class CORE_EXPORT QgsAbstractFeatureIterator
virtual ~QgsAbstractFeatureIterator();

//! fetch next feature, return true on success
virtual bool nextFeature( QgsFeature& f ) = 0;
virtual bool nextFeature( QgsFeature& f );

//! reset the iterator to the starting position
virtual bool rewind() = 0;
//! end of iterating: free the resources / lock
virtual bool close() = 0;

protected:
virtual bool nextFeatureFilterExpression( QgsFeature &f );

virtual bool nextFeatureFilterFids( QgsFeature & f );

virtual bool fetchFeature( QgsFeature& f ) = 0;

QgsFeatureRequest mRequest;

bool mClosed;
@@ -15,38 +15,61 @@
#include "qgsfeaturerequest.h"

#include "qgsfield.h"
#include "qgsgeometry.h"

#include <QStringList>

QgsFeatureRequest::QgsFeatureRequest()
: mFilter( FilterNone )
, mFilterExpression( 0 )
, mFlags( 0 )
{
}

QgsFeatureRequest::QgsFeatureRequest( QgsFeatureId fid )
: mFilter( FilterFid )
, mFilterFid( fid )
, mFilterExpression( 0 )
, mFlags( 0 )
{
}

QgsFeatureRequest::QgsFeatureRequest( const QgsRectangle& rect )
: mFilter( FilterRect )
, mFilterRect( rect )
, mFilterExpression( 0 )
, mFlags( 0 )
{
}

QgsFeatureRequest::QgsFeatureRequest( const QgsFeatureRequest &rh )
{
operator=( rh );
}

QgsFeatureRequest& QgsFeatureRequest::operator=( const QgsFeatureRequest & rh )
{
mFlags = rh.mFlags;
mFilter = rh.mFilter;
mFilterRect = rh.mFilterRect;
mFilterFid = rh.mFilterFid;
mFilterFids = rh.mFilterFids;
if ( rh.mFilterExpression )
{
mFilterExpression = new QgsExpression( rh.mFilterExpression->expression() );
}
else
{
mFilterExpression = 0;
}
mAttrs = rh.mAttrs;
return *this;
}

QgsFeatureRequest::~QgsFeatureRequest()
{
delete mFilterExpression;
}

QgsFeatureRequest& QgsFeatureRequest::setFilterRect( const QgsRectangle& rect )
{
@@ -62,6 +85,21 @@ QgsFeatureRequest& QgsFeatureRequest::setFilterFid( QgsFeatureId fid )
return *this;
}

QgsFeatureRequest&QgsFeatureRequest::setFilterFids( QgsFeatureIds fids )
{
mFilter = FilterFids;
mFilterFids = fids;
return *this;
}

QgsFeatureRequest& QgsFeatureRequest::setFilterExpression( const QString& expression )
{
mFilter = FilterExpression;
delete mFilterExpression;
mFilterExpression = new QgsExpression( expression );
return *this;
}

QgsFeatureRequest& QgsFeatureRequest::setFlags( QgsFeatureRequest::Flags flags )
{
mFlags = flags;
@@ -89,3 +127,43 @@ QgsFeatureRequest& QgsFeatureRequest::setSubsetOfAttributes( const QStringList&

return *this;
}

bool QgsFeatureRequest::acceptFeature( const QgsFeature& feature )
{
switch ( mFilter )
{
case QgsFeatureRequest::FilterNone:
return true;
break;

case QgsFeatureRequest::FilterRect:
if ( feature.geometry() && feature.geometry()->intersects( mFilterRect ) )
return true;
else
return false;
break;

case QgsFeatureRequest::FilterFid:
if ( feature.id () == mFilterFid )
return true;
else
return false;
break;

case QgsFeatureRequest::FilterExpression:
if ( mFilterExpression->evaluate( feature ).toBool() )
return true;
else
return false;
break;

case QgsFeatureRequest::FilterFids:
if ( mFilterFids.contains ( feature.id () ) )
return true;
else
return false;
break;
}

return true;
}
@@ -19,6 +19,7 @@

#include "qgsfeature.h"
#include "qgsrectangle.h"
#include "qgsexpression.h"

#include <QList>
typedef QList<int> QgsAttributeList;
@@ -66,9 +67,11 @@ class CORE_EXPORT QgsFeatureRequest

enum FilterType
{
FilterNone, //!< No filter is applied
FilterRect, //!< Filter using a rectangle
FilterFid //!< Filter using feature ID
FilterNone, //!< No filter is applied
FilterRect, //!< Filter using a rectangle
FilterFid, //!< Filter using feature ID
FilterExpression, //!< Filter using expression
FilterFids //!< Filter using feature ID's
};

//! construct a default request: for all features get attributes and geometries
@@ -80,6 +83,10 @@ class CORE_EXPORT QgsFeatureRequest
//! copy constructor
QgsFeatureRequest( const QgsFeatureRequest& rh );

QgsFeatureRequest& operator=( const QgsFeatureRequest& rh );

~QgsFeatureRequest();

FilterType filterType() const { return mFilter; }

//! Set rectangle from which features will be taken. Empty rectangle removes the filter.
@@ -91,6 +98,14 @@ class CORE_EXPORT QgsFeatureRequest
QgsFeatureRequest& setFilterFid( QgsFeatureId fid );
const QgsFeatureId& filterFid() const { return mFilterFid; }

//! Set feature ID that should be fetched.
QgsFeatureRequest& setFilterFids( QgsFeatureIds fids );
const QgsFeatureIds& filterFids() const { return mFilterFids; }

//! Set filter expression. {@see QgsExpression}
QgsFeatureRequest& setFilterExpression( const QString& expression );
QgsExpression* filterExpression() const { return mFilterExpression; }

//! Set flags that affect how features will be fetched
QgsFeatureRequest& setFlags( Flags flags );
const Flags& flags() const { return mFlags; }
@@ -102,6 +117,17 @@ class CORE_EXPORT QgsFeatureRequest

//! Set a subset of attributes by names that will be fetched
QgsFeatureRequest& setSubsetOfAttributes( const QStringList& attrNames, const QgsFields& fields );

/**
* Check if a feature is accepted by this requests filter
*
* @param feature The feature which will be tested
*
* @return true, if the filter accepts the feature
*
* @note added in 2.1
*/
bool acceptFeature( const QgsFeature& feature );

// TODO: in future
// void setFilterExpression(const QString& expression); // using QgsExpression
@@ -112,6 +138,8 @@ class CORE_EXPORT QgsFeatureRequest
FilterType mFilter;
QgsRectangle mFilterRect;
QgsFeatureId mFilterFid;
QgsFeatureIds mFilterFids;
QgsExpression* mFilterExpression;
Flags mFlags;
QgsAttributeList mAttrs;
};

0 comments on commit 0c90f32

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