2016-08-12 07:39:18 +10:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
***************************************************************************
|
|
|
|
PolygonCentroids.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'
|
|
|
|
|
|
|
|
# This will get replaced with a git SHA1 when you do a git archive323
|
|
|
|
|
|
|
|
__revision__ = '$Format:%H$'
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
|
|
|
from qgis.PyQt.QtGui import QIcon
|
|
|
|
|
2017-03-29 15:00:20 +10:00
|
|
|
from qgis.core import (QgsProcessingAlgorithm,
|
|
|
|
QgsGeometry,
|
|
|
|
QgsFeature,
|
2017-04-26 07:41:42 +10:00
|
|
|
QgsWkbTypes,
|
|
|
|
QgsProcessingUtils)
|
2016-08-12 07:39:18 +10:00
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
from processing.algs.qgis import QgisAlgorithm
|
2016-08-12 07:39:18 +10:00
|
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
|
|
from processing.core.parameters import ParameterVector
|
|
|
|
from processing.core.outputs import OutputVector
|
2017-05-02 14:47:58 +10:00
|
|
|
from processing.tools import dataobjects
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]
|
|
|
|
|
|
|
|
|
2017-05-19 11:27:16 +10:00
|
|
|
class PolygonCentroids(QgisAlgorithm):
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
INPUT_LAYER = 'INPUT_LAYER'
|
|
|
|
OUTPUT_LAYER = 'OUTPUT_LAYER'
|
|
|
|
|
2017-03-29 15:00:20 +10:00
|
|
|
def flags(self):
|
2016-08-12 07:39:18 +10:00
|
|
|
# this algorithm is deprecated - use Centroids instead
|
2017-03-29 15:00:20 +10:00
|
|
|
return QgsProcessingAlgorithm.FlagDeprecated
|
2016-08-12 07:39:18 +10:00
|
|
|
|
2017-03-29 10:42:42 +10:00
|
|
|
def icon(self):
|
2016-08-12 07:39:18 +10:00
|
|
|
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'centroids.png'))
|
|
|
|
|
2017-03-29 12:04:09 +10:00
|
|
|
def group(self):
|
|
|
|
return self.tr('Vector geometry tools')
|
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def name(self):
|
2017-03-29 17:09:39 +10:00
|
|
|
return 'polygoncentroids'
|
2017-03-29 12:51:59 +10:00
|
|
|
|
|
|
|
def displayName(self):
|
|
|
|
return self.tr('Polygon centroids')
|
2016-08-12 07:39:18 +10:00
|
|
|
|
2017-03-29 12:51:59 +10:00
|
|
|
def defineCharacteristics(self):
|
2016-08-12 07:39:18 +10:00
|
|
|
self.addParameter(ParameterVector(self.INPUT_LAYER,
|
2016-08-23 19:33:42 +03:00
|
|
|
self.tr('Input layer'), [dataobjects.TYPE_VECTOR_POLYGON]))
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Centroids')))
|
|
|
|
|
2017-04-25 14:55:38 +10:00
|
|
|
def processAlgorithm(self, context, feedback):
|
2017-05-02 13:40:49 +10:00
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(self.getParameterValue(self.INPUT_LAYER), context)
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
writer = self.getOutputFromName(
|
2017-04-26 14:33:53 +10:00
|
|
|
self.OUTPUT_LAYER).getVectorWriter(layer.fields(), QgsWkbTypes.Point, layer.crs(), context)
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
outFeat = QgsFeature()
|
|
|
|
|
2017-04-25 17:59:35 +10:00
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
2017-04-26 07:41:42 +10:00
|
|
|
total = 100.0 / QgsProcessingUtils.featureCount(layer, context)
|
2016-08-12 07:39:18 +10:00
|
|
|
for current, feat in enumerate(features):
|
|
|
|
inGeom = feat.geometry()
|
|
|
|
attrs = feat.attributes()
|
|
|
|
|
2017-01-30 21:58:23 +10:00
|
|
|
if inGeom.isNull():
|
2016-08-12 07:39:18 +10:00
|
|
|
outGeom = QgsGeometry(None)
|
|
|
|
else:
|
|
|
|
outGeom = QgsGeometry(inGeom.centroid())
|
|
|
|
if not outGeom:
|
|
|
|
raise GeoAlgorithmExecutionException(
|
|
|
|
self.tr('Error calculating centroid'))
|
|
|
|
|
|
|
|
outFeat.setGeometry(outGeom)
|
|
|
|
outFeat.setAttributes(attrs)
|
|
|
|
writer.addFeature(outFeat)
|
2017-01-06 20:04:00 +10:00
|
|
|
feedback.setProgress(int(current * total))
|
2016-08-12 07:39:18 +10:00
|
|
|
|
|
|
|
del writer
|