3

Code below changes the color of all the selected features based on the last condition it satisfied instead of changing the color of the feature which satisfied the condition.

for f in FEATURES:

    total = f['parkeer_approx']
    alloted = parking_capacity[f.id()-int(1)]['parking_capacity']

    print(total,alloted)

    if total == alloted:
        print('in red')
        iface.mapCanvas().setSelectionColor(QColor("red"))

    elif alloted > 0 and alloted < total:
        print('in orange')
        iface.mapCanvas().setSelectionColor(QColor("yellow"))

    else:
        print('in green')
        iface.mapCanvas().setSelectionColor(QColor("green"))

What I want is to change the color of each selected feature separately based on the conditions defined in the code above.

How can I achieve that?

6
  • 2
    Don't think it's possible to have more than one selection colour at any time. The easiest would be to set a style for the layer such as a rule-based style and set your expressions with your chosen colour. Perhaps this post might help: How to programmatically set a rule-based renderer in QGIS?
    – Joseph
    Jul 30, 2019 at 10:09
  • @Joseph is it possible to apply rule-based style on selected features only? If yes then how? Jul 30, 2019 at 10:14
  • 2
    In QGIS 3, there is a function in the rule-based style called is_selected() which checks if the feature is selected. You could set up an if expression to apply a style if the feature is selected or not. I don't think this function is avialable in QGIS 2 though.
    – Joseph
    Jul 30, 2019 at 10:18
  • ok ok ,Btw thanks for the help though @Joseph Jul 30, 2019 at 10:23
  • No problem, good luck :)
    – Joseph
    Jul 30, 2019 at 10:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.