I have got some GPS tracks (MultiLineString). As I import them to QGIS, they look like this:


I would like to merge them and to do so, I have to rename all of them like this (picture 2) otherwise an error occurs (all tracks become one single track at the same position).


I usually give numbers as names (e.g. 01-200). Sometimes I have only a few tracks but sometimes a lot.

Is it possible to rename all the tracks (layers) without renaming them one-by-one by hand?

2 Answers 2


You could try using the following in the Python Console:

x = 0

for layer in QgsMapLayerRegistry.instance().mapLayers().values():
    x = x + 1

Layer panel

The zfill() command adds zeroes to the left of each layer name to fill the width. You can specify this with the total number of layers you want to add. This helps with sorting the layers in order.

E.g. with zfill(2):

1 = 01
9 = 09

With zfill(3):

1 = 001
90 = 090
  • Should I use Python Console and write these to there? ( I am sorry, I am not familiar with these kind of expressions)
    – Skye
    Feb 23, 2016 at 15:27
  • Yes, sorry should have mentioned. You can copy/paste the code from the answers provided into the Python Console =)
    – Joseph
    Feb 23, 2016 at 15:29
  • @Skye - Awesome, glad it works :)
    – Joseph
    Feb 23, 2016 at 15:35

try this:

canvas = qgis.utils.iface.mapCanvas() 
allLayers = canvas.layers() 
i = 1

for bla in allLayers: 
    name = "layer_"+str(i)
    i = i + 1
  • 1
    Not sure you can use qgis.utils.iface.mapCanvas().layers() for changing layer names. Your code works great if you use QgsMapLayerRegistry.instance().mapLayers().values() though :)
    – Joseph
    Feb 23, 2016 at 15:41
  • 1
    huh, strange. I tried it in a project just quickly before I postedd and it worked... but mapLayers().values() is the better option. I'll try again later in different project. Would have to find out why ;)
    – Jason
    Feb 24, 2016 at 9:53
  • 1
    definitely works for me; just for visible layers though. I'm a windows/QGis 2.12.0 user... screenshot oh well, still your way seems to do the job for all layers; no matter if they are visible or not. allLayers = QgsMapLayerRegistry.instance().mapLayers().values() i = 1 for bla in allLayers: name = "layerneu_"+str(i) bla.setLayerName(name) i = i + 1
    – Jason
    Feb 24, 2016 at 12:51
  • 1
    Really sorry buddy, you are correct that your code does work for visible layers! That was a "Do'h!" on my part.
    – Joseph
    Feb 24, 2016 at 12:57
  • 1
    Haha, no worries. your way has the cool 00's and I think thats what Skye wanted ;)
    – Jason
    Feb 24, 2016 at 13:00

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.