mirror of
https://github.com/qgis/QGIS.git
synced 2025-04-17 00:04:02 -04:00
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:
parent
d22016dbbd
commit
428b72812b
@ -1,3 +1,4 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
from PyQt4.QtCore import *
|
from PyQt4.QtCore import *
|
||||||
from PyQt4.QtGui import *
|
from PyQt4.QtGui import *
|
||||||
import ftools_utils
|
import ftools_utils
|
||||||
@ -37,8 +38,8 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
self.uniqueField.clear()
|
self.uniqueField.clear()
|
||||||
self.weightField.addItem( self.tr("(Optional) Weight field") )
|
self.weightField.addItem( self.tr("(Optional) Weight field") )
|
||||||
self.uniqueField.addItem( self.tr("(Optional) Unique ID field") )
|
self.uniqueField.addItem( self.tr("(Optional) Unique ID field") )
|
||||||
self.changedLayer = self.getVectorLayerByName(inputLayer)
|
self.changedLayer = ftools_utils.getVectorLayerByName(inputLayer)
|
||||||
changedField = self.getFieldList(self.changedLayer)
|
changedField = ftools_utils.getFieldList(self.changedLayer)
|
||||||
for i in changedField:
|
for i in changedField:
|
||||||
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.weightField.addItem(unicode(changedField[i].name()))
|
self.weightField.addItem(unicode(changedField[i].name()))
|
||||||
@ -85,7 +86,7 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
self.outShape.setText( QString( self.shapefileName ) )
|
self.outShape.setText( QString( self.shapefileName ) )
|
||||||
|
|
||||||
def compute(self, inName, outName, weightField="", times=1, uniqueField=""):
|
def compute(self, inName, outName, weightField="", times=1, uniqueField=""):
|
||||||
vlayer = self.getVectorLayerByName(inName)
|
vlayer = ftools_utils.getVectorLayerByName(inName)
|
||||||
provider = vlayer.dataProvider()
|
provider = vlayer.dataProvider()
|
||||||
weightIndex = provider.fieldNameIndex(weightField)
|
weightIndex = provider.fieldNameIndex(weightField)
|
||||||
uniqueIndex = provider.fieldNameIndex(uniqueField)
|
uniqueIndex = provider.fieldNameIndex(uniqueField)
|
||||||
@ -98,7 +99,7 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
|
if not QgsVectorFileWriter.deleteShapeFile(self.shapefileName):
|
||||||
return
|
return
|
||||||
if uniqueIndex <> -1:
|
if uniqueIndex <> -1:
|
||||||
uniqueValues = self.getUniqueValues(provider, int(uniqueIndex))
|
uniqueValues = ftools_utils.getUniqueValues(provider, int( uniqueIndex ) )
|
||||||
single = False
|
single = False
|
||||||
else:
|
else:
|
||||||
uniqueValues = [QVariant(1)]
|
uniqueValues = [QVariant(1)]
|
||||||
@ -118,6 +119,11 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
self.progressBar.setRange(0, nFeat)
|
self.progressBar.setRange(0, nFeat)
|
||||||
for j in uniqueValues:
|
for j in uniqueValues:
|
||||||
provider.rewind()
|
provider.rewind()
|
||||||
|
provider.select(allAttrs)
|
||||||
|
cx = 0.00
|
||||||
|
cy = 0.00
|
||||||
|
points = []
|
||||||
|
weights = []
|
||||||
while provider.nextFeature(feat):
|
while provider.nextFeature(feat):
|
||||||
nElement += 1
|
nElement += 1
|
||||||
self.progressBar.setValue(nElement)
|
self.progressBar.setValue(nElement)
|
||||||
@ -143,10 +149,9 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
cx = 0.00
|
cx = 0.00
|
||||||
cy = 0.00
|
cy = 0.00
|
||||||
item = 0
|
item = 0
|
||||||
for i in points:
|
for item, i in enumerate(points):
|
||||||
cx += i.x() * weights[item]
|
cx += i.x() * weights[item]
|
||||||
cy += i.y() * weights[item]
|
cy += i.y() * weights[item]
|
||||||
item += 1
|
|
||||||
cx = cx / sumWeight
|
cx = cx / sumWeight
|
||||||
cy = cy / sumWeight
|
cy = cy / sumWeight
|
||||||
meanPoint = QgsPoint(cx, cy)
|
meanPoint = QgsPoint(cx, cy)
|
||||||
@ -178,28 +183,6 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
break
|
break
|
||||||
del writer
|
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):
|
def extract(self, geom):
|
||||||
multi_geom = QgsGeometry()
|
multi_geom = QgsGeometry()
|
||||||
temp_geom = []
|
temp_geom = []
|
||||||
@ -227,14 +210,3 @@ class Dialog(QDialog, Ui_Dialog):
|
|||||||
temp_geom.extend(i)
|
temp_geom.extend(i)
|
||||||
return temp_geom
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user