I have been working in this code but I am getting the following error:

Traceback (most recent call last): File "", line 1, in File "c:/users/miguel~1/appdata/local/temp/tmpdsyhqn.py", line 17, in processing.load(reproj["G:\pyQGIS\MIRAR EN MI ORDENADOR\nuevo.asc"]) KeyError: 'G:\pyQGIS\MIRAR EN MI ORDENADOR\nuevo.asc'

In QGIS, this error is printed with "\"in every path, even if I only write "\" in my paths of the code.

import processing   
from processing.core.Processing import Processing
from qgis.core import *
from qgis.gui import *


raster = QgsRasterLayer("G:\\pyQGIS\MIRAR EN MI ORDENADOR\\MDT05-0559-H30-LIDAR.asc","MDT05")
print rasterLyr.isValid()
crs = QgsCoordinateReferenceSystem(25830, QgsCoordinateReferenceSystem.EpsgCrsId)

reproj = processing.runalg("gdalogr:warpreproject", raster, "EPSG: 25830", "EPSG:4326", '0', 0, 0, 0, 0, 2, 1, 1, False, 1, False, "extraParam", "G:\\pyQGIS\\MIRAR EN MI ORDENADOR\\nuevo.asc")
processing.load(reproj["G:\\pyQGIS\\MIRAR EN MI ORDENADOR\\nuevo.asc"])

Can you tell me the code: print(repr(reproj)) which makes the following output?

{'OUTPUT': 'G:\\pyQGIS\\MIRAR_COMP\\nuevo.tif'}
  • `G:\\pyQGIS\MIRAR EN MI ORDENADOR\` is not a valid path for processing (spaces) – gene Apr 20 '16 at 16:01
  • Can you insert the debug statement print(repr(reproj)) just before the last line? – Matthias Kuhn Apr 20 '16 at 21:28
  • I have changed the routes: MIRAR_COMP (I have eliminated the spaces) and I have included the code you told. This is the output: {'OUTPUT': 'G:\\pyQGIS\\MIRAR_COMP\\nuevo.tif'} I HAVE USED NOW A .tif – user64823 Apr 21 '16 at 7:59
  • I can't find any "nuevo.tif" file output – user64823 Apr 21 '16 at 8:00
  • I rolled back your last change since it converted the question into an answer which makes it hard to follow for future readers. Please leave comments for updates like this. FYI: You can also answer your own question. – Matthias Kuhn Apr 21 '16 at 9:20

The return value of processing.runalg is a dictionary containing key, map pairs where the keys are defined by the algorithm.

In the case of gdalogr:warpreproject there is one output with the name OUTPUT created which points to the location where the result is. To access it, you can use the following code:


About \ and /

Sidenote: Windows normally uses \ for separating paths which was a terrible decision for programmers because the same character is used for escape sequences in strings, that's why you have to double them. Sometimes.

I recommend always using / instead of \. It's understood by Windows but also by other platforms, so it makes your code portable. It's also easier to read and write because you don't have to double the character. If you run into troubles you can still use pythons os.sep which will dynamically translate to the appropriate character for the current platform.

  • Thank you very much. Sorry for the changes. May I ask a question? Why does EXTRA <ParameterString> accept an empty string? Thank you very much – user64823 Apr 21 '16 at 10:07
  • I don't know but if you don't find the answer and deem the question important enough, ask a new question :) – Matthias Kuhn Apr 21 '16 at 10:10
  • According to loading the new layer, get this error: KeyError: 'G:\\pyQGIS\\MIRAR_COMP\\nuevo.tif' when I establish the same route of output. Is it possible to use a QgsRasterLayer to load the otput? – user64823 Apr 21 '16 at 13:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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