QGIS/python/plugins/sextante/algs/ftools/BasicStatisticsStrings.py

159 lines
5.7 KiB
Python
Raw Normal View History

2012-10-04 19:33:47 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
BasicStatisticsStrings.py
---------------------
Date : September 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__ = 'September 2012'
__copyright__ = '(C) 2012, Victor Olaya'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
import codecs
2012-09-28 15:35:31 +03:00
from PyQt4.QtCore import *
from qgis.core import *
from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.parameters.ParameterVector import ParameterVector
from sextante.parameters.ParameterTableField import ParameterTableField
from sextante.parameters.ParameterBoolean import ParameterBoolean
from sextante.outputs.OutputHTML import OutputHTML
from sextante.outputs.OutputNumber import OutputNumber
from sextante.algs.ftools import FToolsUtils as utils
2012-09-28 15:35:31 +03:00
class BasicStatisticsStrings(GeoAlgorithm):
INPUT_LAYER = "INPUT_LAYER"
FIELD_NAME = "FIELD_NAME"
OUTPUT_HTML_FILE = "OUTPUT_HTML_FILE"
MIN_LEN = "MIN_LEN"
MAX_LEN = "MAX_LEN"
MEAN_LEN = "MEAN_LEN"
COUNT = "COUNT"
EMPTY = "EMPTY"
FILLED = "FILLED"
UNIQUE = "UNIQUE"
#===========================================================================
# def getIcon(self):
# return QtGui.QIcon(os.path.dirname(__file__) + "/icons/basic_statistics.png")
#===========================================================================
2012-09-28 15:35:31 +03:00
def defineCharacteristics(self):
self.name = "Basic statistics for text fields"
self.group = "Vector table tools"
2012-09-28 15:35:31 +03:00
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input vector layer", ParameterVector.VECTOR_TYPE_ANY, False))
self.addParameter(ParameterTableField(self.FIELD_NAME, "Field to calculate statistics on", self.INPUT_LAYER, ParameterTableField.DATA_TYPE_STRING))
self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE, "Statistics for text field"))
self.addOutput(OutputNumber(self.MIN_LEN, "Minimum length"))
self.addOutput(OutputNumber(self.MAX_LEN, "Maximum length"))
self.addOutput(OutputNumber(self.MEAN_LEN, "Mean length"))
self.addOutput(OutputNumber(self.COUNT, "Count"))
self.addOutput(OutputNumber(self.EMPTY, "Number of empty values"))
self.addOutput(OutputNumber(self.FILLED, "Number of non-empty values"))
self.addOutput(OutputNumber(self.UNIQUE, "Number of unique values"))
def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
fieldName = self.getParameterValue(self.FIELD_NAME)
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
index = layer.fieldNameIndex(fieldName)
sumValue = 0
minValue = 0
maxValue = 0
meanValue = 0
countEmpty = 0
countFilled = 0
isFirst = True
values = []
2013-01-01 23:52:00 +01:00
features = QGisLayers.features(layer)
count = len(features)
total = 100.0 / float(count)
current = 0
for ft in features:
length = float(len(ft.attributes()[index].toString()))
2013-01-01 23:52:00 +01:00
if isFirst:
minValue = length
maxValue = length
isFirst = False
else:
if length < minValue:
2012-09-28 15:35:31 +03:00
minValue = length
2013-01-01 23:52:00 +01:00
if length > maxValue:
2012-09-28 15:35:31 +03:00
maxValue = length
2013-01-01 23:52:00 +01:00
if length != 0.00:
countFilled += 1
else:
countEmpty += 1
2012-09-28 15:35:31 +03:00
2013-01-01 23:52:00 +01:00
values.append(length)
sumValue += length
2012-09-28 15:35:31 +03:00
2013-01-01 23:52:00 +01:00
current += 1
progress.setPercentage(int(current * total))
2012-09-28 15:35:31 +03:00
n = float(len(values))
if n > 0:
2013-01-01 23:52:00 +01:00
meanValue = sumValue / n
2012-09-28 15:35:31 +03:00
2013-01-01 23:52:00 +01:00
uniqueValues = utils.getUniqueValuesCount(layer, index)
2012-09-28 15:35:31 +03:00
data = []
data.append("Minimum length: " + unicode(minValue))
data.append("Maximum length: " + unicode(maxValue))
data.append("Mean length: " + unicode(meanValue))
data.append("Filled: " + unicode(countFilled))
data.append("Empty: " + unicode(countEmpty))
data.append("Count: " + unicode(count))
data.append("Unique: " + unicode(uniqueValues))
self.createHTML(outputFile, data)
self.setOutputValue(self.MIN_LEN, minValue)
self.setOutputValue(self.MAX_LEN, maxValue)
self.setOutputValue(self.MEAN_LEN, meanValue)
self.setOutputValue(self.FILLED, countFilled)
self.setOutputValue(self.EMPTY, countEmpty)
self.setOutputValue(self.COUNT, count)
self.setOutputValue(self.UNIQUE, uniqueValues)
def createHTML(self, outputFile, algData):
f = codecs.open(outputFile, "w", encoding="utf-8")
f.write('<html><head>')
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>')
2012-09-28 15:35:31 +03:00
for s in algData:
f.write("<p>" + str(s) + "</p>")
f.write("</body></html>")
2012-09-28 15:35:31 +03:00
f.close()