2012-10-04 19:33:47 +02:00
|
|
|
# -*- 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'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtCore import QVariant
|
2017-03-29 10:42:42 +10:00
|
|
|
from qgis.core import (QgsField,
|
|
|
|
QgsFeature,
|
2017-06-23 14:34:38 +10:00
|
|
|
QgsFeatureSink,
|
2017-04-25 17:53:32 +10:00
|
|
|
QgsApplication,
|
2017-06-06 14:46:55 +10:00
|
|
|
QgsProcessingUtils,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputVectorLayer)
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class AutoincrementalField(QgisAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2017-06-06 14:46:55 +10:00
|
|
|
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')))
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.source = None
|
|
|
|
self.sink = None
|
|
|
|
self.dest_id = None
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector table tools')
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'addautoincrementalfield'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Add autoincremental field')
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def prepareAlgorithm(self, parameters, context, feedback):
|
|
|
|
self.source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
fields = self.source.fields()
|
2013-10-01 20:52:22 +03:00
|
|
|
fields.append(QgsField('AUTO', QVariant.Int))
|
2017-06-06 14:46:55 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
(self.sink, self.dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, self.source.wkbType(), self.source.sourceCrs())
|
|
|
|
return True
|
2017-06-06 14:46:55 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
|
|
|
features = self.source.getFeatures()
|
|
|
|
total = 100.0 / self.source.featureCount() if self.source.featureCount() else 0
|
2017-06-06 14:46:55 +10:00
|
|
|
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)
|
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
self.sink.addFeature(output_feature, QgsFeatureSink.FastInsert)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2017-06-28 19:55:08 +10:00
|
|
|
return True
|
2017-06-06 14:46:55 +10:00
|
|
|
|
2017-06-28 19:55:08 +10:00
|
|
|
def postProcessAlgorithm(self, context, feedback):
|
|
|
|
return {self.OUTPUT: self.dest_id}
|