mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-15 00:04:00 -04:00
[processing] Optimise basic stats numbers algorithm
This commit is contained in:
parent
a927d9743f
commit
e272bb3e9c
@ -31,7 +31,9 @@ import codecs
|
||||
|
||||
from qgis.PyQt.QtGui import QIcon
|
||||
|
||||
from qgis.core import QgsStatisticalSummary
|
||||
from qgis.core import (QgsStatisticalSummary,
|
||||
QgsFeatureRequest,
|
||||
NULL)
|
||||
|
||||
from processing.core.GeoAlgorithm import GeoAlgorithm
|
||||
from processing.core.parameters import ParameterVector
|
||||
@ -107,36 +109,16 @@ class BasicStatisticsNumbers(GeoAlgorithm):
|
||||
|
||||
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
|
||||
|
||||
cvValue = 0
|
||||
minValue = 0
|
||||
maxValue = 0
|
||||
sumValue = 0
|
||||
meanValue = 0
|
||||
medianValue = 0
|
||||
stdDevValue = 0
|
||||
minority = 0
|
||||
majority = 0
|
||||
firstQuartile = 0
|
||||
thirdQuartile = 0
|
||||
nullValues = 0
|
||||
iqr = 0
|
||||
|
||||
values = []
|
||||
|
||||
features = vector.features(layer)
|
||||
request = QgsFeatureRequest().setFlags(QgsFeatureRequest.NoGeometry).setSubsetOfAttributes([fieldName], layer.fields())
|
||||
stat = QgsStatisticalSummary()
|
||||
features = vector.features(layer, request)
|
||||
count = len(features)
|
||||
total = 100.0 / float(count)
|
||||
for current, ft in enumerate(features):
|
||||
value = ft[fieldName]
|
||||
if value or value == 0:
|
||||
values.append(float(value))
|
||||
else:
|
||||
nullValues += 1
|
||||
|
||||
stat.addVariant(ft[fieldName])
|
||||
progress.setPercentage(int(current * total))
|
||||
|
||||
stat = QgsStatisticalSummary()
|
||||
stat.calculate(values)
|
||||
stat.finalize()
|
||||
|
||||
count = stat.count()
|
||||
uniqueValue = stat.variety()
|
||||
@ -147,13 +129,13 @@ class BasicStatisticsNumbers(GeoAlgorithm):
|
||||
meanValue = stat.mean()
|
||||
medianValue = stat.median()
|
||||
stdDevValue = stat.stDev()
|
||||
if meanValue != 0.00:
|
||||
cvValue = stdDevValue / meanValue
|
||||
cvValue = stdDevValue / meanValue if meanValue != 0 else 0
|
||||
minority = stat.minority()
|
||||
majority = stat.majority()
|
||||
firstQuartile = stat.firstQuartile()
|
||||
thirdQuartile = stat.thirdQuartile()
|
||||
iqr = stat.interQuartileRange()
|
||||
nullValues = stat.countMissing()
|
||||
|
||||
data = []
|
||||
data.append(self.tr('Analyzed layer: {}').format(layer.name()))
|
||||
|
Loading…
x
Reference in New Issue
Block a user