Skip to content

Commit a58085b

Browse files
committed
[processing] allow selection parameters with option populated from data source
1 parent 8c8a123 commit a58085b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

python/plugins/processing/core/parameters.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* *
1818
***************************************************************************
1919
"""
20+
from processing.tools.vector import resolveFieldIndex, features
2021

2122
__author__ = 'Victor Olaya'
2223
__date__ = 'August 2012'
@@ -536,10 +537,21 @@ def getFileFilter(self):
536537

537538
class ParameterSelection(Parameter):
538539

539-
def __init__(self, name='', description='', options=[], default=0):
540+
def __init__(self, name='', description='', options=[], default=0, isSource = False):
540541
Parameter.__init__(self, name, description)
541542
self.options = options
542-
if isinstance(self.options, basestring):
543+
if isSource:
544+
self.options = []
545+
layer = QgsVectorLayer(options[0], "layer", "ogr")
546+
if layer.isValid():
547+
try:
548+
index = resolveFieldIndex(layer, options[1])
549+
feats = features(layer)
550+
for feature in feats:
551+
self.options.append(unicode(feature.attributes()[index]))
552+
except ValueError:
553+
pass
554+
elif isinstance(self.options, basestring):
543555
self.options = self.options.split(";")
544556
self.value = None
545557
self.default = int(default)

python/plugins/processing/script/ScriptAlgorithm.py

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def processParameterLine(self, line):
169169
param = ParameterMultipleInput(tokens[0], desc,
170170
ParameterMultipleInput.TYPE_VECTOR_ANY)
171171
param.optional = False
172+
elif tokens[1].lower().strip().startswith('selectionfromfile'):
173+
options = tokens[1].strip()[len('selectionfromfile '):].split(';')
174+
param = ParameterSelection(tokens[0], desc, options, isSource=True)
172175
elif tokens[1].lower().strip().startswith('selection'):
173176
options = tokens[1].strip()[len('selection '):].split(';')
174177
param = ParameterSelection(tokens[0], desc, options)

0 commit comments

Comments
 (0)