3

Qgis version : 2.18.1

I just want to print the ids of the selected features of a certain layer (I should specify that this layer is stored in a spatialite database), but even if I specified the correct one and manually selected features in it I can't access them via the console.

here is my code:

layer1 = QgsMapLayerRegistry.instance().mapLayersByName("Sites_atelier_vienne")[0]

selfeat = layer1.selectedFeatures()

and the result when I print :

print selfeat
[]

But my layer (and I checked, I have the right one in layer1) have selected features so I'm kind of stuck here.

5
  • code looks OK... what version of QGIS, and is this from the python console or somewhere else (plugin / processing script / standalone script)? Just tried this on QGIS 2.18.7 (from the python console) with a spatialite layer and your code seems to work for me.
    – Steven Kay
    Commented Jun 17, 2017 at 23:11
  • I'm using Qgis 2.18.1 and I just typed this code in the console but I want to use it in an init python function to link a vector layer and a table. I know the code is ok because I've already succeeded to do it and it seems to still works with other layers so I'm really puzzled. Commented Jun 18, 2017 at 9:07
  • What is the output of print layer1? And what happens when you do for feature in layer1.getFeatures(): print feature? Does it return anything? If all of above works fine, how about first setting selected features to none using layer1.setSelectedFeatures([]), then select some of them through Python console and then check what selectedFeatures() give you?
    – adamczi
    Commented Jun 18, 2017 at 12:03
  • 1
    Print layer1.name() gives me the name of the chosen layer. I, then, tried your suggestion and it printed me every features contained in the vector layer but still doesn't printed any selected features while I can assure there are. But I think I might be on a track here. When I ask to print every name of the layers via QgsMapLayerRegistry.instance().mapLayers.values() it gave me 4 layers with the exact name of the one I want to iterate over. Commented Jun 18, 2017 at 21:48
  • After reading this other thread [link]gis.stackexchange.com/questions/160565/… and testing with another way to access layers (lay = qgis.utils.iface.legendInterface().layers()) that worked for me it seems that the problem resides in the QgsMapLayerRegistry instance. Anyway thanks for your help Steven and @adamczi Commented Jun 18, 2017 at 22:06

2 Answers 2

1

I'm having the same issue on QGIS 2.14.3. I solved it like this:

layer1 = QgsMapLayerRegistry.instance().mapLayersByName("Sites_atelier_vienne")

for lay in layer1:
    if lay.selectedFeatureCount() > 0:
       laysel = lay
selfeat = laysel.selectedFeatures()

It may not be elegant but it works.

1

I've find the answer to this problem. It seems that my layer ("Sites_atelier_vienne") was redundant in the layer registry. I don't know yet exactly why but this code helped me to clean the registry of redundant layers

iface = qgis.utils.iface
registryLayers = QgsMapLayerRegistry.instance().mapLayers().keys()
legendLayers = [ layer.id() for layer in iface.legendInterface().layers() ]
layersToRemove = set( registryLayers ) - set( legendLayers )
QgsMapLayerRegistry.instance().removeMapLayers( list( layersToRemove ) )

Thanks everyone for helping me !

1
  • Yeah! I have the same problem with redundand layer in the registry. Very good!
    – Sassá
    Commented Aug 23, 2017 at 11:49

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.