mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-17 00:02:54 -04:00
Also - simplify and add tests - remove large memory leak (persistant store of all non-project layers) - remove broken support for direct loading postgres/virtual layers by string (Python version was very broken and would never match a postgres/virtual layer)
31 lines
837 B
Python
31 lines
837 B
Python
##Centroids=name
|
|
##Geometry=group
|
|
##INPUT_LAYER=vector
|
|
##OUTPUT_LAYER=output vector
|
|
|
|
from qgis.core import QgsWkbTypes, QgsGeometry
|
|
|
|
from processing.tools.vector import VectorWriter
|
|
from processing.tools import dataobjects
|
|
|
|
layer = dataobjects.getLayerFromString(INPUT_LAYER)
|
|
fields = layer.fields()
|
|
|
|
writer = VectorWriter(OUTPUT_LAYER, 'utf-8', fields, QgsWkbTypes.Point, layer.crs())
|
|
|
|
features = processing.features(layer)
|
|
count = len(features)
|
|
if count == 0:
|
|
raise GeoAlgorithmExecutionException('Input layer contains no features.')
|
|
|
|
total = 100.0 / len(features)
|
|
|
|
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))
|