Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

Thanks!

share|improve this question
add comment

4 Answers

up vote 4 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()
QgsRasterLayer.buildSupportedRasterFileFilter(a)
# "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/GdalTools_utils.py 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
        else:
            fileDialogFunc = Utils.FileDialog.getOpenFileName
        outputFile = fileDialogFunc(self, self.tr( "Select the raster file to save the results to" ), Utils.FileFilter.allRastersFilter(), lastUsedFilter)
        if outputFile.isEmpty():
            return
        Utils.FileFilter.setLastUsedRasterFilter(lastUsedFilter)
    
        # required either -ts or -tr to create the output file 
        if gdalVersion >= "1.8.0":
            if not QtCore.QFileInfo(outputFile).exists():
                QMessageBox.information( self, self.tr( "Output size required" ), self.tr( "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 (doRasterize.py). 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
add comment

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

    gdal.AllRegister()
    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
add comment

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
add comment
import gdal

gdal.AllRegister()
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
add comment

Your Answer

 
discard

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.