reproject and filetype change in python/pyqgis for QGIS plugin

In my current work on the qgis2leaf plugin I had the idea to place raster data on a leaflet map as an image overlay. With this in mind and looking at a webmap I needed to consider a good filesize, a strict projection of EPSG:4326 and a strict filetype as well. So decision was: projecting everythin to EPSG:4326 and changing file type to *.jpg. I know, how to do this in the Terminal and in QGIS. But what options do you have using python/ pyqgis only?


For doing this work in the terminal/shell/command line the one and only choice is called GDAL. You can install this nice set of functions on every OS (see instructions here). Once you have what you need the repojection and filetype change is done with a one-liner:

gdal_translate -of jpeg -outsize 100% 100% -a_srs EPSG:4326 RASTER_NAME_IN RASTER_NAME_OUT

If you want to call this from inside of Python everyone states to use subprocess:

import subprocess["gdal_translate","-of","jpeg","-outsize","100%","100%","-a_srs","EPSG:4326","RASTER_NAME_IN","RASTER_NAME_OUT"], shell=True)

Despite the argument shell=True is a security problem, the above line of python code worked only on Ubuntu in my case. In Win and Mac it only worked when I provide the whole path to the executables of GDAL. As this depends on the OS and the user/pc this wasn’t an option to create reprojected jpgs in a QGIS plugin. As I wasn’t able to determine the GDAL installation path I was somehow stucked. So I looked back at QGIS.


In QGIS 2.0.1 an above you have several different options to reproject a raster file:

1) the GDAL_translate dialog. You can find this in Raster->Conversion->Translate which calls the above described function gdal_translate directly via shell/terminal/command line and import the result to your QGIS library.

GDAL translate jpg
GDAL translate dialog with “save as” JPG option

2) the GDAL warp dialog. With this dialog you can reproject rasters via Raster->Projections->Warp. It is also possible to save the result as a jpg. So it is quite comparable to GDAL_translate:

GDAL warp dialog QGIS
GDAL Warp dialog

But above options depend on the GDAL tools plugin which needs to be installed. Unfortunately I don’t know how to call those scripts from inside my plugin… So there must be another solution.

3) The processing toolbox. Together with QGIS 2.0.1 the former Sextante toolbox was implemented into QGIS itself. That means it ships together with QGIS already. And the next advantage is: you can use it out of the python terminal. Processing offers access to GDAL functions as well:

GDAL warp in processing toolbox
processing version of GDAL’s warp function
The only problem with processing and it’s implementation of warp and translate function: You can’t save it as JPG files…


So now I use a combination of processing and the GDAL Python bindings (somehow a translation of GDAL to work in Python only):

import processing
from ogr import gdal
processing.runalg("gdalogr:warpreproject","RASTER_NAME_IN","EPSG:XXXX","EPSG:4326",0,1,"","RASTER_NAME_TMP") # this uses the processing version of the warp function from GDAL
format = "jpeg" #define the driver extension that is needed in the end
driver = gdal.GetDriverByName( format ) #define the driver that is needed in the end
src_ds = gdal.Open(RASTER_NAME_TMP) #open the temp raster which is now in EPSG:4326
dst_ds = driver.CreateCopy( RASTER_NAME_OUT, src_ds, 0 ) #creates a copy in the jpg format
dst_ds = None #free the dataset	to save it 
src_ds = None #free the dataset to save it

And that’s it. If you know other alternatives, drop us a line…

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments

[…] post reproject and filetype change in python/pyqgis for QGIS plugin appeared first on Digital […]

6 years ago

> In QGIS 2.0.1 an above you have several different options to reproject a raster file:
> 1) the GDAL_translate dialog.

Just correcting some misinformation: gdal_translate can only convert the format, it does not reproject, as that is what gdalwarp is for.