3

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.

2

1 Answer 1

3

I can partly answer my own question!

The following works now also in my Python plugin: The reason was that I had a differed order of xmin/xmax/ymin/ymax for PROJWIN in the plugin code. Thus the module failed but did not throw any errors. Yes sometimes you don't see the wood for the trees ;-)

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})

BUT! I don't get the processing running as soon as I use it in an extra QThread. When using in the plugin thread, a simple "import processing" is all you need. As I want to parallelize my processing calls I use workers in a QThread. There are some examples here how to do processing in QThreads, to make your QGis plugin interruptible/stoppable with at least on QThread executin the plugin logic. I wonder if it is possible at all to use the processing module in a QThread or what approach would help me in this case.

Do I need to initialize the processing or a QGis application in every thread?

Your Answer

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

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