5

I'm using the action code of this question: "How to create a QGIS Action which loads a raster?"

qgis.utils.iface.addRasterLayer('E:\\Plot Sheet Devt\\1974-1984\\1250k\\[% Grid_Ref %].TIF', '[% Grid_Ref %]')

It is very useful for my work and I would need some more functions. The first one is that the action layer remains selected after the action. Now after loading raster It doesn't remain selected. I tried to add this code:

vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')
iface.setActiveLayer(vl)

but It doesn't work.

Other functions could be:

  • to automatically assign SRC to loaded rasters
  • to create a group of layer where to load the raster.
4

Try using the following code in your Action Text which should do the following:

  • Keeps your 'catalogo_CTR' layer as active
  • Creates a new group to contain the raster layers if one does not exist
  • Sets the crs for any loaded raster
  • Adds the raster layer to the group with the crs applied

Here is the code:

from PyQt4.QtCore import QFileInfo
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "Raster layers"
group = root.findGroup(group_name)
if group == None:
    group = root.addGroup("Raster layers")
else:
    pass
fileName = 'E:/Plot Sheet Devt/1974-1984/1250k.tif'
fileInfo = QFileInfo(fileName)
baseName = '[% Grid_Ref %]'
rlayer = QgsRasterLayer(fileName, baseName)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(27700) 
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))  
  • Thanks @Joseph. It's almost perfect. The only thing that does not work is crs.createFromSrid. I assign 3003 (Gauss Boaga Ovest) but every time It asks me to assign manually the CRS. – Daniele Piccolo Jan 23 '17 at 11:41
  • @DanielePiccolo - Interesting, I cannot confirm this behavour. If I replace 27700 with 3003, the raster layers are set to (EPSG:3003, Monte Mario / Italy zone 1) without me having to manually assign a crs. Which QGIS version are you using? I tested this on QGIS 2.18.2 for Win7 64-bit. – Joseph Jan 23 '17 at 11:47
  • I use your own version. QGIS 2.18.2 for Win7 64-bit. Really strange – Daniele Piccolo Jan 23 '17 at 11:53
  • @DanielePiccolo - There was a typo in the path to the raster layer (i.e. fileName). Perhaps that was the culprit? I've edited my post =) – Joseph Jan 23 '17 at 11:55
  • 1
    If I set a different CRS project and i don't set manually the CRS to the load raster (pressing CANCEL), However, the raster has the right CRS (3003). For me It,s perfect so. Thank you very much Joseph. – Daniele Piccolo Jan 23 '17 at 12:19
1

Searching in this forum I met: "How can I specify the CRS of a raster layer in pyqgis?" that helped me. Using the below code the CR dialog window does not open. A warning appears saying the CR raster will be set on 4326 but instead, at the end of action, the raster has the right CR (3003). So it works well despite the warning message:

from PyQt4.QtCore import QFileInfo,QSettings
from qgis.core import QgsRasterLayer, QgsCoordinateReferenceSystem

s = QSettings()
oldValidation = s.value( "/Projections/defaultBehaviour" )
s.setValue( "/Projections/defaultBehaviour", "useGlobal" )
vl = QgsMapLayerRegistry.instance().mapLayersByName('catalogo_CTR')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "Raster layers"
group = root.findGroup(group_name)
if group == None:
    group = root.addGroup("Raster layers")
else:
    pass
fileName = '\\\\rvphnas02pw\Prodotti_Cartografici\\Ortofoto\\2012_AGEA\\TIFF_GBO\\[% A_CODICE %].tif'
fileInfo = QFileInfo(fileName)
baseName = '[% A_CODICE %]'
rlayer = QgsRasterLayer(fileName, baseName)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(3003) 
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer)) 

s.setValue( "/Projections/defaultBehaviour", oldValidation ) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.