[GIS] How to identify and highlight features programmatically

pyqgisqgis

In QGIS there is an option to select features of a selected layer using a "identify feature" tool. Then the feature get highlighted in a different color and attribute dialog is poping up.
like that i want to highlight the multiple features programatically.

Ex: I have a set of vector layers which consists of number of roads as features. I need to select few roads by looking at its attributes and highlight.

Is there any way of achieving this using python code?

Best Answer

After few days of playing with QGIS, finally found a solution. There is a method in QgsVectorLayer class called setSelectedFeatures (const QgsFeatureIds &ids).

selection=[]

for feature in layer.getFeatures():
    geom = feature.geometry()
    roadNo = feature.attribute("Road_no")
    if roadNo == row[0].strip():
      selection.append(feature.id())
 ----
 ----
 layer.setSelectedFeatures(selection)

Hope this helps,

Related Question