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
|
|
|
|
2017-07-15 22:07:12 +10:00
|
|
|
from qgis.core import (QgsCoordinateReferenceSystem,
|
|
|
|
QgsWkbTypes,
|
|
|
|
QgsFeature,
|
|
|
|
QgsFeatureSink,
|
|
|
|
QgsFields,
|
|
|
|
QgsProcessingParameterField,
|
|
|
|
QgsProcessingParameterFeatureSource,
|
|
|
|
QgsProcessingParameterFeatureSink,
|
|
|
|
QgsProcessingOutputNumber,
|
|
|
|
QgsProcessingOutputString,
|
|
|
|
QgsProcessingParameterFileDestination,
|
|
|
|
QgsProcessingOutputHtml)
|
2017-04-26 09:39:40 +10:00
|
|
|
|
2017-06-06 13:41:42 +10:00
|
|
|
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
|
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
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class UniqueValues(QgisAlgorithm):
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-07-15 22:07:12 +10:00
|
|
|
INPUT = 'INPUT'
|
2013-10-01 20:52:22 +03:00
|
|
|
FIELD_NAME = 'FIELD_NAME'
|
|
|
|
TOTAL_VALUES = 'TOTAL_VALUES'
|
|
|
|
UNIQUE_VALUES = 'UNIQUE_VALUES'
|
|
|
|
OUTPUT = 'OUTPUT'
|
2017-07-15 22:07:12 +10:00
|
|
|
OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE'
|
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):
|
2017-08-22 23:36:42 +10:00
|
|
|
return self.tr('Vector analysis')
|
2017-03-29 12:04:09 +10:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-07-10 16:31:14 +10:00
|
|
|
|
|
|
|
def initAlgorithm(self, config=None):
|
2017-07-15 22:07:12 +10:00
|
|
|
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
|
|
|
|
self.tr('Input layer')))
|
|
|
|
self.addParameter(QgsProcessingParameterField(self.FIELD_NAME,
|
|
|
|
self.tr('Target field'),
|
|
|
|
parentLayerParameterName=self.INPUT, type=QgsProcessingParameterField.Any))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFeatureSink(self.OUTPUT, self.tr('Unique values'), optional=True, defaultValue=''))
|
|
|
|
|
|
|
|
self.addParameter(QgsProcessingParameterFileDestination(self.OUTPUT_HTML_FILE, self.tr('HTML report'), self.tr('HTML files (*.html)'), None, True))
|
|
|
|
self.addOutput(QgsProcessingOutputHtml(self.OUTPUT_HTML_FILE, self.tr('HTML report')))
|
|
|
|
self.addOutput(QgsProcessingOutputNumber(self.TOTAL_VALUES, self.tr('Total unique values')))
|
|
|
|
self.addOutput(QgsProcessingOutputString(self.UNIQUE_VALUES, self.tr('Unique values')))
|
2012-08-04 19:48:25 +02:00
|
|
|
|
2017-05-15 13:40:38 +10:00
|
|
|
def name(self):
|
|
|
|
return 'listuniquevalues'
|
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('List unique values')
|
|
|
|
|
2017-05-15 16:19:46 +10:00
|
|
|
def processAlgorithm(self, parameters, context, feedback):
|
2017-07-15 22:07:12 +10:00
|
|
|
source = self.parameterAsSource(parameters, self.INPUT, context)
|
|
|
|
field_name = self.parameterAsString(parameters, self.FIELD_NAME, context)
|
|
|
|
values = source.uniqueValues(source.fields().lookupField(field_name))
|
|
|
|
|
|
|
|
fields = QgsFields()
|
|
|
|
field = source.fields()[source.fields().lookupField(field_name)]
|
|
|
|
field.setName('VALUES')
|
|
|
|
fields.append(field)
|
|
|
|
(sink, dest_id) = self.parameterAsSink(parameters, self.OUTPUT, context,
|
|
|
|
fields, QgsWkbTypes.NoGeometry, QgsCoordinateReferenceSystem())
|
|
|
|
results = {}
|
|
|
|
if sink:
|
|
|
|
for value in values:
|
|
|
|
if feedback.isCanceled():
|
|
|
|
break
|
|
|
|
|
|
|
|
f = QgsFeature()
|
|
|
|
f.setAttributes([value])
|
|
|
|
sink.addFeature(f, QgsFeatureSink.FastInsert)
|
|
|
|
results[self.OUTPUT] = dest_id
|
|
|
|
|
|
|
|
output_file = self.parameterAsFileOutput(parameters, self.OUTPUT_HTML_FILE, context)
|
|
|
|
if output_file:
|
|
|
|
self.createHTML(output_file, values)
|
|
|
|
results[self.OUTPUT_HTML_FILE] = output_file
|
|
|
|
|
|
|
|
results[self.TOTAL_VALUES] = len(values)
|
|
|
|
results[self.UNIQUE_VALUES] = ';'.join([str(v) for v in
|
|
|
|
values])
|
|
|
|
return results
|
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>')
|