2

I have a grid layer (lidar_cortina) with "NAME" and "percorso"(path) coloumns:

I'm trying to write an QGIS action that loads file .asc from a grid layer.

qgis.utils.iface.addRasterLayer("[%percorso%]", "[%NAME%]") 

but It doesn't work and the message "the command can not be executed" appears

If I use the console loading one .asc it works well

qgis.utils.iface.addRasterLayer("//rvphnas02pw/Remote_Sensing/Lidar/2017_Lidar_Cortina/SECONDA_CONSEGNA/Lidar/DTM/P740451578_0101_100_DTM.asc", "P740451578_0101_100")

Any suggestion.

UPDATE

If I use this code, it works well:

qgis.utils.iface.addRasterLayer('//rvphnas02pw/Remote_Sensing/Lidar/2017_Lidar_Cortina/SECONDA_CONSEGNA/Lidar/DTM/[%NAME%]_DTM.asc', '[%NAME%]')

Using the code of: Developing QGIS Action which loads a raster I have write an other qgis action to open .asc file from a grid layer with "NAME" column:

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

vl = QgsMapLayerRegistry.instance().mapLayersByName('lidar_cortina')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "lidar"
group = root.findGroup(group_name)
if group == None:
    group = root.addGroup("lidar")
else:
    pass
fileName ='D:/GIS/Dati/lidar/[% NAME %]_DTM.asc'
fileInfo = QFileInfo(fileName)
baseName = '[% NAME %]'
qgis.utils.iface.addRasterLayer(fileName, baseName)
rlayer = QgsRasterLayer(fileName, baseName)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(32632) 
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))

but It doesn't work.

Error Message

Using python console to open one .asc file with the same code of error message, It works:

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

vl = QgsMapLayerRegistry.instance().mapLayersByName('lidar_cortina_prova')[0]
qgis.utils.iface.setActiveLayer(vl)
root = QgsProject.instance().layerTreeRoot()
group_name = "lidar"
group = root.findGroup(group_name)
if group == None:
    group = root.addGroup("lidar")
else:
    pass

fileName ='D:/GIS/Dati/lidar/P740451578_0101_100_DTM.asc'
baseName='P740451578_0101_100'
fileInfo = QFileInfo(fileName)
rlayer = QgsRasterLayer(fileName, baseName)
crs = QgsCoordinateReferenceSystem()
crs.createFromSrid(32632) 
rlayer.setCrs(crs)
QgsMapLayerRegistry.instance().addMapLayer(rlayer, False)
group.insertChildNode(-1, QgsLayerTreeLayer(rlayer))

I can't understand Any suggestion is welcome.

UPDATE 2

changing the path with the suggestion in comment of @lrssvt, I obtain the same error:

enter image description here

Daniele Piccolo
  • 909
  • 4
  • 9

1 Answers1

1

This could be due to how the path of the file is interpreted from python.

I guess you can do the following edit to solve the issue:

from:

fileName = 'D:/GIS/Dati/lidar/P740451578_0101_100_DTM.asc'

to:

fileName = 'D:\\GIS\\Dati\\lidar\\P740451578_0101_100_DTM.asc'

If you have the path in attribute table you can edit it using the field calculator with the expression:

replace("path", '/', '\\\\')

lrssvt
  • 1,921
  • 12
  • 9