I have a problem with the grass function r.mfilter outputting an empty file. I'm running the tool from qgis processing standalone.
My system:
OS: Ubuntu 16.04.2 LTS
Saga: 2.2.3
Grass: 7.0.3
QGIS: 2.18.4
Saga installed as per instructions in this question
The script:
from qgis.core import *
from qgis.gui import *
import os
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import Qt
app = QgsApplication(sys.argv, True)
QgsApplication.initQgis();
QgsApplication.setPrefixPath('/usr', True)
sys.path.append('/usr/share/qgis/python/plugins')
from processing.core.Processing import Processing
import processing.tools.general as processing
Processing.initialize()
# Processing directory
cwd = os.getcwd()
procdir = cwd + '/LST-trad/hojd/'
tmpdir = cwd + '/LST-trad/Test2/'
outdir = cwd + '/LST-trad/treecover2/'
filter = cwd + "/LST-trad/qgis5x5filter_1or0_div1.txt"
files = os.listdir(procdir)
# Filter files
filefilt =[]
for f in files:
if f.endswith(".tif"):
filefilt.append(f)
print "help:"
#processing.alghelp("saga:rastercalculator")
processing.alghelp("grass:r.mfilter")
# Coordinates
arglist = []
for file in filefilt:
x1 = int(file[7] + file[11:13]+"000")
x2 = x1 + 25000
y1 = int(file[4:6] + file[9:11] +"000")
y2 = y1 + 25000
coords = str(x1) + "," + str(x2) + "," + str( y1) + "," + str(y2)
outfile = "COV_" + file[4:]
arglist.append([procdir+file, coords, outdir+outfile, outfile])
#print arglist
formula1 =('ifelse(a > 50, 1, 0)')
formula2 = ('a * 4')
for arg in arglist:
tmp1 = tmpdir + "t1_" + arg[3]
tmp2 = tmpdir + "t2_" + arg[3]
print "saga"
processing.runalg("saga:rastercalculator", arg[0], None, formula1, True, 1, tmp1)
print "grass"
processing.runalg("grass:r.mfilter", tmp1, filter,1,False,arg[1], 0, tmp2)
print "rascal"
processing.runalg("saga:rastercalculator", tmp2, None, formula2, True, 1, arg[2])
QgsApplication.exitQgis();
The filter file:
MATRIX 5
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
DIVISOR 1
TYPE P
output form alghelp(r.mfilter):
ALGORITHM: r.mfilter - Performs raster map matrix filter.
input <ParameterRaster>
filter <ParameterFile>
repeat <ParameterNumber>
-z <ParameterBoolean>
GRASS_REGION_PARAMETER <ParameterExtent>
GRASS_REGION_CELLSIZE_PARAMETER <ParameterNumber>
output <OutputRaster>
One thing that confuses me is that the parameters listed here does not match the parameters listed in the grass documentation
I don't understand the
GRASS_REGION_CELLSIZE_PARAMETER <ParameterNumber>parameter. And since it's not in the grass documentation for the tool I don't know where too find an explanation.Re: Note:
– Iamlukesky Mar 27 '17 at 12:04arglistshould really be calledinputfilesor something. Each element is a list with filename, extent etc.GRASS_REGION_PARAMETERshould define the size of the cell for the output raster: if you leave0as value, it will use the size of the input raster, otherwise it will assign a custom value specified by the user. Is there a creation of an empty file or do you get an error without writing anything on the disl? Furthermore, did you try using the algorithm from GRASS7 instead? I mean, the"grass7:r.mfilter" algorithm. – mgri Mar 27 '17 at 12:51processing.runalg("grass7:r.mfilter","/home/johnnie/GiB/Projekt/qgisHeadless/LST-trad/Test2/t1_COV_66_6_0025.tif","/home/johnnie/GiB/Projekt/qgisHeadless/LST-trad/qgis5x5filter_1or0_div1.txt",1,False,"625000.0,650000.0,6600000.0,6625000.0",0,None). So specifying the input as strings seems to be just fine. But when I substitue the line in the script with this line I still get an output of an empty file, no error message. – Iamlukesky Mar 27 '17 at 13:31general()method? See here – mgri Mar 27 '17 at 13:45import processing.tools.general as processing– Iamlukesky Mar 27 '17 at 13:55Noneas output (in combination withrunandload, for example)? – mgri Mar 27 '17 at 13:57