Skip to content

Commit

Permalink
[processing] Remove more UseSelectionIfPresent use
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent d855225 commit f9887aa
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 31 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/BarPlot.py
Expand Up @@ -81,7 +81,7 @@ def processAlgorithm(self, parameters, context, feedback):


output = self.getOutputValue(self.OUTPUT) 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()] x_var = [i[namefieldname] for i in layer.getFeatures()]


Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/BoxPlot.py
Expand Up @@ -90,7 +90,7 @@ def processAlgorithm(self, parameters, context, feedback):


output = self.getOutputValue(self.OUTPUT) 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()] x_var = [i[namefieldname] for i in layer.getFeatures()]


Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/MeanAndStdDevPlot.py
Expand Up @@ -79,7 +79,7 @@ def processAlgorithm(self, parameters, context, feedback):


output = self.getOutputValue(self.OUTPUT) output = self.getOutputValue(self.OUTPUT)


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


d = {} d = {}
for i in range(len(values[namefieldname])): for i in range(len(values[namefieldname])):
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/qgis/PolarPlot.py
Expand Up @@ -78,7 +78,7 @@ def processAlgorithm(self, parameters, context, feedback):


output = self.getOutputValue(self.OUTPUT) output = self.getOutputValue(self.OUTPUT)


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


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


output = self.getOutputValue(self.OUTPUT) output = self.getOutputValue(self.OUTPUT)


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


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


output = self.getOutputValue(self.OUTPUT) output = self.getOutputValue(self.OUTPUT)


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


output = self.getOutputValue(self.OUTPUT) output = self.getOutputValue(self.OUTPUT)


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


data = [go.Scatter3d( data = [go.Scatter3d(
x=values[xfieldname], x=values[xfieldname],
Expand Down
21 changes: 5 additions & 16 deletions python/plugins/processing/tests/ToolsTest.py
Expand Up @@ -53,43 +53,32 @@ def tearDownClass(cls):
shutil.rmtree(path) shutil.rmtree(path)


def testValues(self): def testValues(self):
context = QgsProcessingContext()

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

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


# field by index # 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]) self.assertEqual(res[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])


# field by name # 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]) self.assertEqual(res['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])


# two fields # 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[1], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0]) self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0])


# two fields by name # 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['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0]) self.assertEqual(res['id2'], [2, 1, 0, 2, 1, 0, 0, 0, 0])


# two fields by name and index # 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['id'], [1, 2, 3, 4, 5, 6, 7, 8, 9])
self.assertEqual(res[2], [2, 1, 0, 2, 1, 0, 0, 0, 0]) 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): def testOgrLayerNameExtraction(self):
outdir = tempfile.mkdtemp() outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir) self.cleanup_paths.append(outdir)
Expand Down
4 changes: 0 additions & 4 deletions python/plugins/processing/tools/dataobjects.py
Expand Up @@ -74,10 +74,6 @@ def createContext():
context = QgsProcessingContext() context = QgsProcessingContext()
context.setProject(QgsProject.instance()) 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) invalid_features_method = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
if invalid_features_method is None: if invalid_features_method is None:
invalid_features_method = QgsFeatureRequest.GeometryAbortOnInvalid invalid_features_method = QgsFeatureRequest.GeometryAbortOnInvalid
Expand Down
7 changes: 3 additions & 4 deletions python/plugins/processing/tools/vector.py
Expand Up @@ -78,8 +78,8 @@ def resolveFieldIndex(layer, attr):
return index return index




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


# convert attribute value to number # convert attribute value to number
Expand Down

0 comments on commit f9887aa

Please sign in to comment.