mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-04 00:30:59 -05:00
This script has been changed to use a sink output, not the previous TableWriter csv output. All algorithms which output flat tables should now still output feature sinks, to allow correct use in later model steps.
50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
##Table=group
|
|
|
|
#inputs
|
|
|
|
##Input=source
|
|
##Fields=field multiple Input
|
|
##Frequency=sink table
|
|
|
|
#outputs
|
|
##Frequency=output outputVector
|
|
|
|
from processing.tools.vector import TableWriter
|
|
from collections import defaultdict
|
|
from qgis.core import QgsProcessingUtils, QgsFields, QgsField, QgsWkbTypes, QgsFeature
|
|
from qgis.PyQt.QtCore import QVariant
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
|
|
inputFields = Input.fields()
|
|
fieldIdxs = []
|
|
out_fields = QgsFields()
|
|
for f in Fields:
|
|
idx = inputFields.indexFromName(f)
|
|
if idx == -1:
|
|
raise GeoAlgorithmExecutionException('Field not found:' + f)
|
|
fieldIdxs.append(idx)
|
|
out_fields.append(inputFields.at(idx))
|
|
|
|
out_fields.append(QgsField('FREQ', QVariant.Int))
|
|
|
|
(sink, Frequency) = self.parameterAsSink(parameters, 'Frequency', context,
|
|
out_fields)
|
|
|
|
counts = {}
|
|
feats = Input.getFeatures()
|
|
nFeats = Input.featureCount()
|
|
counts = defaultdict(int)
|
|
for i, feat in enumerate(feats):
|
|
feedback.setProgress(int(100 * i / nFeats))
|
|
if feedback.isCanceled():
|
|
break
|
|
|
|
attrs = feat.attributes()
|
|
clazz = tuple([attrs[i] for i in fieldIdxs])
|
|
counts[clazz] += 1
|
|
|
|
for c in counts:
|
|
f = QgsFeature()
|
|
f.setAttributes(list(c) + [counts[c]])
|
|
sink.addFeature(f)
|