1

This code snippet gives me the unique values for a vector layers attribute called myfieldname

l = iface.activeLayer()
attrIndex = l.fieldNameIndex('myfieldname')
uniValues = l.uniqueValues(attrIndex)

With an active object filter I only get the unique values of a subset matching the filter criteria.

Is there a way to override this behaviour, in order to get all unique values for an attribute (of the complete layer) even with an active object filter?

(applies also for featureCount() etc.)

  • Is the initial subset filter being applied by code? – artwork21 Jul 27 '16 at 19:59
  • Yes it is. There must be a flag or something that tells pyqgis to ignore an existing filter, like the checkbox in the object filter dialog does. – Jochen Schwarze Jul 28 '16 at 6:29
  • Can you just clear the filter before running the unique value function and then reply the filter (e.g. clear filter LYR.setSubsetString(''))? – artwork21 Jul 28 '16 at 12:13
1

Maybe you need use a filter expression.You can try this. For example

1: Step.

Apply your filter.

iface=qgis.utils.iface
l = iface.activeLayer()
l.setSubsetString('name='"\'Balneario Las Arenas\'")

mView= iface.layerTreeView()
node = iface.layerTreeView().currentNode()
nodeslist=mView.selectedLayerNodes()
newValue = node.customProperty("showFeatureCount", 0 ) 
for value in nodeslist:
    value.setCustomProperty("showFeatureCount", int(newValue));

2: Step.

Apply filter expression.

l = iface.activeLayer()
selection =l.getFeatures(QgsFeatureRequest(QgsExpression('name='"\'Balneario Las Arenas\'")))   
for feat in selection:
    attrIndex = l.fieldNameIndex('myfieldname')
    uniValues = l.uniqueValues(attrIndex)

it's a suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.