From 7bc533884bfe39e42f86c1de91bedc5b2185e61f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9d=C3=A9ric=20Ribreux?= Date: Mon, 2 May 2016 14:15:33 +0200 Subject: [PATCH] Add i.smap algorithm --- .../algs/grass7/description/i.smap.txt | 9 +++ .../plugins/processing/algs/grass7/ext/i.py | 56 ++++++++++++++++--- .../processing/algs/grass7/ext/i_smap.py | 33 +++++++++++ 3 files changed, 89 insertions(+), 9 deletions(-) create mode 100644 python/plugins/processing/algs/grass7/description/i.smap.txt create mode 100644 python/plugins/processing/algs/grass7/ext/i_smap.py diff --git a/python/plugins/processing/algs/grass7/description/i.smap.txt b/python/plugins/processing/algs/grass7/description/i.smap.txt new file mode 100644 index 00000000000..67250d02b4b --- /dev/null +++ b/python/plugins/processing/algs/grass7/description/i.smap.txt @@ -0,0 +1,9 @@ +i.smap +Performs contextual image classification using sequential maximum a posteriori (SMAP) estimation. +Imagery (i.*) +ParameterMultipleInput|input|Input rasters|3|False +ParameterFile|signaturefile|Name of input file containing signatures|False|False +ParameterNumber|blocksize|Size of submatrix to process at one time|1|None|1024|True +*ParameterBoolean|-m|Use maximum likelihood estimation (instead of smap)|False +OutputRaster|output|Classification +OutputRaster|goodness|Goodness_of_fit diff --git a/python/plugins/processing/algs/grass7/ext/i.py b/python/plugins/processing/algs/grass7/ext/i.py index a3898ad3579..1f8e1808694 100644 --- a/python/plugins/processing/algs/grass7/ext/i.py +++ b/python/plugins/processing/algs/grass7/ext/i.py @@ -27,6 +27,8 @@ __revision__ = '$Format:%H$' from processing.core.parameters import ParameterRaster, getParameterFromString from processing.tools.system import isWindows +from ..Grass7Utils import Grass7Utils +from os import path def multipleOutputDir(alg, field, basename=None): @@ -92,9 +94,12 @@ def orderedInput(alg, inputParameter, targetParameterDef): return rootFilename -def regroupRasters(alg, field, groupField, subgroupField=None): +def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None): """ Group multiple input rasters into a group + * If there is a subgroupField, a subgroup will automatically created. + * When a sigset file is provided, the file is copied into the sigset + directory of the subgroup. """ # List of rasters names rasters = alg.getParameterFromName(field) @@ -118,6 +123,19 @@ def regroupRasters(alg, field, groupField, subgroupField=None): ) alg.commands.append(command) + # If there is a sigset and a subgroupField, we copy the sigset file + if subgroupField and sigsetField: + sigsetFile = alg.getParameterValue(sigsetField) + shortSigsetFile = path.basename(sigsetFile) + if sigsetFile: + interSigsetFile = path.join(Grass7Utils.grassMapsetFolder(), + 'PERMANENT', + 'group', group.value, + 'subgroup', subgroup.value, + 'sigset', shortSigsetFile) + copyFile(alg, sigsetFile, interSigsetFile) + alg.setParameterValue(sigsetField, shortSigsetFile) + # modify parameters values alg.processCommand() @@ -127,8 +145,6 @@ def regroupRasters(alg, field, groupField, subgroupField=None): alg.parameters.remove(group) if subgroupField: alg.parameters.remove(subgroup) - - if subgroupField: return group.value, subgroup.value return group.value @@ -176,10 +192,32 @@ def file2Output(alg, output): return outputFile -def moveFile(alg, fromFile, toFile): - # move the file - if isWindows(): - command = "MOVE /Y {} {}".format(fromFile, toFile) - else: - command = "mv -f {} {}".format(fromFile, toFile) +def createDestDir(alg, toFile): + """ Generates an mkdir command for GRASS7 script """ + # Creates the destination directory + command = "{} {}".format( + "MD" if isWindows() else "mkdir -p", + path.dirname(toFile) + ) + alg.commands.append(command) + + +def moveFile(alg, fromFile, toFile): + """ Generates a move command for GRASS7 script """ + createDestDir(alg, toFile) + command = "{} {} {}".format( + "MOVE /Y" if isWindows() else "mv -f", + fromFile, + toFile + ) + alg.commands.append(command) + + +def copyFile(alg, fromFile, toFile): + """ Generates a copy command for GRASS7 script """ + createDestDir(alg, toFile) + command = "{} {} {}".format( + "COPY /Y" if isWindows() else "cp -f", + fromFile, + toFile) alg.commands.append(command) diff --git a/python/plugins/processing/algs/grass7/ext/i_smap.py b/python/plugins/processing/algs/grass7/ext/i_smap.py new file mode 100644 index 00000000000..187cb57e2ec --- /dev/null +++ b/python/plugins/processing/algs/grass7/ext/i_smap.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + i_smap.py + --------- + Date : March 2016 + Copyright : (C) 2016 by Médéric Ribreux + Email : medspx at medspx dot fr +*************************************************************************** +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +*************************************************************************** +""" + +__author__ = 'Médéric Ribreux' +__date__ = 'March 2016' +__copyright__ = '(C) 2016, Médéric Ribreux' + +# This will get replaced with a git SHA1 when you do a git archive + +__revision__ = '$Format:%H$' + +from i import regroupRasters, file2Output + + +def processCommand(alg): + # Regroup rasters + regroupRasters(alg, 'input', 'group', 'subgroup', 'signaturefile')