2012-10-04 19:33:47 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
Centroids.py
|
|
|
|
---------------------
|
|
|
|
Date : August 2012
|
|
|
|
Copyright : (C) 2012 by Victor Olaya
|
|
|
|
Email : volayaf at gmail dot com
|
|
|
|
***************************************************************************
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or *
|
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
***************************************************************************
|
|
|
|
"""
|
|
|
|
|
|
|
|
__author__ = 'Victor Olaya'
|
|
|
|
__date__ = 'August 2012'
|
|
|
|
__copyright__ = '(C) 2012, Victor Olaya'
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2013-02-04 00:14:39 +01:00
|
|
|
# This will get replaced with a git SHA1 when you do a git archive323
|
2013-10-01 20:52:22 +03:00
|
|
|
|
2012-10-04 19:33:47 +02:00
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
2012-08-02 07:44:55 +00:00
|
|
|
from PyQt4.QtCore import *
|
2012-07-26 12:26:02 +00:00
|
|
|
from qgis.core import *
|
2013-08-12 20:44:27 +02:00
|
|
|
from processing.core.GeoAlgorithm import GeoAlgorithm
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import \
|
|
|
|
GeoAlgorithmExecutionException
|
2014-07-14 14:19:09 +02:00
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
2013-10-01 20:52:22 +03:00
|
|
|
from processing.tools import dataobjects, vector
|
|
|
|
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
class Centroids(GeoAlgorithm):
|
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
def defineCharacteristics(self):
|
2013-10-01 20:52:22 +03:00
|
|
|
self.name = 'Polygon centroids'
|
|
|
|
self.group = 'Vector geometry tools'
|
2012-07-26 12:26:02 +00:00
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
|
|
|
self.tr('Input layer'), [ParameterVector.VECTOR_TYPE_POLYGON]))
|
2012-07-26 12:26:02 +00:00
|
|
|
|
2015-01-15 20:41:15 +02:00
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Output layer')))
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
def processAlgorithm(self, progress):
|
2013-10-01 20:52:22 +03:00
|
|
|
layer = dataobjects.getObjectFromUri(
|
|
|
|
self.getParameterValue(self.INPUT_LAYER))
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-10-01 20:52:22 +03:00
|
|
|
writer = self.getOutputFromName(
|
|
|
|
self.OUTPUT_LAYER).getVectorWriter(
|
|
|
|
layer.pendingFields().toList(),
|
|
|
|
QGis.WKBPoint,
|
|
|
|
layer.crs())
|
2012-10-01 20:53:42 +03:00
|
|
|
|
2012-07-26 12:26:02 +00:00
|
|
|
outFeat = QgsFeature()
|
2013-01-12 23:36:00 +01:00
|
|
|
|
2013-09-12 13:19:00 +02:00
|
|
|
features = vector.features(layer)
|
2013-01-12 23:36:00 +01:00
|
|
|
total = 100.0 / float(len(features))
|
2012-07-26 12:26:02 +00:00
|
|
|
current = 0
|
2012-10-01 20:53:42 +03:00
|
|
|
|
2013-01-01 23:52:00 +01:00
|
|
|
for inFeat in features:
|
2012-07-26 12:26:02 +00:00
|
|
|
inGeom = inFeat.geometry()
|
2013-02-04 00:14:39 +01:00
|
|
|
attrs = inFeat.attributes()
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
outGeom = QgsGeometry(inGeom.centroid())
|
|
|
|
if outGeom is None:
|
2013-10-01 20:52:22 +03:00
|
|
|
raise GeoAlgorithmExecutionException(
|
2015-01-15 20:41:15 +02:00
|
|
|
self.tr('Error calculating centroid'))
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
outFeat.setGeometry(outGeom)
|
2013-02-04 00:14:39 +01:00
|
|
|
outFeat.setAttributes(attrs)
|
2012-07-26 12:26:02 +00:00
|
|
|
writer.addFeature(outFeat)
|
|
|
|
current += 1
|
2012-10-01 20:53:42 +03:00
|
|
|
progress.setPercentage(int(current * total))
|
2012-07-26 12:26:02 +00:00
|
|
|
|
|
|
|
del writer
|