[QGIS Commit] r12039 - trunk/qgis/python/plugins/fTools/tools

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sun Nov 8 18:06:44 EST 2009


Author: cfarmer
Date: 2009-11-08 18:06:42 -0500 (Sun, 08 Nov 2009)
New Revision: 12039

Modified:
   trunk/qgis/python/plugins/fTools/tools/doVisual.py
   trunk/qgis/python/plugins/fTools/tools/ftools_utils.py
Log:
Adds new statistics/outputs to basic statistics plugin, thanks to Alexander Bruy for the patch

Modified: trunk/qgis/python/plugins/fTools/tools/doVisual.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/doVisual.py	2009-11-08 22:35:44 UTC (rev 12038)
+++ trunk/qgis/python/plugins/fTools/tools/doVisual.py	2009-11-08 23:06:42 UTC (rev 12039)
@@ -47,11 +47,11 @@
       # add all fields in combobox because now we can work with text fields too
       for i in changedField:
         if self.myFunction == 3:
-          if changedField[i].type() == QVariant.Int or changedField[i].type() == QVariant.Double:
+        #  if changedField[i].type() == QVariant.Int or changedField[i].type() == QVariant.Double:
             self.cmbField.addItem( unicode( changedField[i].name() ) )
         else:
           self.cmbField.addItem( unicode( changedField[i].name() ) )
-        self.cmbField.addItem( unicode( changedField[i].name() ) )
+         # self.cmbField.addItem( unicode( changedField[i].name() ) )
         
   def accept( self ):
     if self.inShape.currentText() == "":
@@ -246,19 +246,22 @@
       if nVal > 0.00:
         meanVal = sumVal / nVal
       lstStats = []
-      lstStats.append( QCoreApplication.translate( "statResult", "Max. len.      : " ) + unicode( maxVal ) )
-      lstStats.append( QCoreApplication.translate( "statResult", "Min. len.       : " ) + unicode( minVal ) )
-      lstStats.append( QCoreApplication.translate( "statResult", "Mean. len     : " ) + unicode( meanVal ) )
-      lstStats.append( QCoreApplication.translate( "statResult", "Filled             : " ) + unicode( fillVal ) )
-      lstStats.append( QCoreApplication.translate( "statResult", "Empty           : " ) + unicode( emptyVal ) )
-      lstStats.append( QCoreApplication.translate( "statResult", "N                   : " ) + unicode( nVal ) )
+      lstStats.append( self.tr( "Max. len: " ) + "         " + unicode( maxVal ) )
+      lstStats.append( self.tr( "Min. len: " ) + "         " + unicode( minVal ) )
+      lstStats.append( self.tr( "Mean. len: " ) + "         " + unicode( meanVal ) )
+      lstStats.append( self.tr( "Filled: " ) + "         " + unicode( fillVal ) )
+      lstStats.append( self.tr( "Empty: " ) + "         " + unicode( emptyVal ) )
+      lstStats.append( self.tr( "N: " ) + "         " + unicode( nVal ) )
       return ( lstStats, [] )
     else: # numeric field
       stdVal = 0
       cvVal = 0
+      rangeVal = 0
+      medianVal = 0
       if self.mySelection: # only selected features
         selection = vlayer.selectedFeatures()
         nFeat = vlayer.selectedFeatureCount()
+	uniqueVal = utils.getUniqueValuesCount( vlayer, index, True )
         self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
         self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
         for f in selection:
@@ -277,6 +280,7 @@
           self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
       else: # there is no selection, process the whole layer
         nFeat = vprovider.featureCount()
+	uniqueVal = ftools_utils.getUniqueValuesCount( vlayer, index, False )
         self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
         self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
         while vprovider.nextFeature( feat ):
@@ -294,6 +298,7 @@
           nElement += 1
           self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), nElement )
       nVal= float( len( values ) )
+      rangeVal = maxVal - minVal
       if nVal > 0.00:
         meanVal = sumVal / nVal
         if meanVal != 0.00:
@@ -301,14 +306,24 @@
             stdVal += ( ( val - meanVal ) * ( val - meanVal ) )
           stdVal = math.sqrt( stdVal / nVal )
           cvVal = stdVal / meanVal
+      if nVal > 1:
+	  lstVal = values
+	  lstVal.sort()
+	  if ( nVal % 2 ) == 0:
+	    medianVal = 0.5 * ( lstVal[ int( ( nVal - 1 ) / 2 ) ] + lstVal[ int( ( nVal ) / 2 ) ] )
+	  else:
+	    medianVal = lstVal[ ( nVal + 1 ) / 2 ]
       lstStats = []
-      lstStats.append( "Mean    : " + unicode( meanVal ) )
-      lstStats.append( "StdDev : " + unicode( stdVal ) )
-      lstStats.append( "Sum     : " + unicode( sumVal) )
-      lstStats.append( "Min     : " + unicode( minVal ) )
-      lstStats.append( "Max     : " + unicode( maxVal ) )
-      lstStats.append( "N         : " + unicode( nVal ) )
-      lstStats.append( "CV       : " + unicode( cvVal ) )
+      lstStats.append( self.tr( "Mean: " ) + "         " + unicode( meanVal ) )
+      lstStats.append( self.tr( "StdDev: " ) + "         " + unicode( stdVal ) )
+      lstStats.append( self.tr( "Sum: " ) + "         " + unicode( sumVal) )
+      lstStats.append( self.tr( "Min: " ) + "         " + unicode( minVal ) )
+      lstStats.append( self.tr( "Max: " ) + "         " + unicode( maxVal ) )
+      lstStats.append( self.tr( "N: " ) + "         " + unicode( nVal ) )
+      lstStats.append( self.tr( "CV: " ) + "         " + unicode( cvVal ) )
+      lstStats.append( self.tr( "Number of unique values: " ) + "         " + unicode( uniqueVal ) )
+      lstStats.append( self.tr( "Range: " ) + "         " + unicode( rangeVal ) )
+      lstStats.append( self.tr( "Median: " ) + "         " + unicode( medianVal ) )
       return ( lstStats, [] )
 
   def nearest_neighbour_analysis( self, vlayer ):

Modified: trunk/qgis/python/plugins/fTools/tools/ftools_utils.py
===================================================================
--- trunk/qgis/python/plugins/fTools/tools/ftools_utils.py	2009-11-08 22:35:44 UTC (rev 12038)
+++ trunk/qgis/python/plugins/fTools/tools/ftools_utils.py	2009-11-08 23:06:42 UTC (rev 12039)
@@ -20,6 +20,7 @@
 # getUniqueValues( QgsVectorDataProvider, int *field id )
 # saveDialog( QWidget *parent )
 # getFieldType( QgsVectorLayer, QgsField.name() )
+# getUniqueValuesCount( QgsVectorLayer, int fieldIndex, bool useSelection ):
 #
 # -------------------------------------------------
 
@@ -267,3 +268,24 @@
 	for name, field in fields.iteritems():
 		if field.name() == fieldName:
 			return field.typeName()
+
+# return the number of unique values in field
+def getUniqueValuesCount( vlayer, fieldIndex, useSelection ):
+	vprovider = vlayer.dataProvider()
+	allAttrs = vprovider.attributeIndexes()
+	vprovider.select( allAttrs )
+	count = 0
+	values = []
+	if useSelection:
+		selection = vlayer.selectedFeatures()
+		for f in selection:
+			if f.attributeMap()[ fieldIndex ].toString() not in values:
+				values.append( f.attributeMap()[ fieldIndex ].toString() )
+				count += 1
+	else:
+		feat = QgsFeature()
+		while vprovider.nextFeature( feat ):
+			if feat.attributeMap()[ fieldIndex ].toString() not in values:
+				values.append( feat.attributeMap()[ fieldIndex ].toString() )
+				count += 1
+	return count



More information about the QGIS-commit mailing list