# -*- coding: utf-8 -*- """ *************************************************************************** AutoincrementalField.py --------------------- Date : August 2012 Copyright : (C) 2012 by Victor Olaya Email : volayaf at gmail dot com *************************************************************************** * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * *************************************************************************** """ __author__ = 'Victor Olaya' __date__ = 'August 2012' __copyright__ = '(C) 2012, Victor Olaya' # This will get replaced with a git SHA1 when you do a git archive __revision__ = '$Format:%H$' from qgis.PyQt.QtCore import QVariant from qgis.core import (QgsField, QgsFeature, QgsFeatureSink, QgsApplication, QgsProcessingUtils, QgsProcessingParameterFeatureSource, QgsProcessingParameterFeatureSink, QgsProcessingOutputVectorLayer) from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm 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 group(self): return self.tr('Vector table tools') def name(self): return 'addautoincrementalfield' def displayName(self): return self.tr('Add autoincremental field') def processAlgorithm(self, parameters, context, feedback): source = self.parameterAsSource(parameters, self.INPUT, context) fields = source.fields() fields.append(QgsField('AUTO', QVariant.Int)) (sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context, fields, source.wkbType(), source.sourceCrs()) features = source.getFeatures() total = 100.0 / source.featureCount() if source.featureCount() else 0 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, QgsFeatureSink.FastInsert) feedback.setProgress(int(current * total)) return {self.OUTPUT: dest_id}