Skip to content
Permalink
Browse files

cosmetic fixes

git-svn-id: http://sextante.googlecode.com/svn/trunk/soft/bindings/qgis-plugin@313 881b9c09-3ef8-f3c2-ec3d-21d735c97f4d
  • Loading branch information
alexander.bruy@gmail.com
alexander.bruy@gmail.com committed Jul 26, 2012
1 parent 5b61a76 commit 7a5ca2dc7f3d1a9996a879bd7802e4823a9150c5
Showing with 64 additions and 52 deletions.
  1. +64 −52 src/sextante/ftools/Centroids.py
@@ -1,52 +1,64 @@
from sextante.core.GeoAlgorithm import GeoAlgorithm
import os.path
from PyQt4 import QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from sextante.parameters.ParameterVector import ParameterVector
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from sextante.outputs.OutputVector import OutputVector

class Centroids(GeoAlgorithm):

INPUT = "INPUT"
OUTPUT = "OUTPUT"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/centroids.png")

def processAlgorithm(self, progress):
settings = QSettings()
systemEncoding = settings.value( "/UI/encoding", "System" ).toString()
output = self.getOutputValue(Centroids.OUTPUT)
vlayer = QGisLayers.getObjectFromUri(self.getParameterValue(Centroids.INPUT))
vprovider = vlayer.dataProvider()
allAttrs = vprovider.attributeIndexes()
vprovider.select( allAttrs )
fields = vprovider.fields()
writer = QgsVectorFileWriter( output, systemEncoding, fields, QGis.WKBPoint, vprovider.crs() )
inFeat = QgsFeature()
outFeat = QgsFeature()
nFeat = vprovider.featureCount()
nElement = 0
while vprovider.nextFeature( inFeat ):
nElement += 1
progress.setPercentage(int(nElement/nFeat * 100))
inGeom = inFeat.geometry()
atMap = inFeat.attributeMap()
outGeom = QgsGeometry(inGeom.centroid())
if outGeom is None:
raise GeoAlgorithmExecutionException("Error calculating centroid")
outFeat.setAttributeMap( atMap )
outFeat.setGeometry( outGeom )
writer.addFeature( outFeat )
del writer

def defineCharacteristics(self):
self.name = "Centroids"
self.group = "Geometry tools"
self.addParameter(ParameterVector(Centroids.INPUT, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
self.addOutput(OutputVector(Centroids.OUTPUT, "Output layer"))
#=========================================================
import os.path

from PyQt4 import QtGui

from qgis.core import *

from sextante.core.GeoAlgorithm import GeoAlgorithm
from sextante.core.QGisLayers import QGisLayers
from sextante.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException

from sextante.parameters.ParameterVector import ParameterVector
from sextante.outputs.OutputVector import OutputVector

class Centroids(GeoAlgorithm):

INPUT_LAYER = "INPUT_LAYER"
OUTPUT_LAYER = "OUTPUT_LAYER"

def getIcon(self):
return QtGui.QIcon(os.path.dirname(__file__) + "/icons/centroids.png")

def defineCharacteristics(self):
self.name = "Polygon centroids"
self.group = "Geometry tools"

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

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

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

outFileName = self.getOutputValue(Centroids.OUTPUT_LAYER)

provider = layer.dataProvider()

settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()

writer = QgsVectorFileWriter(outFileName, encoding, provider.fields(),
QGis.WKBPoint, provider.crs())

allAttrs = provider.attributeIndexes()
provider.select(allAttrs)

inFeat = QgsFeature()
outFeat = QgsFeature()
total = provider.featureCount()
current = 0
while provider.nextFeature(inFeat):
inGeom = inFeat.geometry()
attrMap = inFeat.attributeMap()

outGeom = QgsGeometry(inGeom.centroid())
if outGeom is None:
raise GeoAlgorithmExecutionException("Error calculating centroid")

outFeat.setGeometry(outGeom)
outFeat.setAttributeMap(attrMap)
writer.addFeature(outFeat)
current += 1
progress.setPercentage(int(current / total * 100))

del writer

0 comments on commit 7a5ca2d

Please sign in to comment.
You can’t perform that action at this time.