Skip to content
Permalink
Browse files

[processing] Remove more UseSelectionIfPresent use

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent d855225 commit f9887aabf51e433f73e05ebfbbe920f3041319f7
@@ -81,7 +81,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, valuefieldname)
values = vector.values(layer, valuefieldname)

x_var = [i[namefieldname] for i in layer.getFeatures()]

@@ -90,7 +90,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, valuefieldname)
values = vector.values(layer, valuefieldname)

x_var = [i[namefieldname] for i in layer.getFeatures()]

@@ -79,7 +79,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, namefieldname, valuefieldname)
values = vector.values(layer, namefieldname, valuefieldname)

d = {}
for i in range(len(values[namefieldname])):
@@ -78,7 +78,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, valuefieldname)
values = vector.values(layer, valuefieldname)

data = [go.Area(r=values[valuefieldname],
t=np.degrees(np.arange(0.0, 2 * np.pi, 2 * np.pi / len(values[valuefieldname]))))]
@@ -79,7 +79,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, fieldname)
values = vector.values(layer, fieldname)

data = [go.Histogram(x=values[fieldname],
nbinsx=bins)]
@@ -82,7 +82,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, xfieldname, yfieldname)
values = vector.values(layer, xfieldname, yfieldname)
data = [go.Scatter(x=values[xfieldname],
y=values[yfieldname],
mode='markers')]
@@ -89,7 +89,7 @@ def processAlgorithm(self, parameters, context, feedback):

output = self.getOutputValue(self.OUTPUT)

values = vector.values(layer, context, xfieldname, yfieldname, zfieldname)
values = vector.values(layer, xfieldname, yfieldname, zfieldname)

data = [go.Scatter3d(
x=values[xfieldname],
@@ -53,43 +53,32 @@ def tearDownClass(cls):
shutil.rmtree(path)

def testValues(self):
context = QgsProcessingContext()

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

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

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

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

# two fields
res = vector.values(test_layer, context, 1, 2)
res = vector.values(test_layer, 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, 'id', 'id2')
res = vector.values(test_layer, '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, 'id', 2)
res = vector.values(test_layer, '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.UseSelectionIfPresent)
test_layer.selectByIds([2, 4, 6])
res = vector.values(test_layer, context, 1)
self.assertEqual(set(res[1]), set([5, 7, 3]))

def testOgrLayerNameExtraction(self):
outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir)
@@ -74,10 +74,6 @@ def createContext():
context = QgsProcessingContext()
context.setProject(QgsProject.instance())

use_selection = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
if use_selection:
context.setFlags(QgsProcessingContext.UseSelectionIfPresent)

invalid_features_method = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
if invalid_features_method is None:
invalid_features_method = QgsFeatureRequest.GeometryAbortOnInvalid
@@ -78,8 +78,8 @@ def resolveFieldIndex(layer, attr):
return index


def values(layer, context, *attributes):
"""Returns the values in the attributes table of a vector layer,
def values(source, *attributes):
"""Returns the values in the attributes table of a feature source,
for the passed fields.
Field can be passed as field names or as zero-based field indices.
@@ -88,7 +88,6 @@ def values(layer, context, *attributes):
It assummes fields are numeric or contain values that can be parsed
to a number.
:param context:
"""
ret = {}
indices = []
@@ -101,7 +100,7 @@ def values(layer, context, *attributes):
# use an optimised feature request
request = QgsFeatureRequest().setSubsetOfAttributes(indices).setFlags(QgsFeatureRequest.NoGeometry)

for feature in QgsProcessingUtils.getFeatures(layer, context, request):
for feature in source.getFeatures(request):
for i in indices:

# convert attribute value to number

0 comments on commit f9887aa

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