2

When I import the following processing script in the QGIS Python console it does not give any error and even displays ("Executing algorithm xy"). However the output file is not generated. Does anyone know why? Do I need to save raster output files differently than vector output files in processing scripts?

The two versions of code access the input image differently and the extent values do not make much sense in these snippets.

### gdalogr:translate ###
from PyQt4.QtCore import QFileInfo
import processing

file = iface.activeLayer()
f = file.source()
fileInfo = QFileInfo(f)
baseName = fileInfo.baseName()
rlayer = QgsRasterLayer(f, baseName)
extent = rlayer.extent()
xmin = extent.xMinimum()
xmax = extent.xMaximum()
ymin = extent.yMinimum()
ymax = extent.yMaximum()

output = "C:/result.tif"
processing.runalg('gdalogr:translate',{"INPUT":rlayer,"OUTSIZE":100,"OUTSIZE_PERC":False,"EXPAND":2,"PROJWIN":"%f,%f,%f,%f"%(xmin, xmax, ymin, ymax),"OUTPUT":output})

##########################################################################
### gdalogr:cliprasterbyextent ###
from PyQt4.QtCore import QFileInfo
import processing
import os

os.chdir(r"D:\...\3_Resizing")
files = os.listdir(os.curdir)

for f in files:
    if os.path.splitext(f)[1]=='.tif':
        fileInfo = QFileInfo(f)
        baseName = fileInfo.baseName()
        rlayer = QgsRasterLayer(f, baseName)        
        rextent = rlayer.extent()
        xmin = rextent.xMinimum()
        xmax = rextent.xMaximum()
        ymin = rextent.yMinimum()
        ymax = rextent.yMaximum()

        clip_output= "D:/.../test.tif"
        # I also get no result when providing a name 'test' only, which did work to generate a vector output from qgis:vectorgrid       

        processing.runalg('gdalogr:cliprasterbyextent', {"INPUT":rlayer, "PROJWIN":"%f,%f,%f,%f"% (xmin,xmax,ymin,ymax),"OUTPUT":"%s"%(clip_output)})
  • Please make sure you have the correct access rights. – julsbreakdown Oct 4 '16 at 14:42
  • I don't think this is the problem, because calling processing.runalg('qgis:vectorgrid', {"EXTENT":"%f,%f,%f,%f"% (r_xmin, r_xmax, r_ymin, r_ymax), "STEP_X":x, "STEP_Y":y, "TYPE":1,"OUTPUT":grid_output}) in a similar script in the same directory did save a vector output. However, how do I check the access rights of Qgis processing scripts? – Sophie Crommelinck Oct 4 '16 at 15:07
2

I found the solution: My error was not in wrongly defining the output file, but in defining the input file as:

rlayer = QgsRasterLayer(f, baseName)

This does not correctly load the input raster layer, because f doesn't provide the full path name.

Everything works fine when defining the full pathname of the input file:

filePath = str(os.path.abspath(f))      
rlayer = QgsRasterLayer(filePath, baseName)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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