Skip to content
Permalink
Browse files

[processing] added multiple option to ParameterTableFIeld

removed ParameterTableMultipleField and did some cleanup
  • Loading branch information
volaya committed Oct 5, 2016
1 parent bc06600 commit fe5d0166ccbb3c64589773df47dd11a07d089de3
@@ -36,7 +36,7 @@
class DeleteColumn(GeoAlgorithm):

INPUT = 'INPUT'
COLUMN = 'COLUMN'
COLUMNS = 'COLUMN'
OUTPUT = 'OUTPUT'

def defineCharacteristics(self):
@@ -45,17 +45,20 @@ def defineCharacteristics(self):

self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
self.addParameter(ParameterTableField(self.COLUMN,
self.tr('Field to delete'), self.INPUT))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Deleted column')))
self.addParameter(ParameterTableField(self.COLUMNS,
self.tr('Fields to delete'), self.INPUT, multiple=True))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Output layer')))

def processAlgorithm(self, progress):
layer = dataobjects.getObjectFromUri(
self.getParameterValue(self.INPUT))
idx = layer.fields().lookupField(self.getParameterValue(self.COLUMN))

layer = dataobjects.getObjectFromUri(self.getParameterValue(self.INPUT))

toDelete = self.getParameterValue(self.COLUMNS)
fields = layer.fields()
fields.remove(idx)
idxs = []
for f in toDelete:
idx = layer.fieldNameIndex()
fields.remove(idx)
idxs.append[idx]

writer = self.getOutputFromName(self.OUTPUT).getVectorWriter(fields,
layer.wkbType(), layer.crs())
@@ -67,7 +70,8 @@ def processAlgorithm(self, progress):
for current, f in enumerate(features):
feat.setGeometry(f.geometry())
attributes = f.attributes()
del attributes[idx]
for idx in idxs:
del attributes[idx]
feat.setAttributes(attributes)
writer.addFeature(feat)

@@ -37,7 +37,7 @@
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterTableMultipleField
from processing.core.parameters import ParameterTableField
from processing.core.outputs import OutputVector
from processing.tools import vector, dataobjects

@@ -62,8 +62,8 @@ def defineCharacteristics(self):
[dataobjects.TYPE_VECTOR_POLYGON, dataobjects.TYPE_VECTOR_LINE]))
self.addParameter(ParameterBoolean(Dissolve.DISSOLVE_ALL,
self.tr('Dissolve all (do not use fields)'), True))
self.addParameter(ParameterTableMultipleField(Dissolve.FIELD,
self.tr('Unique ID fields'), Dissolve.INPUT, optional=True))
self.addParameter(ParameterTableField(Dissolve.FIELD,
self.tr('Unique ID fields'), Dissolve.INPUT, optional=True, multiple=True))
self.addOutput(OutputVector(Dissolve.OUTPUT, self.tr('Dissolved')))

def processAlgorithm(self, progress):
@@ -46,7 +46,6 @@
from processing.core.parameters import ParameterBoolean
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterTableMultipleField
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterCrs
from processing.core.parameters import ParameterFile
@@ -328,8 +327,7 @@ def getImportCommands(self):
commands.append(param.name + '= NULL')
else:
commands.append(param.name + ' = "' + param.value + '"')
elif isinstance(param, (ParameterTableField, ParameterTableMultipleField, ParameterString,
ParameterFile)):
elif isinstance(param, (ParameterTableField, ParameterString, ParameterFile)):
if param.value is None:
commands.append(param.name + '= NULL')
else:
@@ -106,10 +106,8 @@ class Parameter:
take as input.
"""

default_metadata = {
'widget_wrapper': 'processing.gui.wrappers.BasicWidgetWrapper'
}

default_metadata = {}

def __init__(self, name='', description='', default=None, optional=False,
metadata={}):
self.name = name
@@ -809,8 +807,7 @@ def setValue(self, n):
if isinstance(n, basestring):
try:
v = self._evaluate(n)
float(v)
self.value = n
self.value = float(v)
return True
except:
return False
@@ -846,8 +843,8 @@ def fromScriptCode(self, line):
default = definition.strip()[len('number') + 1:] or None
return ParameterNumber(name, descName, default=default, optional=isOptional)

def _evaluate(self, v):
exp = QgsExpression(v)
def _evaluate(self):
exp = QgsExpression(self.value)
if exp.hasParserError():
raise ValueError(self.tr("Error in parameter expression: ") + exp.parserErrorString())
result = exp.evaluate(_expressionContext())
@@ -856,8 +853,7 @@ def _evaluate(self, v):
return result

def evaluate(self, alg):
if isinstance(self.value, basestring):
self.value = self._evaluate(self.value)
self.value = self._evaluate(self.value)

def expressionContext(self):
return _expressionContext()
@@ -866,17 +862,12 @@ def getValueAsCommandLineParameter(self):
if self.value is None:
return str(None)
if isinstance(self.value, basestring):
return '"%s"' % self.value
return '"%s"' + self.value
return str(self.value)



class ParameterRange(Parameter):

default_metadata = {
'widget_wrapper': 'processing.gui.wrappers.BasicWidgetWrapper'
}


def __init__(self, name='', description='', default=None, optional=False):
Parameter.__init__(self, name, description, default, optional)
@@ -1226,24 +1217,28 @@ class ParameterTableField(Parameter):
DATA_TYPE_ANY = -1

def __init__(self, name='', description='', parent=None, datatype=-1,
optional=False):
optional=False, multiple = False):
Parameter.__init__(self, name, description, None, optional)
self.parent = parent
self.multiple = True
self.datatype = int(datatype)

def getValueAsCommandLineParameter(self):
return '"' + str(self.value) + '"' if self.value is not None else str(None)

def setValue(self, value):
if value is None:
if not bool(value):
if not self.optional:
return False
self.value = None
return True

elif len(value) == 0 and not self.optional:
return False
self.value = str(value)
if isinstance(value, list):
if not self.multiple and len(value) > 1:
return False
self.value = ";".join(value)
return True
else:
self.value = str(value)
return True

def __str__(self):
@@ -1284,97 +1279,12 @@ def fromScriptCode(self, line):
return ParameterTableField(name, descName, parent, datatype, isOptional)


class ParameterTableMultipleField(Parameter):

"""A parameter representing several table fields.
Its value is a string with items separated by semicolons, each of
which represents the name of each field.
In a script you can use it with
##Fields=[optional] multiple field [number|string] Parentinput
In the batch runner simply use a string with items separated by
semicolons, each of which represents the name of each field.
see algs.qgis.DeleteColumn.py for an usage example
"""

DATA_TYPE_NUMBER = 0
DATA_TYPE_STRING = 1
DATA_TYPE_ANY = -1

def __init__(self, name='', description='', parent=None, datatype=-1,
optional=False):
Parameter.__init__(self, name, description, None, optional)
self.parent = parent
self.datatype = int(datatype)

def getValueAsCommandLineParameter(self):
return '"' + str(self.value) + '"' if self.value is not None else str(None)

def setValue(self, obj):
if obj is None:
if self.optional:
self.value = None
return True
return False

if isinstance(obj, list):
if len(obj) == 0:
if self.optional:
self.value = None
return True
return False
self.value = ";".join(obj)
return True
else:
self.value = str(obj)
return True

def __str__(self):
return self.name + ' <' + self.__module__.split('.')[-1] + ' from ' \
+ self.parent + '>'

def dataType(self):
if self.datatype == self.DATA_TYPE_NUMBER:
return 'numeric'
elif self.datatype == self.DATA_TYPE_STRING:
return 'string'
else:
return 'any'

def getAsScriptCode(self):
param_type = ''
if self.optional:
param_type += 'optional '
param_type += 'multiple field '
return '##' + self.name + '=' + param_type + self.parent

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
if definition.lower().strip().startswith('multiple field'):
descName = _createDescriptiveName(name)
if definition.lower().strip().startswith('multiple field number'):
field = definition.strip()[len('multiple field number') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_NUMBER
elif definition.lower().strip().startswith('multiple field string'):
field = definition.strip()[len('multiple field string') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_STRING
else:
field = definition.strip()[len('multiple field') + 1:]
datatype = ParameterTableMultipleField.DATA_TYPE_ANY

return ParameterTableMultipleField(name, descName, field, datatype, isOptional)


class ParameterVector(ParameterDataObject):

default_metadata = {
'widget_wrapper': 'processing.gui.wrappers.VectorWidgetWrapper'
}


def __init__(self, name='', description='', datatype=[-1],
optional=False):
ParameterDataObject.__init__(self, name, description, None, optional)
@@ -1468,10 +1378,6 @@ def fromScriptCode(self, line):

class ParameterGeometryPredicate(Parameter):

default_metadata = {
'widget_wrapper': 'processing.gui.wrappers.BasicWidgetWrapper'
}

predicates = ('intersects',
'contains',
'disjoint',
@@ -50,7 +50,6 @@
from processing.core.parameters import ParameterFixedTable
from processing.core.parameters import ParameterRange
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterTableMultipleField
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
@@ -42,27 +42,7 @@
from qgis.PyQt.QtGui import QIcon

from processing.gui.OutputSelectionPanel import OutputSelectionPanel
from processing.gui.PointSelectionPanel import PointSelectionPanel
from processing.gui.GeometryPredicateSelectionPanel import \
GeometryPredicateSelectionPanel
from processing.gui.ListMultiselectWidget import ListMultiSelectWidget
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterTable
from processing.core.parameters import ParameterTableField
from processing.core.parameters import ParameterTableMultipleField
from processing.core.parameters import ParameterSelection
from processing.core.parameters import ParameterFixedTable
from processing.core.parameters import ParameterRange
from processing.core.parameters import ParameterMultipleInput
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterExtent
from processing.core.parameters import ParameterFile
from processing.core.parameters import ParameterCrs
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterPoint
from processing.core.parameters import ParameterGeometryPredicate

from processing.core.parameters import ParameterVector, ParameterExtent, ParameterPoint
from processing.core.outputs import OutputRaster
from processing.core.outputs import OutputTable
from processing.core.outputs import OutputVector

0 comments on commit fe5d016

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