Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a QGIS plugin in python. I need to show a "Save as" dialog window in order to allow the user to select the path of the output raster file. Do I need to create a QFileDialog and manually add all the raster formats supported by QGIS or does it exist a method to load these formats from somewhere? Or, better, is there a method to show a "Save As" dialog window ready to manage raster files?


share|improve this question
up vote 5 down vote accepted

Browsing the QGIS source code, I've found a specific function, called buildSupportedRasterFileFilter. This code works well in the QGIS python console:

from PyQt4.QtCore import *
a = QString()
# "a" contains a string that can be used in a Dialog Window.

Now I'm browsing for a similar method that excludes read-only formats.

EDIT: There is a very good implementation of a procedure to open raster files in the GDAL Tools plugins. To use it:

  1. Download the QGIS source code. Copy the file [source_dir]/python/plugins/GdalTools/tools/ to your plugin directory.
  2. Inside your dialog window class, import the module:

    import GdalTools_utils as Utils
  3. implement the following method:

    def get_raster_name(self):
        lastUsedFilter = Utils.FileFilter.lastUsedRasterFilter()
        # rasterize supports output file creation for GDAL 1.8
        gdalVersion = Utils.GdalConfig.version()
        if gdalVersion >= "1.8.0":
            fileDialogFunc = Utils.FileDialog.getSaveFileName
            fileDialogFunc = Utils.FileDialog.getOpenFileName
        outputFile = fileDialogFunc(self, "Select the raster file to save the results to" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter)
        if outputFile.isEmpty():
        # required either -ts or -tr to create the output file 
        if gdalVersion >= "1.8.0":
            if not QtCore.QFileInfo(outputFile).exists():
                QMessageBox.information( self, "Output size required" ), "The output file doesn't exist. You must set up the output size to create it." ) )
        return outputFile

Now, call the method when you need a file. The code is taken from an another file of the Gdal Tools ( Remember to insert a copyright notice in the utils file, to specify you have taken it from the QGIS source code.

share|improve this answer

There is no ready made dialog. The raster formats supported by QGIS depend on the GDAL version used. You can get a list of file formats and extensions supported by the installed version of GDAL like this:

import gdal

    for i in range(0, gdal.GetDriverCount()):
        drv = gdal.GetDriver(i)
        drv_meta = drv.GetMetadata()
        if 'DMD_EXTENSION' in drv_meta:
            print "%s: .%s" % (drv.LongName, drv_meta['DMD_EXTENSION'])

Alternatively you can decide which of the "typical" GDAL formats you will support and hardcode the dialog.

share|improve this answer
Thank you gsherman, I think I've found a simplier way, see my answer. – diegogb Mar 19 '13 at 18:03
Good catch---I'll have to work on memorizing the API :) – gsherman Mar 19 '13 at 18:54

Follow the menus "raster" > "conversion" > "translate", this allows you to save the raster as any desirable format. If you still want to write your own "Save as..." you can look at the source to grab the list of formats.

enter image description here

share|improve this answer
Hi Pablo, my aim wasn't to convert between formats, but to allow the user to choose a file and a format to save the output of an analysis that I'm implementing in a plugin, so I needed to call a "Save as" dialog from my code. – diegogb Mar 19 '13 at 21:48
Ok, anyway I will keep the answer for reference. – Pablo Mar 19 '13 at 21:51
import gdal

for i in range(0, gdal.GetDriverCount()):
    drv = gdal.GetDriver(i)
    drv_meta = drv.GetMetadata()
    if 'DMD_EXTENSION' in drv_meta:
        print "%s: .%s" % (drv.LongName, drv_meta['DMD_EXTENSION'])
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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