Skip to content
Permalink
Browse files

Add methods to vectorlayercache and tests

  • Loading branch information
m-kuhn committed Jul 19, 2016
1 parent d91a45c commit c8c623ecee3794636f360fb96883cb5cca33eaf7
@@ -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.
@@ -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
@@ -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]
@@ -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
@@ -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
@@ -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",
@@ -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()

2 comments on commit c8c623e

@blazek

This comment has been minimized.

Copy link
Member

@blazek blazek replied Jul 19, 2016

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

This comment has been minimized.

Copy link
Member Author

@m-kuhn m-kuhn replied Jul 19, 2016

Sorry

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