Skip to content

Commit

Permalink
Add methods to vectorlayercache and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jul 19, 2016
1 parent d91a45c commit c8c623e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 27 deletions.
11 changes: 8 additions & 3 deletions python/core/qgsvectorlayer.sip
Expand Up @@ -617,21 +617,26 @@ class QgsVectorLayer : QgsMapLayer
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;

/**
* Query the provider for features matching a given expression.
* Query the layer for features matching a given expression.
*/
QgsFeatureIterator getFeatures( const QString& expression );

/**
* Query the provider for the feature with the given id.
* Query the layer for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
QgsFeature getFeature( QgsFeatureId fid );

/**
* Query the provider for the features with the given ids.
* Query the layer for the features with the given ids.
*/
QgsFeatureIterator getFeatures( QgsFeatureIds fids );

/**
* Query the layer for the features which intersect the specified rectangle.
*/
QgsFeatureIterator getFeatures( const QgsRectangle& rectangle );

/** Adds a feature
@param f feature to add
@param alsoUpdateExtent If True, will also go to the effort of e.g. updating the extents.
Expand Down
21 changes: 21 additions & 0 deletions python/core/qgsvectorlayercache.sip
Expand Up @@ -89,6 +89,27 @@ class QgsVectorLayerCache : QObject
*/
QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest = QgsFeatureRequest() );

/**
* Query the layer for features matching a given expression.
*/
QgsFeatureIterator getFeatures( const QString& expression );

/**
* Query the layer for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
QgsFeature getFeature( QgsFeatureId fid );

/**
* Query the layer for the features with the given ids.
*/
QgsFeatureIterator getFeatures( QgsFeatureIds fids );

/**
* Query the layer for the features which intersect the specified rectangle.
*/
QgsFeatureIterator getFeatures( const QgsRectangle& rectangle );

/**
* Check if a certain feature id is cached.
* @param fid The feature id to look for
Expand Down
18 changes: 0 additions & 18 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1112,24 +1112,6 @@ QgsFeatureIterator QgsVectorLayer::getFeatures( const QgsFeatureRequest& request
return QgsFeatureIterator( new QgsVectorLayerFeatureIterator( new QgsVectorLayerFeatureSource( this ), true, request ) );
}

QgsFeatureIterator QgsVectorLayer::getFeatures( const QString& expression )
{
return getFeatures( QgsFeatureRequest( expression ) );
}

QgsFeature QgsVectorLayer::getFeature( QgsFeatureId fid )
{
QgsFeature feature;
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
return feature;
}

QgsFeatureIterator QgsVectorLayer::getFeatures( QgsFeatureIds fids )
{
return getFeatures( QgsFeatureRequest( fids ) );
}


bool QgsVectorLayer::addFeature( QgsFeature& feature, bool alsoUpdateExtent )
{
Q_UNUSED( alsoUpdateExtent ); // TODO[MD]
Expand Down
31 changes: 25 additions & 6 deletions src/core/qgsvectorlayer.h
Expand Up @@ -988,20 +988,39 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
QgsFeatureIterator getFeatures( const QgsFeatureRequest& request = QgsFeatureRequest() ) const;

/**
* Query the provider for features matching a given expression.
* Query the layer for features matching a given expression.
*/
QgsFeatureIterator getFeatures( const QString& expression );
inline QgsFeatureIterator getFeatures( const QString& expression )
{
return getFeatures( QgsFeatureRequest( expression ) );
}

/**
* Query the provider for the feature with the given id.
* Query the layer for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
QgsFeature getFeature( QgsFeatureId fid );
inline QgsFeature getFeature( QgsFeatureId fid )
{
QgsFeature feature;
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
return feature;
}

/**
* Query the provider for the features with the given ids.
* Query the layer for the features with the given ids.
*/
QgsFeatureIterator getFeatures( QgsFeatureIds fids );
inline QgsFeatureIterator getFeatures( const QgsFeatureIds& fids )
{
return getFeatures( QgsFeatureRequest( fids ) );
}

/**
* Query the layer for the features which intersect the specified rectangle.
*/
inline QgsFeatureIterator getFeatures( const QgsRectangle& rectangle )
{
return getFeatures( QgsFeatureRequest( rectangle ) );
}

/** Adds a feature
@param feature feature to add
Expand Down
35 changes: 35 additions & 0 deletions src/core/qgsvectorlayercache.h
Expand Up @@ -155,6 +155,41 @@ class CORE_EXPORT QgsVectorLayerCache : public QObject
*/
QgsFeatureIterator getFeatures( const QgsFeatureRequest& featureRequest = QgsFeatureRequest() );

/**
* Query the layer for features matching a given expression.
*/
inline QgsFeatureIterator getFeatures( const QString& expression )
{
return getFeatures( QgsFeatureRequest( expression ) );
}

/**
* Query the layer for the feature with the given id.
* If there is no such feature, the returned feature will be invalid.
*/
inline QgsFeature getFeature( QgsFeatureId fid )
{
QgsFeature feature;
getFeatures( QgsFeatureRequest( fid ) ).nextFeature( feature );
return feature;
}

/**
* Query the layer for the features with the given ids.
*/
inline QgsFeatureIterator getFeatures( const QgsFeatureIds& fids )
{
return getFeatures( QgsFeatureRequest( fids ) );
}

/**
* Query the layer for the features which intersect the specified rectangle.
*/
inline QgsFeatureIterator getFeatures( const QgsRectangle& rectangle )
{
return getFeatures( QgsFeatureRequest( rectangle ) );
}

/**
* Check if a certain feature id is cached.
* @param fid The feature id to look for
Expand Down
49 changes: 49 additions & 0 deletions tests/src/python/test_qgsvectorlayer.py
Expand Up @@ -74,6 +74,30 @@ def createLayerWithTwoPoints():
return layer


def createLayerWithFivePoints():
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer",
"addfeat", "memory")
pr = layer.dataProvider()
f = QgsFeature()
f.setAttributes(["test", 123])
f.setGeometry(QgsGeometry.fromPoint(QgsPoint(100, 200)))
f2 = QgsFeature()
f2.setAttributes(["test2", 457])
f2.setGeometry(QgsGeometry.fromPoint(QgsPoint(200, 200)))
f3 = QgsFeature()
f3.setAttributes(["test2", 888])
f3.setGeometry(QgsGeometry.fromPoint(QgsPoint(300, 200)))
f4 = QgsFeature()
f4.setAttributes(["test3", -1])
f4.setGeometry(QgsGeometry.fromPoint(QgsPoint(400, 300)))
f5 = QgsFeature()
f5.setAttributes(["test4", 0])
f5.setGeometry(QgsGeometry.fromPoint(QgsPoint(0, 0)))
assert pr.addFeatures([f, f2, f3, f4, f5])
assert layer.featureCount() == 5
return layer


def createJoinLayer():
joinLayer = QgsVectorLayer(
"Point?field=x:string&field=y:integer&field=z:integer",
Expand Down Expand Up @@ -1050,6 +1074,31 @@ def test_getFeatures(self):

self.assertFalse(fi.nextFeature(f))

layer2 = createLayerWithFivePoints()

# getFeature(fid)
feat = layer2.getFeature(4)
self.assertTrue(feat.isValid())
self.assertEqual(feat['fldtxt'], 'test3')
self.assertEqual(feat['fldint'], -1)
feat = layer2.getFeature(10)
self.assertFalse(feat.isValid())

# getFeatures(expression)
it = layer2.getFeatures("fldint <= 0")
fids = [f.id() for f in it]
self.assertEqual(set(fids), set([4, 5]))

# getFeatures(fids)
it = layer2.getFeatures([1, 2])
fids = [f.id() for f in it]
self.assertEqual(set(fids), set([1, 2]))

# getFeatures(rect)
it = layer2.getFeatures(QgsRectangle(99, 99, 201, 201))
fids = [f.id() for f in it]
self.assertEqual(set(fids), set([1, 2]))

def test_join(self):

joinLayer = createJoinLayer()
Expand Down

2 comments on commit c8c623e

@blazek
Copy link
Member

@blazek blazek commented on c8c623e Jul 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it is failing with

In file included from qgis/src/core/symbology-ng/qgscategorizedsymbolrendererv2.cpp:30: qgis/src/core/qgsvectorlayer.h:993:31: error: incomplete result type 'QgsFeatureIterator' in function definition inline QgsFeatureIterator getFeatures( const QString& expression )

etc. etc.

@m-kuhn
Copy link
Member Author

@m-kuhn m-kuhn commented on c8c623e Jul 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry

Please sign in to comment.