10

I am looking for solution to get the unique values of an attribute of a vector layer.

What is the best way in PyQGIS to get the unique values of one distinct attribute?

I have polygons described by an attribute "group". Each group contains 1 to about 10 polygons. I want to process the polygons belonging to a certain group in for-loop, i.e. for each iteration of the for-loop i select the polygons with setSubsetString to process this group, e.g. to write them to a shape file. However I do not know the group before reading the vector layer.

I work with QGIS 1.8.0.

Taras
35.7k5 gold badges77 silver badges151 bronze badges
asked Oct 8, 2013 at 17:27
2
  • 2
    There is a split tool which will create separate shapefiles depending on an attribute if that's all you need. Otherwise, that plugin would also be the place to look for sample code. Commented Oct 8, 2013 at 18:58
  • From the plugin you were mentioned the function split in splitterdialog.py is what i was looking for. Thank you for the hint! Commented Oct 8, 2013 at 20:04

3 Answers 3

25

QGIS provides a method .uniqueValues(fieldIndex) of the QgsVectorLayer class to get this information. You need to get the index of the field in question and then query the layer for unique values.

vectorLayer = iface.activeLayer()
idx = vectorLayer.fields().indexOf('FIELD_NAME')
values = vectorLayer.uniqueValues(idx)
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Aug 28, 2015 at 17:38
0
8

Python's sets are handy for finding unique values. For example:

>>> set(range(4) + range(5) + range(6))
set([0, 1, 2, 3, 4, 5])

Try something like this:

PyQGIS 2.0:

values = set()
for feature in layer.getFeatures():
 values.add(feature[ATTRNAME])

PyQGIS 1.8:

values = set()
layer.select()
for feature in layer:
 index = layer.fieldNameIndex(ATTRNAME)
 values.add(feature.attributeMap()[index].toInt())
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Oct 8, 2013 at 21:33
4

There is a QGIS algorithm for that called "List unique values"

layer = iface.activeLayer()
params = {
 'INPUT' : layer,
 'FIELDS' : ['FIELD_NAME'],
 'OUTPUT' : 'TEMPORARY_OUTPUT',
 'OUTPUT_HTML_FILE' : 'TEMPORARY_OUTPUT'
 }
result = processing.run("qgis:listuniquevalues", params)
print(result['UNIQUE_VALUES'])
Taras
35.7k5 gold badges77 silver badges151 bronze badges
answered Jul 17, 2015 at 23:00
1
  • Dear @saheka, I decided to rollback your answer, because I noticed, that your initial version was unique (the last edit version overlapped too much with MatthiasKuhn's answer) and it is still usable. Commented Jun 20 at 7:44

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.