31 lines
808 B
Python
Raw Normal View History

2017-03-16 12:30:26 +02:00
##Centroids=name
##Geometry=group
2017-06-27 11:12:20 +10:00
#inputs
##INPUT_LAYER=source
##OUTPUT_LAYER=sink point
from qgis.core import QgsWkbTypes, QgsProcessingUtils
2017-03-16 12:30:26 +02:00
2017-06-27 11:12:20 +10:00
fields = INPUT_LAYER.fields()
2017-03-16 12:30:26 +02:00
2017-06-27 11:12:20 +10:00
(sink, OUTPUT_LAYER) = self.parameterAsSink(parameters, 'OUTPUT_LAYER', context,
fields, QgsWkbTypes.Point, INPUT_LAYER.sourceCrs())
2017-03-16 12:30:26 +02:00
2017-06-27 11:12:20 +10:00
features = INPUT_LAYER.getFeatures()
count = INPUT_LAYER.featureCount()
2017-03-16 12:30:26 +02:00
if count == 0:
raise GeoAlgorithmExecutionException('Input layer contains no features.')
2017-04-26 07:41:42 +10:00
total = 100.0 / count
2017-03-16 12:30:26 +02:00
for count, f in enumerate(features):
outputFeature = f
2017-03-16 17:18:52 +02:00
if f.hasGeometry():
2017-03-16 12:30:26 +02:00
outputGeometry = f.geometry().centroid()
outputFeature.setGeometry(outputGeometry)
2017-06-27 11:12:20 +10:00
sink.addFeature(outputFeature)
2017-03-16 12:30:26 +02:00
feedback.setProgress(int(count * total))