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

2
  • Maybe a subtility with the unc naming of the files ? Did u try with the full paths ?
    – Snaileater
    Commented Dec 6, 2017 at 9:54
  • "percorso" is a full network path Commented Dec 6, 2017 at 9:57

1 Answer 1

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", '/', '\\\\')

3
  • With your suggestion I obtain the same error. Look the update 2 in question. Commented Dec 7, 2017 at 11:43
  • Please, could you provide what the log message says by clicking the icon to the bottom right of the status bar?
    – lrssvt
    Commented Dec 7, 2017 at 11:48
  • No log message. The only error message is that of the image. I don't understand I use this action with a lot of other file directory. Commented Dec 7, 2017 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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