I've had lots of problems getting models to run that are exported from other-wise working QGIS graphic models. Searching previous questions, apparently this "save as python" functionality went away in previous versions of QGIS but is back on the v2.18.7 version that I am using on my Mac (OS X Yosemite 10.10.5).
The problem seems to arise whenever I try to either string output of one step into the next, or save output to a file (rather than just temporary output). In all the problem cases I have been able to do operations successfully in the graphic model buildier but these fail when I export to Python and attempt to run them within the QGIS script editor processing framework.
Here's an example: This version works (note that no output is saved):
##Warp2ImagesWORKED=name
##m02=raster
##m01=raster
outputs_GDALOGRWARPREPROJECT_1=processing.runalg('gdalogr:warpreproject', m01,'EPSG:4326','EPSG:32616',None,500.0,0,['-87.75,-83.1, 10.65, 15.1'],'EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,None,None)
outputs_GDALOGRWARPREPROJECT_2=processing.runalg('gdalogr:warpreproject', m02,'EPSG:4326','EPSG:32616',None,500.0,0,['-87.75, -83.1, 10.65, 15.1'],'EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,None,None)
However, when I attempt to save output to a file, it crashes. This is the output from save as python:
##Warp2ImagesCRASH=name
##m02=raster
##m01=raster
##mout=output raster
##mout=output raster
outputs_GDALOGRWARPREPROJECT_1=processing.runalg('gdalogr:warpreproject', m01,'EPSG:4326','EPSG:32616',None,500.0,0,['-87.75,-83.1, 10.65, 15.1'],'EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,None,mout)
outputs_GDALOGRWARPREPROJECT_2=processing.runalg('gdalogr:warpreproject', m02,'EPSG:4326','EPSG:32616',None,500.0,0,['-87.75, -83.1, 10.65, 15.1'],'EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,None,mout)
The processing output this gives is:
2017年06月14日T17:40:35 1 Error: Wrong parameter value: ['-87.75,-83.1, 10.65, 15.1'] 2017年06月14日T17:40:35 1 Error: Wrong parameter value: ['-87.75, -83.1, 10.65, 15.1'] 2017年06月14日T17:40:35 2 Error loading result layer: Traceback (most recent call last): File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/gui/Postprocessing.py", line 75, in handleAlgorithmResults out.name)) File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/tools/dataobjects.py", line 199, in load + '\nCheck the processing framework log to look for errors') RuntimeError: Could not load layer: /var/folders/8y/6924gdvx3jg6k2rz5yrzwz100000gr/T/processinge2fea9e1d32a408384b155e3ab4b6db5/9dbcc9629f5e4191bb177b1b7627186c/mout.tif Check the processing framework log to look for errors 2017年06月14日T17:40:35 2 Error loading result layer: Traceback (most recent call last): File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/gui/Postprocessing.py", line 75, in handleAlgorithmResults out.name)) File "/Applications/QGIS.app/Contents/MacOS/../Resources/python/plugins/processing/tools/dataobjects.py", line 199, in load + '\nCheck the processing framework log to look for errors') RuntimeError: Could not load layer: /var/folders/8y/6924gdvx3jg6k2rz5yrzwz100000gr/T/processinge2fea9e1d32a408384b155e3ab4b6db5/9dbcc9629f5e4191bb177b1b7627186c/mout.tif Check the processing framework log to look for errors
I do not know what the "processing framework log" is but perhaps it is the one that pops up as you are processing? In any event here is that:
Algorithm Warp2ImagesCRASH starting... Converting outputs Loading resulting layers
The following layers were not correctly generated. mout mout You can check the log messages to find more information about the execution of the algorithm
At first I thought the problem might be the two ##mout with the same names, so I renamed them to mout01 and mout02, respectively, but that yields the same error. I've read that warp in GDAL can be tricky at times, but given that these have worked in graphic model and in python when no output is saved, I don't think that is the cause here.
Is there any way to use the Save As Python to make these operations work?
I have basically nonexistent Python skills- just relying on experience with R and other programing environments and doing really basic editing of generated code.
2 Answers 2
Your log says:
2017年06月14日T17:40:35 1 Error: Wrong parameter value: ['-87.75,-83.1, 10.65, 15.1'] 2017年06月14日T17:40:35 1 Error: Wrong parameter value: ['-87.75, -83.1, 10.65, 15.1'] 2017年06月14日T17:40:35
so there are some problems with the input parameters. You specified the extent between square brackets, but they are not needed (just use a string, without any blank space inside it).
Then, I'm not sure that None
parameters would work, so try replacing the first one of them with 0
and the second one with ''
(the former because it is specified to leave 0 for no change, the latter is an empty string because that parameter requires a string).
You may try replacing your original code with the following one:
##Warp2ImagesCRASH=name
##m02=raster
##m01=raster
##mout_1=output raster
##mout_2=output raster
outputs_GDALOGRWARPREPROJECT_1=processing.runalg('gdalogr:warpreproject', m01,'EPSG:4326','EPSG:32616',0,500.0,0,'-87.75,-83.1,10.65,15.1','EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,'',mout_1)
outputs_GDALOGRWARPREPROJECT_2=processing.runalg('gdalogr:warpreproject', m02,'EPSG:4326','EPSG:32616',0,500.0,0,'-87.75,-83.1,10.65,15.1','EPSG:4326',1,0,75.0,6.0,1.0,False,0,False,'',mout_2)
Hopefully, it should work.
-
1Removing square brackets worked. There were no errors generated from None, so I did not change these. I should however emphasize 2 things: 1) the square brackets are automatically added when exporting from the graphic modeler. 2) these square brackets did not cause a problem when the the output was not used in subsequent operations or as saved to a file. Thus, the functionality of generating script from "Save as Python" appears to be not entirely robust (at least on this version on my OS X 10.10.x)Tom– Tom2017年06月15日 14:40:16 +00:00Commented Jun 15, 2017 at 14:40
-
@Tom thanks for the clarifications, I'm glad you solved the issue. And I agree with you about the robustness of the functionality, but it is quite recent and some time is needed for improving it.mgri– mgri2017年06月15日 15:09:50 +00:00Commented Jun 15, 2017 at 15:09
You can ́t just use the code from the graphical modeler as a python script to run the process. You need to create a standalone script. There are many questions and answers here on GIS-SE on the topic PyQgis.
The model-script output only has the code for the tools themself. All the needed imports and settings for a running python-script are not included. It is only for the use in the modeler.
Also you define an userinput with the ##. And you do not have a userinput in a standalone script.
-
2The code could be used if it is saved and executed as a script from the processing framework :)Joseph– Joseph2017年06月15日 09:21:07 +00:00Commented Jun 15, 2017 at 9:21
-
2Thats very true. But wouldn´t you use a .model file for it? Because you can´t input a model via python-script.Matte– Matte2017年06月15日 10:34:46 +00:00Commented Jun 15, 2017 at 10:34
-
2You're correct but running a script from the processing framework allows for greater flexibility and control provided you have some python knowledge. The models are great for users with little or no knowledge of python so perhaps the OP wants to add more to the script later on? :)Joseph– Joseph2017年06月15日 10:45:40 +00:00Commented Jun 15, 2017 at 10:45
-
Thanks. I should have specified that I am running resulting .py file within QGIS scripting framework (Script Editor | "Run Algorithm"), so all the trimmings of stand-alone .py code should not be necessary. Will edit after I process other suggestions. My understanding is that graphic models saved as .py should in theory be immediately runnable within this framework. Ultimately, I intend to string several operations together to automate/document processing (and as way to help learn Python). Graphic model will be cumbersome for scale I'm processing at.Tom– Tom2017年06月15日 13:46:24 +00:00Commented Jun 15, 2017 at 13:46