In a QGis python plugin i rasterize a vector layer with this code. All work fine except the callback: no error but nothing happen. The function callbackRasterize is never called

    gdal.RasterizeLayer(target_ds, [1], source_layer,
                        options=["ALL_TOUCHED=TRUE", attribute],

I found that using this code, the progress is visualized on standard output, but i can't use it with python:

    gdal.RasterizeLayer( .... callback=gdal.TermProgress)

How can I do to know when the rasterization process is over?

3 Answers 3


Alex is right, the call to gdal.RasterizeLayer is synchronous. There's little reason to include a callback unless you wish to obtain progress information, or to cancel the rasterisation part way through.

At a guess I'd say the method signature of your callbackRasterize doesn't match that expected by GDAL. Though a little trial and error I got the following to work.

def progress_callback(complete, message, unknown):
    print('progress: {}, message: "{}", unknown {}'.format(complete, message, unknown))
    return 1

Called by using something like:

exitcode = gdal.RasterizeLayer(targetds, [1], source_layer, 
                               burn_values=[1], callback = progress_callback )

Prints out the following:

progress: 0.0, message: "", unknown None
progress: 0.230125, message: "", unknown None
progress: 0.46025, message: "", unknown None
progress: 0.690375, message: "", unknown None
progress: 0.9205, message: "", unknown None
progress: 1.0, message: "", unknown None

Returning a 0 value from the progress_callback causes gdal.RasterizeLayer to return immediately with a non-zero exit code. I've no idea on what the unknown parameter is for, however it is required for the callback to work.


When you run normal Python, it's all done in sequence, I think. So you need to get the return, or check the result. It'll run inline.

So, you'll know it's finished when it returns! (I think)


How about this:

from osgeo import gdal
import progressbar as pb

def progress_callback(complete, message, data):
    percent = int(complete * 100)  # round to integer percent
    data.update(percent)  # update the progressbar
    return 1

pbar = pb.ProgressBar(max_value=100).start()

exitcode = gdal.RasterizeLayer(targetds, [1], source_layer, burn_values=[1],
                               callback=progress_callback, callback_data=pbar)


I'm not 100% sure this works though. With gdal.Warp it works a little differently:

options = {'callback': progress_callback,
           'callback_data': pbar}
gdal.Warp(dst, src, options=gdal.WarpOptions(**options))

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.