65 lines
2.0 KiB
Python
Raw Normal View History

import os.path
2012-10-01 20:53:42 +03:00
from PyQt4 import QtGui
from PyQt4.QtCore import *
2012-10-01 20:53:42 +03:00
from qgis.core import *
2012-10-01 20:53:42 +03:00
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"
2012-10-01 20:53:42 +03:00
self.addParameter(ParameterVector(self.INPUT_LAYER, "Input layer", ParameterVector.VECTOR_TYPE_POLYGON))
2012-10-01 20:53:42 +03:00
self.addOutput(OutputVector(self.OUTPUT_LAYER, "Output layer"))
def processAlgorithm(self, progress):
2012-10-01 20:53:42 +03:00
layer = QGisLayers.getObjectFromUri(self.getParameterValue(self.INPUT_LAYER))
2012-10-01 20:53:42 +03:00
outFileName = self.getOutputValue(self.OUTPUT_LAYER)
2012-10-01 20:53:42 +03:00
settings = QSettings()
encoding = settings.value( "/UI/encoding", "System" ).toString()
2012-10-01 20:53:42 +03:00
writer = self.getOutputFromName(self.OUTPUT_LAYER).getVectorWriter(layer.pendingFields(),
QGis.WKBPoint, layer.dataProvider().crs())
layer.select(layer.pendingAllAttributesList())
inFeat = QgsFeature()
outFeat = QgsFeature()
2012-10-01 20:53:42 +03:00
total = 100.0 / float(layer.featureCount())
current = 0
2012-10-01 20:53:42 +03:00
while layer.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
2012-10-01 20:53:42 +03:00
progress.setPercentage(int(current * total))
del writer