2013-02-16 00:23:56 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
RasterLayerStatistics.py
|
|
|
|
---------------------
|
|
|
|
Date : January 2013
|
|
|
|
Copyright : (C) 2013 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__ = 'January 2013'
|
|
|
|
__copyright__ = '(C) 2013, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import math
|
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
|
|
|
|
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 ParameterRaster
|
|
|
|
from processing.core.outputs import OutputNumber
|
|
|
|
from processing.core.outputs import OutputHTML
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects
|
|
|
|
from processing.tools import raster
|
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
|
|
|
|
class RasterLayerStatistics(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT = 'INPUT'
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
MIN = 'MIN'
|
|
|
|
MAX = 'MAX'
|
|
|
|
SUM = 'SUM'
|
|
|
|
MEAN = 'MEAN'
|
|
|
|
COUNT = 'COUNT'
|
|
|
|
NO_DATA_COUNT = 'NO_DATA_COUNT'
|
|
|
|
STD_DEV = 'STD_DEV'
|
|
|
|
OUTPUT_HTML_FILE = 'OUTPUT_HTML_FILE'
|
2013-02-16 00:23:56 +01:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
|
|
|
outputFile = self.getOutputValue(self.OUTPUT_HTML_FILE)
|
|
|
|
uri = self.getParameterValue(self.INPUT)
|
2013-09-12 13:19:00 +02:00
|
|
|
layer = dataobjects.getObjectFromUri(uri)
|
2013-02-28 22:08:32 +01:00
|
|
|
values = raster.scanraster(layer, progress)
|
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
n = 0
|
|
|
|
nodata = 0
|
|
|
|
mean = 0
|
|
|
|
M2 = 0
|
|
|
|
sum = 0
|
|
|
|
minvalue = None
|
|
|
|
maxvalue = None
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
for v in values:
|
2013-02-28 22:08:32 +01:00
|
|
|
if v is not None:
|
|
|
|
sum += v
|
2013-02-16 00:23:56 +01:00
|
|
|
n = n + 1
|
|
|
|
delta = v - mean
|
2013-10-01 20:52:22 +03:00
|
|
|
mean = mean + delta / n
|
|
|
|
M2 = M2 + delta * (v - mean)
|
2013-02-16 00:23:56 +01:00
|
|
|
if minvalue is None:
|
|
|
|
minvalue = v
|
|
|
|
maxvalue = v
|
|
|
|
else:
|
|
|
|
minvalue = min(v, minvalue)
|
|
|
|
maxvalue = max(v, maxvalue)
|
|
|
|
else:
|
|
|
|
nodata += 1
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
variance = M2 / (n - 1)
|
2013-02-16 00:23:56 +01:00
|
|
|
stddev = math.sqrt(variance)
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
data = []
|
2013-10-01 20:52:22 +03:00
|
|
|
data.append('Valid cells: ' + unicode(n))
|
|
|
|
data.append('No-data cells: ' + unicode(nodata))
|
|
|
|
data.append('Minimum value: ' + unicode(minvalue))
|
|
|
|
data.append('Maximum value: ' + unicode(maxvalue))
|
|
|
|
data.append('Sum: ' + unicode(sum))
|
|
|
|
data.append('Mean value: ' + unicode(mean))
|
|
|
|
data.append('Standard deviation: ' + unicode(stddev))
|
2013-02-16 00:23:56 +01:00
|
|
|
|
|
|
|
self.createHTML(outputFile, data)
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
self.setOutputValue(self.COUNT, n)
|
|
|
|
self.setOutputValue(self.NO_DATA_COUNT, nodata)
|
|
|
|
self.setOutputValue(self.MIN, minvalue)
|
2013-02-28 22:08:32 +01:00
|
|
|
self.setOutputValue(self.MAX, maxvalue)
|
2013-02-16 00:23:56 +01:00
|
|
|
self.setOutputValue(self.SUM, sum)
|
2013-02-28 22:08:32 +01:00
|
|
|
self.setOutputValue(self.MEAN, mean)
|
2013-02-16 00:23:56 +01:00
|
|
|
self.setOutputValue(self.STD_DEV, stddev)
|
2013-02-28 22:08:32 +01:00
|
|
|
|
2013-02-16 00:23:56 +01:00
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Raster layer statistics'
|
|
|
|
self.group = 'Raster tools'
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterRaster(self.INPUT, self.tr('Input layer')))
|
|
|
|
self.addOutput(OutputHTML(self.OUTPUT_HTML_FILE, self.tr('Statistics')))
|
|
|
|
self.addOutput(OutputNumber(self.MIN, self.tr('Minimum value')))
|
|
|
|
self.addOutput(OutputNumber(self.MAX, self.tr('Maximum value')))
|
|
|
|
self.addOutput(OutputNumber(self.SUM, self.tr('Sum')))
|
|
|
|
self.addOutput(OutputNumber(self.MEAN, self.tr('Mean value')))
|
|
|
|
self.addOutput(OutputNumber(self.COUNT, self.tr('valid cells count')))
|
|
|
|
self.addOutput(OutputNumber(self.COUNT, self.tr('No-data cells count')))
|
|
|
|
self.addOutput(OutputNumber(self.STD_DEV, self.tr('Standard deviation')))
|
2013-02-16 00:23:56 +01:00
|
|
|
|
|
|
|
def createHTML(self, outputFile, algData):
|
2013-10-01 20:52:22 +03:00
|
|
|
f = open(outputFile, 'w')
|
2013-02-16 00:23:56 +01:00
|
|
|
for s in algData:
|
2013-10-01 20:52:22 +03:00
|
|
|
f.write('<p>' + str(s) + '</p>')
|
2013-02-16 00:23:56 +01:00
|
|
|
f.close()
|