3

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)})
Germán Carrillo
37.3k5 gold badges127 silver badges182 bronze badges
asked Oct 4, 2016 at 14:06
2
  • Please make sure you have the correct access rights. Commented Oct 4, 2016 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? Commented Oct 4, 2016 at 15:07

1 Answer 1

3

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)
answered Oct 10, 2016 at 8:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.