As far as I see, on QgsMapLayerRegistry.instance()
there is a method to select layers:
mapLayersByName(Qstring)
and the method
mapLayer(QString)
respectively, wich accesses a layer by its id.
As the name says, mapLayersByName()
selects layers by their name in the legend, wich causes scripts or plugins to fail if a user renames a layer/table (i.e. renaming a table '160520_parcels' to 'Parcels').
So is there a way to access a layer by the name of it's origining table in PyQGIS?
update
something like ...
def selectLayerByOriginalName(tablename):
# return layers whose orininal names start with 'tablename'
llist = []
for l in QgsMapLayerRegistry.instance().mapLayers().values():
if tablename in l.id():
if l.id().index(tablename) == 0:
llist.append(lyr)
return llist
... works, but is there a more convenient approach?
OriginalName()
method (docs) inQgsMapLayer
, but strangely it is altered once the user changes the layer name. I agree that using theid()
(extracting the first part of it) seems to be the only way. However, using your code, looking for a "Roads2" layer, a layer called "Roads" would also match and enter the 2if
s. I'd rather try to remove the timestamp.