Using part of the code in "Developing QGIS Action which loads a raster"
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 )
How can I load the rasters that intersect a polygon feature using an action?
In my case I have a layer with the grid of the boundaries of rasters (catalogo_CTR) and a layer with administrative boundaries (Comuni).
For the "Comuni" I need an action that load the rasters corresponding to the boundaries that are intersected (i.e. 8 rasters). The action should load raster in a group layer like the action linked at the beginning.