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)})
Answer
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)
No comments:
Post a Comment