Skip to content

Commit e91efc1

Browse files
committedOct 4, 2012
review Polygon centroids tool
1 parent a6418d6 commit e91efc1

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed
 

‎python/plugins/sextante/ftools/Centroids.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import os.path
2+
23
from PyQt4 import QtGui
34
from PyQt4.QtCore import *
5+
46
from qgis.core import *
7+
58
from sextante.core.GeoAlgorithm import GeoAlgorithm
69
from sextante.core.QGisLayers import QGisLayers
710
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
@@ -21,26 +24,30 @@ def defineCharacteristics(self):
2124
self.name = "Polygon centroids"
2225
self.group = "Geometry tools"
2326

24-
self.addParameter(ParameterVector(Centroids.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
27+
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
2528

26-
self.addOutput(OutputVector(Centroids.OUTPUT_LAYER, "Output layer"))
29+
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
2730

2831
def processAlgorithm(self, progress):
29-
layer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT_LAYER))
32+
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
3033

31-
provider = layer.dataProvider()
34+
outFileName = self.getOutputValue(self.OUTPUT_LAYER)
3235

33-
writer = self.getOutputFromName(Centroids.OUTPUT_LAYER).getVectorWriter(provider.fields(),
34-
QGis.WKBPoint, provider.crs())
36+
settings = QSettings()
37+
encoding = settings.value( "/UI/encoding", "System" ).toString()
3538

36-
allAttrs = provider.attributeIndexes()
37-
provider.select(allAttrs)
39+
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(layer.pendingFields(),
40+
QGis.WKBPoint, layer.dataProvider().crs())
41+
42+
layer.select(layer.pendingAllAttributesList())
3843

3944
inFeat = QgsFeature()
4045
outFeat = QgsFeature()
41-
total = provider.featureCount()
46+
47+
total = 100.0 / float(layer.featureCount())
4248
current = 0
43-
while provider.nextFeature(inFeat):
49+
50+
while layer.nextFeature(inFeat):
4451
inGeom = inFeat.geometry()
4552
attrMap = inFeat.attributeMap()
4653

@@ -52,6 +59,6 @@ def processAlgorithm(self, progress):
5259
outFeat.setAttributeMap(attrMap)
5360
writer.addFeature(outFeat)
5461
current += 1
55-
progress.setPercentage(int(current / total * 100))
62+
progress.setPercentage(int(current * total))
5663

5764
del writer

0 commit comments

Comments
 (0)
Please sign in to comment.