mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-03 00:02:25 -05:00
31 lines
808 B
Python
31 lines
808 B
Python
##Centroids=name
|
|
##Geometry=group
|
|
|
|
#inputs
|
|
|
|
##INPUT_LAYER=source
|
|
##OUTPUT_LAYER=sink point
|
|
|
|
from qgis.core import QgsWkbTypes, QgsProcessingUtils
|
|
|
|
fields = INPUT_LAYER.fields()
|
|
|
|
(sink, OUTPUT_LAYER) = self.parameterAsSink(parameters, 'OUTPUT_LAYER', context,
|
|
fields, QgsWkbTypes.Point, INPUT_LAYER.sourceCrs())
|
|
|
|
features = INPUT_LAYER.getFeatures()
|
|
count = INPUT_LAYER.featureCount()
|
|
if count == 0:
|
|
raise GeoAlgorithmExecutionException('Input layer contains no features.')
|
|
|
|
total = 100.0 / count
|
|
|
|
for count, f in enumerate(features):
|
|
outputFeature = f
|
|
if f.hasGeometry():
|
|
outputGeometry = f.geometry().centroid()
|
|
outputFeature.setGeometry(outputGeometry)
|
|
|
|
sink.addFeature(outputFeature)
|
|
feedback.setProgress(int(count * total))
|