The following code inserts my
new_layer just above the active layer in the QGIS ToC.
new_layer = iface.addRasterLayer(...)
Instead, I'd like my
new_layer to be inserted at the top of the QGIS ToC.
Do you know how can I do it?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
By using the new Layer tree (aka legend or ToC) added by Martin Dobias since QGIS v.2.4, you can load a layer to the top of the ToC following these steps:
Get a reference of the layer tree
root = QgsProject.instance().layerTreeRoot()
Create the 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
Insert the layer at the top of the ToC (position