2

I'm trying to use gdal_translate tool in a python script I'm writing in QGIS 2.18.3. I use the translate tool because I want to change all the zero values (in all 8 bands of my raster) to nodata. I'm able to do so manually by using the tool from the toolbox. When I run my code in QGIS script editor the processing window disappear after a second, I don't get any error message but no new file is created. I think it is something to do with how this tool understands what is raster, but I've successfully used other gdal tools (merge, raster calculator) in python script using the same method, so I don't understand why it does not work in the translate tool.

This is a simple code I'm trying to use:

##last_try=name
##Select_directory=Folder

import glob, os
from PyQt4.QtCore import QFileInfo
from qgis.core import *

for raster in glob.glob("*.tif"):
    fileInfo = QFileInfo(raster)
    baseName = fileInfo.baseName()
    rlayer = QgsRasterLayer(raster, baseName)
    trans = os.path.join(Select_directory,"image_suffix.tif") 
    outputs_GDALOGRTRANSLATE_1=processing.runalg('gdalogr:translate', rlayer,100.0,True,'0',0,'',None,False,5,4,75.0,6.0,1.0,False,0,False,None,trans)

As I mentioned, this type of code works great with other gdal tools.

What's wrong with my code?

1 Answer 1

1

You haven't told the script where to look for the rasters. The following line

for raster in glob.glob("*.tif"):

searches for files with the .tif extension but you haven't supplied a path. I believe this is why nothing happens. So you should replace the above line with:

for raster in glob.glob(Select_directory + "/*.tif"):

Unfortunately, atleast for me, it results in an error:

Error: Wrong parameter value: None

The error is due to the PROJWIN <ParameterExtent> parameter (this is becoming a consistently annoying issue) whereby it seems you need to specify the extent of each raster layer.

I also noticed the output of the raster name is the same, in which case the output (of which there will only be one) will be overwritten constantly. I've added the raster name to the output but ofcourse you can change this.


So the following code works for me (your script name made me chuckle):

##last_try=name
##Select_directory=Folder

import glob, os
from PyQt4.QtCore import QFileInfo
from qgis.core import *

for raster in glob.glob(Select_directory + "/*.tif"):
    fileInfo = QFileInfo(raster)
    baseName = fileInfo.baseName()
    rlayer = QgsRasterLayer(raster, baseName)
    extent = rlayer.extent()
    xmin = extent.xMinimum()
    xmax = extent.xMaximum()
    ymin = extent.yMinimum()
    ymax = extent.yMaximum()
    trans = os.path.join(Select_directory,baseName + "_image_suffix.tif") 
    outputs_GDALOGRTRANSLATE_1=processing.runalg('gdalogr:translate', rlayer,100.0,True,'0',0,'',"%f,%f,%f,%f"% (xmin, xmax, ymin, ymax),False,5,4,75.0,6.0,1.0,False,0,False,None,trans)
5
  • 1
    I had not noticed about the definition of the trans parameter, so I deleted my answer because yours is correct!
    – mgri
    Feb 16, 2017 at 10:39
  • 1
    Don't worry, I usually overlook these details too! But thanks, we'll see if it is correct for the OP :)
    – Joseph
    Feb 16, 2017 at 10:42
  • hi guys, thank you so much for the help, it works :) Regarding the loop to find tif I should of have pay attention to that because I kind of snipped the code so it would be simple and short, and in the original code I used os.chdir(Select_Folder) before so that the loop is executed in the correct library..
    – user88484
    Feb 16, 2017 at 17:14
  • One question though...I usually export to python small modelers (containing one or two tools) and then change the parameters according to my needs. I also did it this time and the code I posted was how it was exported. I tried to look online for clarification about the parameters in this tool in order to see what I was missing but couldn't find a proper one. So my question is- how was I suppose to know that the first "NONE" needs to have extent parameters? because as I understand, the gdal merge tool for example does not have a parameter for extent. and again, thanks you.
    – user88484
    Feb 16, 2017 at 17:14
  • @user88484 - Most welcome, glad it worked! I normally use the method suggested here which lets you know which parameters are incorrect or required. But I know for parameters which requires a string, None can be used. Which just leaves the extent parameter :)
    – Joseph
    Feb 17, 2017 at 10:02

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.