To get all features of a layer by the layer name you do not need to activate it. Just use
name = 'counties'
layer = QgsProject.instance().mapLayersByName( name )
poles = layer.getFeatures()
for pole in poles:
print('it is the north pole')
First of all, you do not need to care about the active layer. The active layer is the layer which is currently selected in the layer tree and therefore is nice for two things
- letting the user select a layer to operate on
- selecting a layer for quickly testing code from the python console
The second one is handy when you are developing, the first one is what all the maptools are based on: the currently selected layer (the active one) is modified, selected, deleted...
Methods to access layers
However, you seem to want to access layers not based on user input but based on something else.
All layers have
- An id that needs to be unique
- A name that is not necessarily (but often) unique and can be changed by the user
- A reference (also often referred to as pointer but in python the two terms are interchangeable) that is unique and stable for the lifetime of a layer during the application run time and can be assigned to a variable (e.g.
vlayer is a reference to the currently active layer in your example).
- Properties like the URI (table name etc.)
If you already have a reference to the layer (because you created/added it in your plugin) just use that one.
If you don't, think about what property you want to use to get access to the layer. Most likely something that is stable (the layer id is very stable for the lifetime of a project but always is different for different projects), the name is user adjustable, the table may depend on the data source.
There is the map layer registry which lets you access layers. It manages references to all layers. This used to be called QgsMapLayerRegistry, but that is no longer available in QGIS 3, so QgsProject can be used instead.
registry = QgsProject.instance()
If you have the layer id go with
layer = registry.mapLayer( id )
If you have the layer name
layer = registry.mapLayersByName( name ) # The method returns a list since it can be ambiguous, 0 picks the first entry in the list
If you have something else you have to write your own code:
for lyr in registry.mapLayers().values():
if lyr.dataProvider().dataSourceUri().table() == 'mytable':
layer = lyr
Or a shorthand that does the same and makes you feel more hacker'ish but will make the people who will have to maintain the code later on cost some extra time to decipher:
layer = (l for l in registry.mapLayers().values() if l.dataProvider().dataSourceUri().table() == 'mytable').next()