Skip to content

Commit

Permalink
Basic statistics tool should no longer crash with PostGIS layers. Als…
Browse files Browse the repository at this point in the history
…o adds support for varchar, char, and text fields (PostGIS layers). Should fix #2510

git-svn-id: http://svn.osgeo.org/qgis/trunk@13028 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
cfarmer committed Mar 8, 2010
1 parent f0e23e3 commit 7bfbb76
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 57 deletions.
68 changes: 37 additions & 31 deletions python/plugins/fTools/tools/doVisual.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
Expand All @@ -17,7 +18,7 @@ def __init__( self, iface, function ):
self.cancel_close = self.buttonBox_2.button( QDialogButtonBox.Close )
self.progressBar.setValue( 0 )
self.partProgressBar.setValue( 0 )
self.partProgressBar.setEnabled( False )
self.partProgressBar.setVisible( False )

def keyPressEvent( self, e ):
'''
Expand All @@ -27,11 +28,11 @@ def keyPressEvent( self, e ):
selection = self.lstUnique.selectedItems()
items = QString()
if self.myFunction in ( 1, 2 ):
for rec in range( self.tblUnique.rowCount() ):
items.append( self.tblUnique.item( rec, 0 ).text() + "\n" )
for rec in range( self.tblUnique.rowCount() ):
items.append( self.tblUnique.item( rec, 0 ).text() + "\n" )
else:
for rec in range( self.tblUnique.rowCount() ):
items.append( self.tblUnique.item( rec, 0 ).text() + ":" + self.tblUnique.item( rec, 1 ).text() + "\n" )
for rec in range( self.tblUnique.rowCount() ):
items.append( self.tblUnique.item( rec, 0 ).text() + ":" + self.tblUnique.item( rec, 1 ).text() + "\n" )
if not items.isEmpty():
clip_board = QApplication.clipboard()
clip_board.setText( items )
Expand Down Expand Up @@ -125,26 +126,25 @@ def cancelThread( self ):

def runFinishedFromThread( self, output ):
self.testThread.stop()

result = output[ 0 ]
numRows = len( result )
self.tblUnique.setRowCount( numRows )
if self.myFunction in ( 1, 2 ):
self.tblUnique.setColumnCount( 1 )
for rec in range( numRows ):
item = QTableWidgetItem( result[ rec ] )
self.tblUnique.setItem( rec, 0, item )
item = QTableWidgetItem( result[ rec ] )
self.tblUnique.setItem( rec, 0, item )
else:
self.tblUnique.setColumnCount( 2 )
for rec in range( numRows ):
tmp = result[ rec ].split( ":" )
item = QTableWidgetItem( tmp[ 0 ] )
self.tblUnique.setItem( rec, 0, item )
item = QTableWidgetItem( tmp[ 1 ] )
self.tblUnique.setItem( rec, 1, item )
self.tblUnique.setHorizontalHeaderLabels( [ self.tr("Parameter"), self.tr("Value") ] )
self.tblUnique.horizontalHeader().setResizeMode( 1, QHeaderView.ResizeToContents )
self.tblUnique.horizontalHeader().show()
tmp = result[ rec ].split( ":" )
item = QTableWidgetItem( tmp[ 0 ] )
self.tblUnique.setItem( rec, 0, item )
item = QTableWidgetItem( tmp[ 1 ] )
self.tblUnique.setItem( rec, 1, item )
self.tblUnique.setHorizontalHeaderLabels( [ self.tr("Parameter"), self.tr("Value") ] )
self.tblUnique.horizontalHeader().setResizeMode( 1, QHeaderView.ResizeToContents )
self.tblUnique.horizontalHeader().show()
self.tblUnique.horizontalHeader().setResizeMode( 0, QHeaderView.Stretch )
self.tblUnique.resizeRowsToContents()

Expand All @@ -162,11 +162,11 @@ def runRangeFromThread( self, range_vals ):
def runPartStatusFromThread( self, status ):
self.partProgressBar.setValue( status )
if status >= self.part_max:
self.partProgressBar.setEnabled( False )
self.partProgressBar.setVisible( False )

def runPartRangeFromThread( self, range_vals ):
self.part_max = range_vals[ 1 ]
self.partProgressBar.setEnabled( True )
self.partProgressBar.setVisible( True )
self.partProgressBar.setRange( range_vals[ 0 ], range_vals[ 1 ] )

class visualThread( QThread ):
Expand Down Expand Up @@ -230,7 +230,8 @@ def basic_statistics( self, vlayer, myField ):
first = True
nElement = 0
# determine selected field type
if ftools_utils.getFieldType( vlayer, myField ) == 'String':
if ftools_utils.getFieldType( vlayer, myField ) in (
'String', 'varchar', 'char', 'text'):
fillVal = 0
emptyVal = 0
if self.mySelection: # only selected features
Expand Down Expand Up @@ -260,6 +261,7 @@ def basic_statistics( self, vlayer, myField ):
nFeat = vprovider.featureCount()
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
vprovider.select( allAttrs )
while vprovider.nextFeature( feat ):
atMap = feat.attributeMap()
lenVal = float( len( atMap[ index ].toString() ) )
Expand Down Expand Up @@ -290,14 +292,16 @@ def basic_statistics( self, vlayer, myField ):
lstStats.append( self.tr( "N:" ) + unicode( nVal ) )
return ( lstStats, [] )
else: # numeric field
stdVal = 0
cvVal = 0
rangeVal = 0
medianVal = 0
stdVal = 0.00
cvVal = 0.00
rangeVal = 0.00
medianVal = 0.00
maxVal = 0.00
minVal = 0.00
if self.mySelection: # only selected features
selection = vlayer.selectedFeatures()
nFeat = vlayer.selectedFeatureCount()
uniqueVal = ftools_utils.getUniqueValuesCount( vlayer, index, True )
uniqueVal = ftools_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 @@ -316,9 +320,10 @@ 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 )
uniqueVal = ftools_utils.getUniqueValuesCount( vlayer, index, False )
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )
vprovider.select( allAttrs )
while vprovider.nextFeature( feat ):
atMap = feat.attributeMap()
value = float( atMap[ index ].toDouble()[ 0 ] )
Expand All @@ -343,12 +348,12 @@ def basic_statistics( self, vlayer, myField ):
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[ int( ( nVal + 1 ) / 2 ) ]
lstVal = values
lstVal.sort()
if ( nVal % 2 ) == 0:
medianVal = 0.5 * ( lstVal[ int( ( nVal - 1 ) / 2 ) ] + lstVal[ int( ( nVal ) / 2 ) ] )
else:
medianVal = lstVal[ int( ( nVal + 1 ) / 2 ) ]
lstStats = []
lstStats.append( self.tr( "Mean:" ) + unicode( meanVal ) )
lstStats.append( self.tr( "StdDev:" ) + unicode( stdVal ) )
Expand Down Expand Up @@ -411,6 +416,7 @@ def check_geometry( self, vlayer ):
nElement = 0
self.emit( SIGNAL( "runStatus(PyQt_PyObject)" ), 0 )
self.emit( SIGNAL( "runRange(PyQt_PyObject)" ), ( 0, nFeat ) )

while vprovider.nextFeature( feat ):
geom = QgsGeometry( feat.geometry() )
nElement += 1
Expand Down
46 changes: 20 additions & 26 deletions python/plugins/fTools/tools/frmVisual.ui
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@
</item>
</layout>
</item>
<item row="3" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="useSelected">
<property name="text">
<string>Use only selected features</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="4" column="0" colspan="2">
<layout class="QVBoxLayout">
<item>
Expand Down Expand Up @@ -114,16 +125,15 @@
</item>
</layout>
</item>
<item row="3" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCheckBox" name="useSelected">
<property name="text">
<string>Use only selected features</string>
</property>
</widget>
</item>
</layout>
<item row="9" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="8" column="1" rowspan="2">
<widget class="QDialogButtonBox" name="buttonBox_2">
Expand All @@ -135,27 +145,11 @@
</property>
</widget>
</item>
<item row="9" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QProgressBar" name="partProgressBar">
<property name="enabled">
<bool>false</bool>
</property>
<property name="value">
<number>24</number>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
</layout>
Expand Down

0 comments on commit 7bfbb76

Please sign in to comment.