I have loaded two layers in canvas and would like to move the layers using the following code in TOC, but it is not moving the layers to the position based on the index given.
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
from qgis.gui import *
import qgis.utils
canvas = qgis.utils.iface.mapCanvas()
layers = canvas.layers()
for i in layers:
if i.name() == "Layer2":
alayer = i
elif i.name() == "Layer1":
blayer = i
qgis.utils.iface.legendInterface().moveLayer(alayer, 0)
qgis.utils.iface.legendInterface().moveLayer(blayer, 1)
canvas.refresh()
How can I do that?