Skip to content

Commit

Permalink
Port another alg to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent ab64428 commit d53fa8d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 56 deletions.
83 changes: 45 additions & 38 deletions python/plugins/processing/algs/qgis/AddTableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@
from qgis.core import (QgsField,
QgsFeature,
QgsApplication,
QgsProcessingUtils)
from processing.algs.qgis import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm


class AddTableField(QgisAlgorithm):
Expand Down Expand Up @@ -64,18 +65,19 @@ def __init__(self):
self.tr('Float'),
self.tr('String')]

self.addParameter(ParameterVector(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(ParameterString(self.FIELD_NAME,
self.tr('Field name')))
self.addParameter(ParameterSelection(self.FIELD_TYPE,
self.tr('Field type'), self.type_names))
self.addParameter(ParameterNumber(self.FIELD_LENGTH,
self.tr('Field length'), 1, 255, 10))
self.addParameter(ParameterNumber(self.FIELD_PRECISION,
self.tr('Field precision'), 0, 10, 0))
self.addOutput(OutputVector(
self.OUTPUT_LAYER, self.tr('Added')))
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_LAYER,
self.tr('Input layer')))
self.addParameter(QgsProcessingParameterString(self.FIELD_NAME,
self.tr('Field name')))
self.addParameter(QgsProcessingParameterEnum(self.FIELD_TYPE,
self.tr('Field type'), self.type_names))
self.addParameter(QgsProcessingParameterNumber(self.FIELD_LENGTH,
self.tr('Field length'), QgsProcessingParameterNumber.Integer,
10, False, 1, 255))
self.addParameter(QgsProcessingParameterNumber(self.FIELD_PRECISION,
self.tr('Field precision'), QgsProcessingParameterNumber.Integer, 0, False, 0, 10))
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT_LAYER, self.tr('Added')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr('Added')))

def name(self):
return 'addfieldtoattributestable'
Expand All @@ -84,27 +86,32 @@ def displayName(self):
return self.tr('Add field to attributes table')

def processAlgorithm(self, parameters, context, feedback):
fieldType = self.getParameterValue(self.FIELD_TYPE)
fieldName = self.getParameterValue(self.FIELD_NAME)
fieldLength = self.getParameterValue(self.FIELD_LENGTH)
fieldPrecision = self.getParameterValue(self.FIELD_PRECISION)
output = self.getOutputFromName(self.OUTPUT_LAYER)
source = self.parameterAsSource(parameters, self.INPUT_LAYER, context)

layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
fieldType = self.parameterAsEnum(parameters, self.FIELD_TYPE, context)
fieldName = self.parameterAsString(parameters, self.FIELD_NAME, context)
fieldLength = self.parameterAsInt(parameters, self.FIELD_LENGTH, context)
fieldPrecision = self.parameterAsInt(parameters, self.FIELD_PRECISION, context)

fields = layer.fields()
fields = source.fields()
fields.append(QgsField(fieldName, self.TYPES[fieldType], '',
fieldLength, fieldPrecision))
writer = output.getVectorWriter(fields, layer.wkbType(), layer.crs(), context)
outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(layer, context)
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
for current, feat in enumerate(features):
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT_LAYER, context,
fields, source.wkbType(), source.sourceCrs())

features = source.getFeatures()
total = 100.0 / source.featureCount()

for current, input_feature in enumerate(features):
if feedback.isCanceled():
break

output_feature = input_feature
attributes = input_feature.attributes()
attributes.append(None)
output_feature.setAttributes(attributes)

sink.addFeature(output_feature)
feedback.setProgress(int(current * total))
geom = feat.geometry()
outFeat.setGeometry(geom)
atMap = feat.attributes()
atMap.append(None)
outFeat.setAttributes(atMap)
writer.addFeature(outFeat)
del writer

return {self.OUTPUT_LAYER: dest_id}
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
# from .RasterLayerStatistics import RasterLayerStatistics
# from .StatisticsByCategories import StatisticsByCategories
# from .EquivalentNumField import EquivalentNumField
# from .AddTableField import AddTableField
from .AddTableField import AddTableField
# from .FieldsCalculator import FieldsCalculator
# from .SaveSelectedFeatures import SaveSelectedFeatures
# from .Explode import Explode
Expand Down Expand Up @@ -220,7 +220,7 @@ def getAlgs(self):
# ExtractByAttribute(), SelectByAttribute(), GridPolygon(),
# GridLine(), Gridify(), HubDistancePoints(),
# HubDistanceLines(), HubLines(), Merge(),
# GeometryConvert(), AddTableField(), FieldsCalculator(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# AutoincrementalField(), Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
Expand Down Expand Up @@ -259,7 +259,8 @@ def getAlgs(self):
# FixGeometry(), ExecuteSQL(), FindProjection(),
# TopoColor(), EliminateSelection()
# ]
algs = [Boundary(),
algs = [AddTableField(),
Boundary(),
BoundingBox()]

# if hasPlotly:
Expand Down
15 changes: 15 additions & 0 deletions python/plugins/processing/gui/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,21 @@ def createWidget(self):
widget.addItem(desc, val)
return widget

def showExpressionsBuilder(self):
context = dataobjects.createExpressionContext()
value = self.value()
if not isinstance(value, str):
value = ''
dlg = QgsExpressionBuilderDialog(None, value, self.widget, 'generic', context)
dlg.setWindowTitle(self.tr('Expression based input'))
if dlg.exec_() == QDialog.Accepted:
exp = QgsExpression(dlg.expressionText())
if not exp.hasParserError():
if self.dialogType == DIALOG_STANDARD:
self.setValue(str(exp.evaluate(context)))
else:
self.setValue(dlg.expressionText())

def setValue(self, value):
if self.dialogType == DIALOG_STANDARD:
if self.param.multiLine():
Expand Down
30 changes: 15 additions & 15 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2524,21 +2524,21 @@ tests:
# # name: expected/extract_by_location_within.gml
# # type: vector
#
# - algorithm: qgis:addfieldtoattributestable
# name: add float field
# params:
# FIELD_LENGTH: 10
# FIELD_NAME: field
# FIELD_PRECISION: 2
# FIELD_TYPE: '1'
# INPUT_LAYER:
# name: custom/points.shp
# type: vector
# results:
# OUTPUT_LAYER:
# name: expected/add_field.gml
# type: vector
#
- algorithm: qgis:addfieldtoattributestable
name: add float field
params:
FIELD_LENGTH: 10
FIELD_NAME: field
FIELD_PRECISION: 2
FIELD_TYPE: '1'
INPUT_LAYER:
name: custom/points.shp
type: vector
results:
OUTPUT_LAYER:
name: expected/add_field.gml
type: vector

# - algorithm: qgis:randompointsinextent
# name: Random point in extent, don't check result
# params:
Expand Down

0 comments on commit d53fa8d

Please sign in to comment.