Added stdev

This commit is contained in:
Sören Gebbert 2015-01-16 21:27:36 +01:00
parent 98a72425a3
commit fe3760032b
3 changed files with 44 additions and 10 deletions

View File

@ -31,6 +31,7 @@
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ftools_utils
import math
from qgis.core import *
from ui_frmPointsInPolygon import Ui_Dialog
@ -207,8 +208,8 @@ class PointsInPolygonThread(QThread):
columnName = unicode(item.text() + "_" + self.statistics)
index = polyProvider.fieldNameIndex(unicode(columnName))
if index == -1:
if item.type() == typeDouble or self.statistics == "mean":
fieldList.append( QgsField(columnName, QVariant.Double, "double", 10, 2, "Value") )
if item.type() == typeDouble or self.statistics == "mean" or self.statistics == "stddev":
fieldList.append( QgsField(columnName, QVariant.Double, "double", 24, 15, "Value") )
else:
fieldList.append( QgsField(columnName, QVariant.Int, "int", 10, 0, "Value") )
@ -268,6 +269,16 @@ class PointsInPolygonThread(QThread):
# Compute the statistical values for selected vector attributes
for item in selectedItems:
values = valueList[item.text()]
# Check if the input contains non-numeric values
non_numeric_values = False
for value in values:
if (isinstance(value, type(float())) != True) and (isinstance(value, type(int())) != True):
non_numeric_values = True
break
# Jump over invalid values
if non_numeric_values is True:
continue
if values and len(values) > 0:
if self.statistics == "sum":
value = reduce(myAdder, values)
@ -279,6 +290,9 @@ class PointsInPolygonThread(QThread):
elif self.statistics == "max":
values.sort()
value = values[-1]
elif self.statistics == "stddev":
value = two_pass_variance(values)
value = math.sqrt(value)
atMap.append(value)
outFeat.setAttributes(atMap)
@ -309,3 +323,27 @@ class PointsInPolygonThread(QThread):
def myAdder(x,y):
return x+y
def two_pass_variance(data):
"""
Variance algorithm taken from Wikipedia:
https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
"""
n = 0.0
sum1 = 0.0
sum2 = 0.0
for x in data:
n = n + 1.0
sum1 = sum1 + float(x)
if (n < 2):
return 0
mean = sum1 / n
for x in data:
sum2 = sum2 + (x - mean)*(x - mean)
variance = sum2 / (n - 1)
return variance

View File

@ -264,7 +264,6 @@ class Dialog(QDialog, Ui_Dialog):
pt4 = QgsPoint(x, y - yOffset)
pt5 = QgsPoint(x, y)
print self.angle.value()
if self.angle.value() != 0.0:
self.rotatePoint(pt1)
self.rotatePoint(pt2)

View File

@ -205,7 +205,7 @@
<number>10</number>
</property>
<property name="minimum">
<double>0.000001000000000</double>
<double>0.000100000000000</double>
</property>
<property name="maximum">
<double>1000000000.000000000000000</double>
@ -270,7 +270,7 @@
<number>10</number>
</property>
<property name="minimum">
<double>0.000001000000000</double>
<double>0.000100000000000</double>
</property>
<property name="maximum">
<double>1000000000.000000000000000</double>
@ -286,7 +286,7 @@
<string>Output grid as polygons</string>
</property>
<property name="checked">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
@ -296,7 +296,7 @@
<string>Output grid as lines</string>
</property>
<property name="checked">
<bool>false</bool>
<bool>true</bool>
</property>
</widget>
</item>
@ -369,9 +369,6 @@
<property name="text">
<string>Add result to canvas</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item row="6" column="0">