Skip to content

Commit

Permalink
Adds new statistics/outputs to basic statistics plugin, thanks to Ale…
Browse files Browse the repository at this point in the history
…xander Bruy for the patch

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@12039 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Nov 8, 2009
1 parent 936d175 commit c4f936b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
45 changes: 30 additions & 15 deletions python/plugins/fTools/tools/doVisual.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ def update( self ):
# 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() == "":
Expand Down Expand Up @@ -246,19 +246,22 @@ def basic_statistics( self, vlayer, myField ):
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:
Expand All @@ -277,6 +280,7 @@ def basic_statistics( self, vlayer, myField ):
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 ):
Expand All @@ -294,21 +298,32 @@ def basic_statistics( self, vlayer, myField ):
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:
for val in values:
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 ):
Expand Down
22 changes: 22 additions & 0 deletions python/plugins/fTools/tools/ftools_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# getUniqueValues( QgsVectorDataProvider, int *field id )
# saveDialog( QWidget *parent )
# getFieldType( QgsVectorLayer, QgsField.name() )
# getUniqueValuesCount( QgsVectorLayer, int fieldIndex, bool useSelection ):
#
# -------------------------------------------------

Expand Down Expand Up @@ -267,3 +268,24 @@ def getFieldType(vlayer, fieldName):
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

0 comments on commit c4f936b

Please sign in to comment.