TransWikia.com

Developing QGIS Action which loads a raster

Geographic Information Systems Asked by Daniele Piccolo on December 8, 2020

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 Devt1974-19841250k[% 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

2 Answers

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))  

Correct answer by Joseph on December 8, 2020

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 = '\rvphnas02pwProdotti_CartograficiOrtofoto2012_AGEATIFF_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 ) 

Answered by Daniele Piccolo on December 8, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP