2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
UniqueValues.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. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
2016-09-21 18:24:26 +02:00
|
|
|
from builtins import str
|
2012-10-04 19:33:47 +02:00
|
|
|
|
|
|
|
__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-01-25 15:42:11 +02:00
|
|
|
import os
|
2012-09-28 14:39:13 +03:00
|
|
|
import codecs
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2016-04-22 10:38:48 +02:00
|
|
|
from qgis.PyQt.QtGui import QIcon
|
2016-01-25 15:42:11 +02:00
|
|
|
|
2013-08-12 20:44:27 +02: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 OutputHTML
|
|
|
|
from processing.core.outputs import OutputNumber
|
|
|
|
from processing.core.outputs import OutputString
|
2014-03-25 15:55:00 +02:00
|
|
|
from processing.tools import dataobjects, vector
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2016-01-25 15:42:11 +02:00
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
class UniqueValues(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
FIELD_NAME = 'FIELD_NAME'
|
|
|
|
TOTAL_VALUES = 'TOTAL_VALUES'
|
|
|
|
UNIQUE_VALUES = 'UNIQUE_VALUES'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-01-25 15:42:11 +02:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'unique.png'))
|
|
|
|
|
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 'listuniquevalues'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('List unique values')
|
|
|
|
|
2012-08-04 19:48:25 +02:00
|
|
|
def defineCharacteristics(self):
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer')))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterTableField(self.FIELD_NAME,
|
2015-08-22 14:29:41 +02:00
|
|
|
self.tr('Target field'),
|
|
|
|
self.INPUT_LAYER, ParameterTableField.DATA_TYPE_ANY))
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputHTML(self.OUTPUT, self.tr('Unique values')))
|
|
|
|
self.addOutput(OutputNumber(self.TOTAL_VALUES, self.tr('Total unique values')))
|
|
|
|
self.addOutput(OutputString(self.UNIQUE_VALUES, self.tr('Unique values')))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-04-05 18:35:55 +10:00
|
|
|
layer = dataobjects.getLayerFromString(self.getParameterValue(self.INPUT_LAYER))
|
2012-09-28 14:39:13 +03:00
|
|
|
fieldName = self.getParameterValue(self.FIELD_NAME)
|
|
|
|
outputFile = self.getOutputValue(self.OUTPUT)
|
2017-04-25 16:05:29 +10:00
|
|
|
values = vector.getUniqueValues(layer, context, layer.fields().lookupField(fieldName))
|
2012-08-04 19:48:25 +02:00
|
|
|
self.createHTML(outputFile, values)
|
2012-09-28 14:39:13 +03:00
|
|
|
self.setOutputValue(self.TOTAL_VALUES, len(values))
|
2016-09-21 18:24:26 +02:00
|
|
|
self.setOutputValue(self.UNIQUE_VALUES, ';'.join([str(v) for v in
|
2016-01-30 09:33:24 +11:00
|
|
|
values]))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
|
|
|
def createHTML(self, outputFile, algData):
|
2016-11-07 10:35:15 +10:00
|
|
|
with codecs.open(outputFile, 'w', encoding='utf-8') as f:
|
|
|
|
f.write('<html><head>')
|
|
|
|
f.write('<meta http-equiv="Content-Type" content="text/html; \
|
|
|
|
charset=utf-8" /></head><body>')
|
|
|
|
f.write(self.tr('<p>Total unique values: ') + str(len(algData)) + '</p>')
|
|
|
|
f.write(self.tr('<p>Unique values:</p>'))
|
|
|
|
f.write('<ul>')
|
|
|
|
for s in algData:
|
|
|
|
f.write('<li>' + str(s) + '</li>')
|
|
|
|
f.write('</ul></body></html>')
|