Skip to content
Permalink
Browse files
Use context to control vector.features()
  • Loading branch information
nyalldawson committed Apr 26, 2017
1 parent 3f137e1 commit 8cf1617f5d8f739c81a289b37daf7c5cc0115231
Showing with 16 additions and 23 deletions.
  1. +11 −11 python/plugins/processing/tests/ToolsTest.py
  2. +5 −12 python/plugins/processing/tools/vector.py
@@ -60,7 +60,7 @@ def testFeatures(self):
context = QgsProcessingContext()

# disable check for geometry validity
context.setFlags(0)
context.setFlags(QgsProcessingContext.Flags(0))

# test with all features
features = vector.features(test_layer, context)
@@ -75,7 +75,7 @@ def testFeatures(self):
self.assertEqual(set([f.id() for f in features]), set([2, 4, 6]))

# selection, but not using selected features
context.setFlags(0)
context.setFlags(QgsProcessingContext.Flags(0))
test_layer.selectByIds([2, 4, 6])
features = vector.features(test_layer, context)
self.assertEqual(len(features), 9)
@@ -89,7 +89,7 @@ def testFeatures(self):
self.assertEqual(set([f.id() for f in features]), set([0, 1, 2, 3, 4, 5, 6, 7, 8]))

# test that feature request is honored
context.setFlags(0)
context.setFlags(QgsProcessingContext.Flags(0))
features = vector.features(test_layer, context, QgsFeatureRequest().setFilterFids([1, 3, 5]))
self.assertEqual(set([f.id() for f in features]), set([1, 3, 5]))

@@ -112,45 +112,45 @@ def testValues(self):
context = QgsProcessingContext()

# disable check for geometry validity
context.setFlags(0)
context.setFlags(QgsProcessingContext.Flags(0))

test_data = points()
test_layer = QgsVectorLayer(test_data, 'test', 'ogr')

# field by index
res = vector.values(test_layer, context)
res = vector.values(test_layer, context, 1)
self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])

# field by name
res = vector.values(test_layer, context)
res = vector.values(test_layer, context, 'id')
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])

# two fields
res = vector.values(test_layer, context, 2)
res = vector.values(test_layer, context, 1, 2)
self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

# two fields by name
res = vector.values(test_layer, context, 'id2')
res = vector.values(test_layer, context, 'id', 'id2')
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])

# two fields by name and index
res = vector.values(test_layer, context, 2)
res = vector.values(test_layer, context, 'id', 2)
self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])

# test with selected features
context.setFlags(QgsProcessingContext.UseSelection)
test_layer.selectByIds([2, 4, 6])
res = vector.values(test_layer, context)
res = vector.values(test_layer, context, 1)
self.assertEqual(set(res[1]), set([5, 7, 3]))

def testUniqueValues(self):

context = QgsProcessingContext()
# disable check for geometry validity
context.setFlags(0)
context.setFlags(QgsProcessingContext.Flags(0))

test_data = points()
test_layer = QgsVectorLayer(test_data, 'test', 'ogr')
@@ -98,20 +98,13 @@ class Features(object):

DO_NOT_CHECK, IGNORE, RAISE_EXCEPTION = range(3)

def __init__(self, layer, request):
def __init__(self, layer, context, request):
self.layer = layer
self.selection = False
self.selection = context.flags() & QgsProcessingContext.UseSelection and layer.selectedFeatureCount() > 0
request.setInvalidGeometryCheck(context.invalidGeometryCheck())

invalidFeaturesMethod = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
if invalidFeaturesMethod == self.IGNORE:
request.setInvalidGeometryCheck(QgsFeatureRequest.GeometrySkipInvalid)
elif invalidFeaturesMethod == self.RAISE_EXCEPTION:
request.setInvalidGeometryCheck(QgsFeatureRequest.GeometryAbortOnInvalid)

if ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)\
and layer.selectedFeatureCount() > 0:
if self.selection:
self.iter = layer.selectedFeaturesIterator(request)
self.selection = True
else:
self.iter = layer.getFeatures(request)

@@ -131,7 +124,7 @@ def __len__(self):
def tr(self, string):
return QCoreApplication.translate("FeatureIterator", string)

return Features(layer, request)
return Features(layer, context, request)


def uniqueValues(layer, context, attribute):

0 comments on commit 8cf1617

Please sign in to comment.