Skip to content
Permalink
Browse files
Add context argument to other vector methods
  • Loading branch information
nyalldawson committed Apr 26, 2017
1 parent adc7cf0 commit 3f137e123d33fac181e2b656a14c75d074effd04
@@ -81,7 +81,7 @@ def processAlgorithm(self, context, feedback):

output = self.getOutputValue(self.OUTPUT)

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

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

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

output = self.getOutputValue(self.OUTPUT)

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

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

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

output = self.getOutputValue(self.OUTPUT)

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

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

output = self.getOutputValue(self.OUTPUT)

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

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

features = vector.features(layer, context)
featureCount = len(features)
unique = vector.getUniqueValues(layer, index)
unique = vector.getUniqueValues(layer, context, index)
value = int(self.getParameterValue(self.NUMBER))
if method == 0:
if value > featureCount:
@@ -90,7 +90,7 @@ def processAlgorithm(self, context, feedback):
layer.removeSelection()
index = layer.fields().lookupField(field)

unique = vector.getUniqueValues(layer, index)
unique = vector.getUniqueValues(layer, context, index)
featureCount = layer.featureCount()

value = int(self.getParameterValue(self.NUMBER))
@@ -76,7 +76,7 @@ def processAlgorithm(self, context, feedback):
layer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT_LAYER))
fieldName = self.getParameterValue(self.FIELD_NAME)
outputFile = self.getOutputValue(self.OUTPUT)
values = vector.getUniqueValues(layer, layer.fields().lookupField(fieldName))
values = vector.getUniqueValues(layer, context, layer.fields().lookupField(fieldName))
self.createHTML(outputFile, values)
self.setOutputValue(self.TOTAL_VALUES, len(values))
self.setOutputValue(self.UNIQUE_VALUES, ';'.join([str(v) for v in
@@ -78,7 +78,7 @@ def processAlgorithm(self, context, feedback):

output = self.getOutputValue(self.OUTPUT)

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

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

output = self.getOutputValue(self.OUTPUT)

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

output = self.getOutputValue(self.OUTPUT)

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

data = [go.Scatter3d(
x=values[xfieldname],
@@ -74,7 +74,7 @@ def processAlgorithm(self, context, feedback):
mkdir(directory)

fieldIndex = layer.fields().lookupField(fieldName)
uniqueValues = vector.uniqueValues(layer, fieldIndex)
uniqueValues = vector.uniqueValues(layer, context, fieldIndex)
baseName = os.path.join(directory, '{0}_{1}'.format(layer.name(), fieldName))

fields = layer.fields()
@@ -34,7 +34,6 @@
QgsProcessingContext)
from qgis.testing import start_app, unittest

from processing.core.ProcessingConfig import ProcessingConfig
from processing.tests.TestData import points
from processing.tools import vector

@@ -55,8 +54,6 @@ def tearDownClass(cls):
shutil.rmtree(path)

def testFeatures(self):
ProcessingConfig.initialize()

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

@@ -112,79 +109,69 @@ def testFeatures(self):
# feats = [f for f in features]

def testValues(self):
ProcessingConfig.initialize()
context = QgsProcessingContext()

# disable check for geometry validity
prevInvalidGeoms = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, 0)
context.setFlags(0)

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

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

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

# two fields
res = vector.values(test_layer, 1, 2)
res = vector.values(test_layer, context, 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, 'id', 'id2')
res = vector.values(test_layer, context, '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, 'id', 2)
res = vector.values(test_layer, context, 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
previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
context.setFlags(QgsProcessingContext.UseSelection)
test_layer.selectByIds([2, 4, 6])
res = vector.values(test_layer, 1)
res = vector.values(test_layer, context)
self.assertEqual(set(res[1]), set([5, 7, 3]))

ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, prevInvalidGeoms)

def testUniqueValues(self):
ProcessingConfig.initialize()

context = QgsProcessingContext()
# disable check for geometry validity
prevInvalidGeoms = ProcessingConfig.getSetting(ProcessingConfig.FILTER_INVALID_GEOMETRIES)
ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, 0)
context.setFlags(0)

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

# field by index
v = vector.uniqueValues(test_layer, 2)
v = vector.uniqueValues(test_layer, context, 2)
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([2, 1, 0]))

# field by name
v = vector.uniqueValues(test_layer, 'id2')
v = vector.uniqueValues(test_layer, context, 'id2')
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([2, 1, 0]))

# test with selected features
previous_value = ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED)
ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, True)
context.setFlags(QgsProcessingContext.UseSelection)
test_layer.selectByIds([2, 4, 6])
v = vector.uniqueValues(test_layer, 'id')
v = vector.uniqueValues(test_layer, context, 'id')
self.assertEqual(len(v), len(set(v)))
self.assertEqual(set(v), set([5, 7, 3]))

ProcessingConfig.setSettingValue(ProcessingConfig.USE_SELECTED, previous_value)
ProcessingConfig.setSettingValue(ProcessingConfig.FILTER_INVALID_GEOMETRIES, prevInvalidGeoms)

def testOgrLayerNameExtraction(self):
outdir = tempfile.mkdtemp()
self.cleanup_paths.append(outdir)
@@ -44,7 +44,8 @@
from qgis.core import (QgsFields, QgsField, QgsGeometry, QgsRectangle, QgsWkbTypes,
QgsSpatialIndex, QgsProject, QgsMapLayer, QgsVectorLayer,
QgsVectorFileWriter, QgsDistanceArea, QgsDataSourceUri, QgsCredentials,
QgsFeatureRequest, QgsSettings)
QgsFeatureRequest, QgsSettings,
QgsProcessingContext)

from processing.core.ProcessingConfig import ProcessingConfig
from processing.core.ProcessingLog import ProcessingLog
@@ -133,15 +134,16 @@ def tr(self, string):
return Features(layer, request)


def uniqueValues(layer, attribute):
def uniqueValues(layer, context, attribute):
"""Returns a list of unique values for a given attribute.
Attribute can be defined using a field names or a zero-based
field index. It considers the existing selection.
:param context:
"""

fieldIndex = resolveFieldIndex(layer, attribute)
if ProcessingConfig.getSetting(ProcessingConfig.USE_SELECTED) \
if context.flags() & QgsProcessingContext.UseSelection \
and layer.selectedFeatureCount() > 0:

# iterate through selected features
@@ -178,7 +180,7 @@ def resolveFieldIndex(layer, attr):
return index


def values(layer, *attributes):
def values(layer, context, *attributes):
"""Returns the values in the attributes table of a vector layer,
for the passed fields.
@@ -188,6 +190,7 @@ def values(layer, *attributes):
It assummes fields are numeric or contain values that can be parsed
to a number.
:param context:
"""
ret = {}
indices = []
@@ -341,7 +344,7 @@ def simpleMeasure(geom, method=0, ellips=None, crs=None):
return (attr1, attr2)


def getUniqueValues(layer, fieldIndex):
def getUniqueValues(layer, context, fieldIndex):
values = []
feats = features(layer, context)
for feat in feats:
@@ -350,10 +353,6 @@ def getUniqueValues(layer, fieldIndex):
return values


def getUniqueValuesCount(layer, fieldIndex):
return len(getUniqueValues(layer, fieldIndex))


def combineVectorFields(layerA, layerB):
"""Create single field map from two input field maps.
"""

0 comments on commit 3f137e1

Please sign in to comment.