2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
EquivalentNumField.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$'
|
|
|
|
|
fix python pep8 warnings and fix some revealed errors
pep8 --ignore=E111,E128,E201,E202,E203,E211,E221,E222,E225,E226,E227,E231,E241,E261,E265,E272,E302,E303,E501,E701 \
--exclude="ui_*.py,debian/*,python/ext-libs/*" \
.
2015-02-01 14:15:42 +01:00
|
|
|
from PyQt4.QtCore import QVariant
|
|
|
|
from qgis.core import QgsField, QgsFeature, QgsGeometry
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.parameters import ParameterTableField
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2012-11-12 08:56:26 +01:00
|
|
|
class EquivalentNumField(GeoAlgorithm):
|
2012-09-15 18:25:25 +03:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
|
|
|
OUTPUT = 'OUTPUT'
|
|
|
|
FIELD = 'FIELD'
|
2012-09-15 18:25:25 +03:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2012-11-12 08:56:26 +01:00
|
|
|
fieldname = self.getParameterValue(self.FIELD)
|
2012-09-15 18:25:25 +03:00
|
|
|
output = self.getOutputFromName(self.OUTPUT)
|
2013-10-01 20:52:22 +03:00
|
|
|
vlayer = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(self.INPUT))
|
2012-09-15 18:25:25 +03:00
|
|
|
vprovider = vlayer.dataProvider()
|
2013-02-07 01:09:39 +01:00
|
|
|
fieldindex = vlayer.fieldNameIndex(fieldname)
|
2012-09-15 18:25:25 +03:00
|
|
|
fields = vprovider.fields()
|
2013-10-01 20:52:22 +03:00
|
|
|
fields.append(QgsField('NUM_FIELD', QVariant.Int))
|
|
|
|
writer = output.getVectorWriter(fields, vprovider.geometryType(),
|
2015-08-22 14:29:41 +02:00
|
|
|
vlayer.crs())
|
2012-09-15 18:25:25 +03:00
|
|
|
outFeat = QgsFeature()
|
2013-02-07 01:09:39 +01:00
|
|
|
inGeom = QgsGeometry()
|
2012-09-15 18:25:25 +03:00
|
|
|
nElement = 0
|
|
|
|
classes = {}
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(vlayer)
|
2013-02-04 00:14:39 +01:00
|
|
|
nFeat = len(features)
|
2012-12-24 00:03:30 +01:00
|
|
|
for feature in features:
|
2013-10-01 20:52:22 +03:00
|
|
|
progress.setPercentage(int(100 * nElement / nFeat))
|
2013-02-04 00:14:39 +01:00
|
|
|
nElement += 1
|
|
|
|
inGeom = feature.geometry()
|
2013-10-01 20:52:22 +03:00
|
|
|
outFeat.setGeometry(inGeom)
|
2013-02-04 00:14:39 +01:00
|
|
|
atMap = feature.attributes()
|
2013-06-03 21:25:22 +02:00
|
|
|
clazz = atMap[fieldindex]
|
2013-02-04 00:14:39 +01:00
|
|
|
if clazz not in classes:
|
|
|
|
classes[clazz] = len(classes.keys())
|
2013-06-03 21:25:22 +02:00
|
|
|
atMap.append(classes[clazz])
|
2013-02-07 01:09:39 +01:00
|
|
|
outFeat.setAttributes(atMap)
|
2013-10-01 20:52:22 +03:00
|
|
|
writer.addFeature(outFeat)
|
2012-09-15 18:25:25 +03:00
|
|
|
del writer
|
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2015-10-10 10:02:41 +02:00
|
|
|
self.name, self.i18n_name = self.trAlgorithm('Add unique value index field')
|
2015-07-26 03:48:27 +02:00
|
|
|
self.group, self.i18n_group = self.trAlgorithm('Vector table tools')
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_ANY]))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Class field'), self.INPUT))
|
2015-10-10 10:02:41 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT, self.tr('Layer with index field')))
|