1

I have layer which has close to 66 features each of them i have colored differently. Is there any way to know which color feature is which line on the layer? i.e If i choose a feature from layer toolbar, i want that feature to be highlighted in the layer.

Example: In the image below, if i click on 2, I want the feature 2 of the layer to be highlighted or shown or anything so that i know that this feature is feature 2. I want this to happen with all the features. Right now i have no idea which feature is what color graphically, I only know them by ID. I hope i am able to clearly state my problem, English is not my first language, pardon.

enter image description here

2 Answers 2

3

One way around this would be to use labels (right click on the layer > Properties > labels > select the id field). i.e by labeling your features based on their id(s), you can have each feature labeled with its id on the map and you don't really need to look at the table of content. Though this didn't really do the trick you needed for click/highlight, but I think this shall help and I couldn't post it as a comment either! :)

0
3

From a cartographic point of view, it should be said that if you can't readily distinguish visually between categories, then there is not much point to having the categorized style in the first place. So having a separate category for every feature id in a layer probably doesn't make a lot of sense and simply labelling the layer with feature id (as per @Esmaeel's answer) is a better approach. However, I found the question of highlighting a feature on the map canvas when its category is selected in the layer tree an interesting exercise. So here is a way to achieve it fairly easily with some pyqgis. This can be run directly from the Python console for testing, but for the script to work properly without modification, the layer must be categorized with the value $id. You would also need to modify it a bit to work in a plugin. The basic idea is to access the QItemSelectionModel() object of the layer tree view and connect to its selectionChanged() signal. In the slot function, we access the current layer and add a condition to isolate the logic to only that layer, using the layer name. We then access the current legend node and return its data item with index 2 which, in this case, corresponds to a feature id. We can then use the flashFeatureIds() method of QgsMapCanvas() to blink the feature on the canvas.

Example code snippet:

def was_changed():
    lyr = iface.layerTreeView().currentLayer()
    lyr_name = lyr.name()
    if lyr_name == 'newgen':
        current_legend_node = iface.layerTreeView().currentLegendNode()
        if current_legend_node is not None:
            feature_id = current_legend_node.data(2)
            iface.mapCanvas().flashFeatureIds(lyr, [int(feature_id)])
    

iface.layerTreeView().selectionModel().currentChanged.connect(was_changed)

Demo: enter image description here

To disconnect the slot from the signal, use the following:

iface.layerTreeView().selectionModel().currentChanged.disconnect(was_changed)

Link to docs for flashFeatureIds(). You can pass additional arguments to change the start color, end color, number of flashes and duration.

Your Answer

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

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