I use addRasterLayer(rast,'Name') but this adds the raster to the top. It is possible to set position when I add raster at once? Is it possible??
Answer
By using the new Layer tree (aka legend or Toc) added by Martin Dobias since QGIS v.2.4, you can load a raster layer to any position of the ToC following these steps:
Get a reference of the layer tree
root = QgsProject.instance().layerTreeRoot()
Create the raster layer object
from PyQt4.QtCore import QFileInfo fileName = "/path/to/raster/file.tif" fileInfo = QFileInfo(fileName) baseName = fileInfo.baseName() mylayer = QgsRasterLayer(fileName, baseName)
Add the layer to the QGIS Map Layer Registry (the second argument must be set to False for you to specify a custom position in step 4.)
QgsMapLayerRegistry.instance().addMapLayer(mylayer, False)
Insert the layer in the third position of the ToC
root.insertLayer(2, mylayer)
No comments:
Post a Comment