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.

UPDATE Now, way is a linestring. I tried your code and at the beginning it gave a few hours on the name of the columns that I changed. Then I inserted the centroid of station in the variables of the ST_LineLocatePoint, because it asked me for a point as a second argument. I entered the new code in the application update. everything works but the order is null.

WITH

dumped_ways AS ( SELECT name, dmp.path1 AS way_part, dmp.geom FROM s_602_ptrc.way, LATERAL ST_Dump(geom) AS dmp )

SELECT ROW_NUMBER() OVER(PARTITION BY nway.name, nway.way_part ORDER BY nway._frac) AS id, station.nome AS station, nway.name, nway.way_part, station.geom FROM s_602_ptrc.station CROSS JOIN LATERAL ( SELECT dumped_ways.name, dumped_ways.way_part, ST_LineLocatePoint(dumped_ways.geom, ST_Centroid(station.geom)) AS _frac FROM dumped_ways ORDER BY dumped_ways.geom <-> ST_Centroid(station.geom) LIMIT 1 ) AS nway ORDER BY nway.name, nway.way_part, nway._frac ;

enter image description here

I think the problem is in Dumped_ways query. This is the result enter image description here

Daniele Piccolo
  • 909
  • 4
  • 9

2 Answers2

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))  
Joseph
  • 75,746
  • 7
  • 171
  • 282
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 ) 
Daniele Piccolo
  • 909
  • 4
  • 9