Skip to content

Commit

Permalink
Port addautoincrementalfield to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent dfb687b commit f0f0411
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 38 deletions.
58 changes: 33 additions & 25 deletions python/plugins/processing/algs/qgis/AutoincrementalField.py
Expand Up @@ -29,17 +29,27 @@
from qgis.core import (QgsField,
QgsFeature,
QgsApplication,
QgsProcessingUtils)
QgsProcessingUtils,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterFeatureSink,
QgsProcessingOutputVectorLayer)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.outputs import OutputVector


class AutoincrementalField(QgisAlgorithm):

INPUT = 'INPUT'
OUTPUT = 'OUTPUT'

def __init__(self):
super().__init__()

self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer')))

self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Incremented')))
self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT, self.tr('Incremented')))

def icon(self):
return QgsApplication.getThemeIcon("/providerQgis.svg")

Expand All @@ -49,34 +59,32 @@ def svgIconPath(self):
def group(self):
return self.tr('Vector table tools')

def __init__(self):
super().__init__()
self.addParameter(ParameterVector(self.INPUT,
self.tr('Input layer')))
self.addOutput(OutputVector(self.OUTPUT, self.tr('Incremented')))

def name(self):
return 'addautoincrementalfield'

def displayName(self):
return self.tr('Add autoincremental field')

def processAlgorithm(self, parameters, context, feedback):
output = self.getOutputFromName(self.OUTPUT)
vlayer = \
QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT), context)
fields = vlayer.fields()
source = self.parameterAsSource(parameters, self.INPUT, context)
fields = source.fields()
fields.append(QgsField('AUTO', QVariant.Int))
writer = output.getVectorWriter(fields, vlayer.wkbType(), vlayer.crs(), context)
outFeat = QgsFeature()
features = QgsProcessingUtils.getFeatures(vlayer, context)
total = 100.0 / QgsProcessingUtils.featureCount(vlayer, context)
for current, feat in enumerate(features):

(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, 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(current)
output_feature.setAttributes(attributes)

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

return {self.OUTPUT: dest_id}
5 changes: 3 additions & 2 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -108,7 +108,7 @@
# from .FieldsCalculator import FieldsCalculator
# from .SaveSelectedFeatures import SaveSelectedFeatures
# from .Explode import Explode
# from .AutoincrementalField import AutoincrementalField
from .AutoincrementalField import AutoincrementalField
# from .FieldPyculator import FieldsPyculator
# from .JoinAttributes import JoinAttributes
# from .CreateConstantRaster import CreateConstantRaster
Expand Down Expand Up @@ -222,7 +222,7 @@ def getAlgs(self):
# HubDistanceLines(), HubLines(), Merge(),
# GeometryConvert(), FieldsCalculator(),
# SaveSelectedFeatures(), JoinAttributes(),
# AutoincrementalField(), Explode(), FieldsPyculator(),
# Explode(), FieldsPyculator(),
# EquivalentNumField(), PointsLayerFromTable(),
# StatisticsByCategories(), ConcaveHull(),
# RasterLayerStatistics(), PointsDisplacement(),
Expand Down Expand Up @@ -261,6 +261,7 @@ def getAlgs(self):
# ]
algs = [AddTableField(),
Aspect(),
AutoincrementalField(),
Boundary(),
BoundingBox()]

Expand Down
22 changes: 11 additions & 11 deletions python/plugins/processing/tests/testdata/qgis_algorithm_tests.yaml
Expand Up @@ -242,17 +242,17 @@ tests:
# geometry:
# precision: 7
#
# - algorithm: qgis:addautoincrementalfield
# name: Add autoincremental field
# params:
# INPUT:
# name: points.gml
# type: vector
# results:
# OUTPUT:
# name: expected/autoincrement_field.gml
# type: vector
#
- algorithm: qgis:addautoincrementalfield
name: Add autoincremental field
params:
INPUT:
name: points.gml
type: vector
results:
OUTPUT:
name: expected/autoincrement_field.gml
type: vector

# - algorithm: qgis:dissolve
# name: Dissolve using field
# params:
Expand Down

0 comments on commit f0f0411

Please sign in to comment.