Skip to content
Permalink
Browse files

Port addautoincrementalfield to new API

  • Loading branch information
nyalldawson committed Jun 6, 2017
1 parent dfb687b commit f0f0411e7be0cb1a54e6f51b9628b93a58872bd3
@@ -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")

@@ -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}
@@ -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
@@ -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(),
@@ -261,6 +261,7 @@ def getAlgs(self):
# ]
algs = [AddTableField(),
Aspect(),
AutoincrementalField(),
Boundary(),
BoundingBox()]

@@ -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:

0 comments on commit f0f0411

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