mirror of
https://github.com/qgis/QGIS.git
synced 2025-11-15 00:06:49 -05:00
Added stdev
This commit is contained in:
parent
98a72425a3
commit
fe3760032b
@ -31,6 +31,7 @@
|
|||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
import ftools_utils
|
import ftools_utils
|
||||||
|
import math
|
||||||
from qgis.core import *
|
from qgis.core import *
|
||||||
from ui_frmPointsInPolygon import Ui_Dialog
|
from ui_frmPointsInPolygon import Ui_Dialog
|
||||||
|
|
||||||
@ -207,8 +208,8 @@ class PointsInPolygonThread(QThread):
|
|||||||
columnName = unicode(item.text() + "_" + self.statistics)
|
columnName = unicode(item.text() + "_" + self.statistics)
|
||||||
index = polyProvider.fieldNameIndex(unicode(columnName))
|
index = polyProvider.fieldNameIndex(unicode(columnName))
|
||||||
if index == -1:
|
if index == -1:
|
||||||
if item.type() == typeDouble or self.statistics == "mean":
|
if item.type() == typeDouble or self.statistics == "mean" or self.statistics == "stddev":
|
||||||
fieldList.append( QgsField(columnName, QVariant.Double, "double", 10, 2, "Value") )
|
fieldList.append( QgsField(columnName, QVariant.Double, "double", 24, 15, "Value") )
|
||||||
else:
|
else:
|
||||||
fieldList.append( QgsField(columnName, QVariant.Int, "int", 10, 0, "Value") )
|
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
|
# Compute the statistical values for selected vector attributes
|
||||||
for item in selectedItems:
|
for item in selectedItems:
|
||||||
values = valueList[item.text()]
|
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 values and len(values) > 0:
|
||||||
if self.statistics == "sum":
|
if self.statistics == "sum":
|
||||||
value = reduce(myAdder, values)
|
value = reduce(myAdder, values)
|
||||||
@ -279,6 +290,9 @@ class PointsInPolygonThread(QThread):
|
|||||||
elif self.statistics == "max":
|
elif self.statistics == "max":
|
||||||
values.sort()
|
values.sort()
|
||||||
value = values[-1]
|
value = values[-1]
|
||||||
|
elif self.statistics == "stddev":
|
||||||
|
value = two_pass_variance(values)
|
||||||
|
value = math.sqrt(value)
|
||||||
atMap.append(value)
|
atMap.append(value)
|
||||||
|
|
||||||
outFeat.setAttributes(atMap)
|
outFeat.setAttributes(atMap)
|
||||||
@ -309,3 +323,27 @@ class PointsInPolygonThread(QThread):
|
|||||||
|
|
||||||
def myAdder(x,y):
|
def myAdder(x,y):
|
||||||
return 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
|
||||||
|
|||||||
@ -264,7 +264,6 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
pt4 = QgsPoint(x, y - yOffset)
|
pt4 = QgsPoint(x, y - yOffset)
|
||||||
pt5 = QgsPoint(x, y)
|
pt5 = QgsPoint(x, y)
|
||||||
|
|
||||||
print self.angle.value()
|
|
||||||
if self.angle.value() != 0.0:
|
if self.angle.value() != 0.0:
|
||||||
self.rotatePoint(pt1)
|
self.rotatePoint(pt1)
|
||||||
self.rotatePoint(pt2)
|
self.rotatePoint(pt2)
|
||||||
|
|||||||
@ -205,7 +205,7 @@
|
|||||||
<number>10</number>
|
<number>10</number>
|
||||||
</property>
|
</property>
|
||||||
<property name="minimum">
|
<property name="minimum">
|
||||||
<double>0.000001000000000</double>
|
<double>0.000100000000000</double>
|
||||||
</property>
|
</property>
|
||||||
<property name="maximum">
|
<property name="maximum">
|
||||||
<double>1000000000.000000000000000</double>
|
<double>1000000000.000000000000000</double>
|
||||||
@ -270,7 +270,7 @@
|
|||||||
<number>10</number>
|
<number>10</number>
|
||||||
</property>
|
</property>
|
||||||
<property name="minimum">
|
<property name="minimum">
|
||||||
<double>0.000001000000000</double>
|
<double>0.000100000000000</double>
|
||||||
</property>
|
</property>
|
||||||
<property name="maximum">
|
<property name="maximum">
|
||||||
<double>1000000000.000000000000000</double>
|
<double>1000000000.000000000000000</double>
|
||||||
@ -286,7 +286,7 @@
|
|||||||
<string>Output grid as polygons</string>
|
<string>Output grid as polygons</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="checked">
|
<property name="checked">
|
||||||
<bool>true</bool>
|
<bool>false</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@ -296,7 +296,7 @@
|
|||||||
<string>Output grid as lines</string>
|
<string>Output grid as lines</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="checked">
|
<property name="checked">
|
||||||
<bool>false</bool>
|
<bool>true</bool>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
@ -369,9 +369,6 @@
|
|||||||
<property name="text">
|
<property name="text">
|
||||||
<string>Add result to canvas</string>
|
<string>Add result to canvas</string>
|
||||||
</property>
|
</property>
|
||||||
<property name="checked">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="6" column="0">
|
<item row="6" column="0">
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user