Means for unique values now properly calculated. Fixes #2509.

git-svn-id: http://svn.osgeo.org/qgis/trunk@13030 c8812cc2-4d05-0410-92ff-de0c093fc19c
This commit is contained in:
cfarmer 2010-03-08 23:15:11 +00:00
parent d22016dbbd
commit 428b72812b

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import ftools_utils
@ -37,8 +38,8 @@ class Dialog(QDialog, Ui_Dialog):
self.uniqueField.clear()
self.weightField.addItem( self.tr("(Optional) Weight field") )
self.uniqueField.addItem( self.tr("(Optional) Unique ID field") )
self.changedLayer = self.getVectorLayerByName(inputLayer)
changedField = self.getFieldList(self.changedLayer)
self.changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
changedField = ftools_utils.getFieldList(self.changedLayer)
for i in changedField:
if changedField[i].type() == QVariant.Int or changedField[i].type() == QVariant.Double:
self.weightField.addItem(unicode(changedField[i].name()))
@ -85,7 +86,7 @@ class Dialog(QDialog, Ui_Dialog):
self.outShape.setText( QString( self.shapefileName ) )
def compute(self, inName, outName, weightField="", times=1, uniqueField=""):
vlayer = self.getVectorLayerByName(inName)
vlayer = ftools_utils.getVectorLayerByName(inName)
provider = vlayer.dataProvider()
weightIndex = provider.fieldNameIndex(weightField)
uniqueIndex = provider.fieldNameIndex(uniqueField)
@ -98,7 +99,7 @@ class Dialog(QDialog, Ui_Dialog):
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
return
if uniqueIndex <> -1:
uniqueValues = self.getUniqueValues(provider, int(uniqueIndex))
uniqueValues = ftools_utils.getUniqueValues(provider, int( uniqueIndex ) )
single = False
else:
uniqueValues = [QVariant(1)]
@ -118,6 +119,11 @@ class Dialog(QDialog, Ui_Dialog):
self.progressBar.setRange(0, nFeat)
for j in uniqueValues:
provider.rewind()
provider.select(allAttrs)
cx = 0.00
cy = 0.00
points = []
weights = []
while provider.nextFeature(feat):
nElement += 1
self.progressBar.setValue(nElement)
@ -143,10 +149,9 @@ class Dialog(QDialog, Ui_Dialog):
cx = 0.00
cy = 0.00
item = 0
for i in points:
for item, i in enumerate(points):
cx += i.x() * weights[item]
cy += i.y() * weights[item]
item += 1
cx = cx / sumWeight
cy = cy / sumWeight
meanPoint = QgsPoint(cx, cy)
@ -178,28 +183,6 @@ class Dialog(QDialog, Ui_Dialog):
break
del writer
# Gets vector layer by layername in canvas
def getVectorLayerByName(self, myName):
mc = self.iface.mapCanvas()
nLayers = mc.layerCount()
for l in range(nLayers):
layer = mc.layer(l)
if layer.name() == unicode(myName):
vlayer = QgsVectorLayer(unicode(layer.source()), unicode(myName), unicode(layer.dataProvider().name()))
if vlayer.isValid():
return vlayer
# Retrieve the field map of a vector Layer
def getFieldList(self, vlayer):
fProvider = vlayer.dataProvider()
feat = QgsFeature()
allAttrs = fProvider.attributeIndexes()
# fetch all attributes for each feature
fProvider.select(allAttrs)
# retrieve all fields
myFields = fProvider.fields()
return myFields
def extract(self, geom):
multi_geom = QgsGeometry()
temp_geom = []
@ -227,14 +210,3 @@ class Dialog(QDialog, Ui_Dialog):
temp_geom.extend(i)
return temp_geom
def getUniqueValues(self, provider, index):
allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
f = QgsFeature()
values = []
check = []
while provider.nextFeature( f ):
if not f.attributeMap()[index].toString() in check:
values.append( f.attributeMap()[index] )
check.append( f.attributeMap()[index].toString() )
return values