mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-10 00:13:55 -04:00
flexible QVariantMap creatOptions parameters which include an optional fileEncoding value More flexible, allows sinks to be created using any creation option which is passed to the underlying provider
28 lines
879 B
Python
28 lines
879 B
Python
##Centroids=name
|
|
##Geometry=group
|
|
##INPUT_LAYER=vector
|
|
##OUTPUT_LAYER=output vector
|
|
|
|
from qgis.core import QgsWkbTypes, QgsProcessingUtils
|
|
|
|
layer = QgsProcessingUtils.mapLayerFromString(INPUT_LAYER, context)
|
|
fields = layer.fields()
|
|
|
|
writer, writer_dest = QgsProcessingUtils.createFeatureSink(OUTPUT_LAYER, context, fields, QgsWkbTypes.Point, layer.crs(), {'fileEncoding': 'utf-8'})
|
|
|
|
features = QgsProcessingUtils.getFeatures(layer, context)
|
|
count = QgsProcessingUtils.featureCount(layer, context)
|
|
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)
|
|
|
|
writer.addFeature(outputFeature)
|
|
feedback.setProgress(int(count * total))
|