Skip to content

Commit

Permalink
review Polygon centroids tool
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbruy committed Oct 4, 2012
1 parent a6418d6 commit e91efc1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions python/plugins/sextante/ftools/Centroids.py
@@ -1,7 +1,10 @@
import os.path

from PyQt4 import QtGui
from PyQt4.QtCore import *

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
Expand All @@ -21,26 +24,30 @@ def defineCharacteristics(self):
self.name = "Polygon centroids"
self.group = "Geometry tools"

self.addParameter(ParameterVector(Centroids.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))

self.addOutput(OutputVector(Centroids.OUTPUT_LAYER, "Output layer"))
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))

def processAlgorithm(self, progress):
layer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT_LAYER))
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))

provider = layer.dataProvider()
outFileName = self.getOutputValue(self.OUTPUT_LAYER)

writer = self.getOutputFromName(Centroids.OUTPUT_LAYER).getVectorWriter(provider.fields(),
QGis.WKBPoint, provider.crs())
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

allAttrs = provider.attributeIndexes()
provider.select(allAttrs)
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(layer.pendingFields(),
QGis.WKBPoint, layer.dataProvider().crs())

layer.select(layer.pendingAllAttributesList())

inFeat = QgsFeature()
outFeat = QgsFeature()
total = provider.featureCount()

total = 100.0 / float(layer.featureCount())
current = 0
while provider.nextFeature(inFeat):

while layer.nextFeature(inFeat):
inGeom = inFeat.geometry()
attrMap = inFeat.attributeMap()

Expand All @@ -52,6 +59,6 @@ def processAlgorithm(self, progress):
outFeat.setAttributeMap(attrMap)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current / total * 100))
progress.setPercentage(int(current * total))

del writer

0 comments on commit e91efc1

Please sign in to comment.