[QGIS-Developer] Python script to count the number of feature for each unique values

kimaidou kimaidou at gmail.com
Mon Feb 25 09:30:42 PST 2019


Hi all,

I needed to gather basic stats for a layer containing multiple fields. I
created the following gist to compute the number of features per each
unique value for a list of given fields
https://gist.github.com/mdouchin/a234efb7e67ebd8dae3a04cb26cf5e72

I know I could use PostgreSQL to do it (see
https://twitter.com/kimaidou/status/1100053546978983936 conversation), wich
is much faster, but I needed a quick way to do it with Python.
I tested virtual layers too, but my source layer has many fields (with some
heavy data) and the copy/pasting into spatialite was not efficient here.

The method uniqueValues(fieldIndex) of QgsVectorLayer is pretty fast
compared to my iteration through the layer features. Any hint how to
improve the speed of my calculation ?

Regards,
Michaƫl
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190225/5bae0660/attachment.html>


More information about the QGIS-Developer mailing list