I am writing a QGis plugin with UI that should utilize certain QGIS processing tools when the user runs the plugin.
Now I tried a lot, searched a lot and came along quite many different approaches. First I want to succeed using QGis processes and second I want to make sure this survives the upcoming changes in QGis 3.0.
For now all I want is to use the process "gdalogr:cliprasterbyextent" and manged to use this one when opening the Python console and entering directly:
import processing
from qgis.core import QgsRasterLayer
raster = QgsRasterLayer('/tmp/test.tif', 'test.tif')
output = '/tmp/result.tif'
extent = '67.9986111111,97.5069444444,6.70138888889,37.1097222222'
processing.runalg('gdalogr:cliprasterbyextent', {'INPUT': raster, 'OUTPUT': output, 'PROJWIN': extent})
The same code in my Python plugin doesn't produce any output nor errors. It just runs through with not blocking the execution. I wasn't sure if the processing is properly callable from within my plugin? So I did a print("processing.runalg") and see that it is still a function. Yes, it is still a callable function and accessible from my Python plugin! I can also just use "processing.runalg('gdalogr:cliprasterbyextent')" and get the cliprasterbyextent help text when running my plugin. I also made sure the file exists and access rights are no obstacle.
I also saw examples where runalg is getting passed only arguments like:
processing.runalg('gdalogr:cliprasterbyextent', raster, '', extent, 5, 4, ...)
and I saw examples which first import and then initialize the Processing framework like this:
sys.path.append(QgsApplication.pluginPath())
from processing.core.Processing import Processing
Processing.initialize()
So far this all doesn't help me. I am lost and wonder why utilizing a simple process in my plugin seems so complicate. Why doesn't runalg throw any errors or block the execution for at least 1-3 seconds like when calling it from the QGis Console?
Could someone please indicate me what the right approach would be. I think, such cases should also be properly documented or an example guide should be in the QGis documentation/tutorials.