mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-04 00:30:59 -05:00
33 lines
925 B
Python
33 lines
925 B
Python
##Table=group
|
|
##Input=vector
|
|
##Fields=Field Input
|
|
##Frequency=output table
|
|
|
|
from processing.tools.vector import TableWriter
|
|
from collections import defaultdict
|
|
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
|
|
|
|
layer = processing.getObject(Input)
|
|
inputFields = layer.fields()
|
|
fieldIdxs = []
|
|
fields = Fields.split(',')
|
|
for f in fields:
|
|
idx = inputFields.indexFromName(f)
|
|
if idx == -1:
|
|
raise GeoAlgorithmExecutionException('Field not found:' + f)
|
|
fieldIdxs.append(idx)
|
|
writer = TableWriter(Frequency, None, fields + ['FREQ'])
|
|
|
|
counts = {}
|
|
feats = processing.features(layer)
|
|
nFeats = len(feats)
|
|
counts = defaultdict(int)
|
|
for i, feat in enumerate(feats):
|
|
progress.setPercentage(int(100 * i / nFeats))
|
|
attrs = feat.attributes()
|
|
clazz = tuple([attrs[i] for i in fieldIdxs])
|
|
counts[clazz] += 1
|
|
|
|
for c in counts:
|
|
writer.addRecord(list(c) + [counts[c]])
|