Skip to content
Browse files

Add i.smap algorithm

  • Loading branch information
Médéric Ribreux Médéric RIBREUX
Médéric Ribreux authored and Médéric RIBREUX committed May 29, 2016
1 parent 297c632 commit 7bc533884bfe39e42f86c1de91bedc5b2185e61f
@@ -0,0 +1,9 @@
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
@@ -27,6 +27,8 @@

from processing.core.parameters import ParameterRaster, getParameterFromString
from 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):

# 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(),
'group', group.value,
'subgroup', subgroup.value,
'sigset', shortSigsetFile)
copyFile(alg, sigsetFile, interSigsetFile)
alg.setParameterValue(sigsetField, shortSigsetFile)

# modify parameters values

@@ -127,8 +145,6 @@ def regroupRasters(alg, field, groupField, subgroupField=None):
if subgroupField:

if subgroupField:
return group.value, subgroup.value

return group.value
@@ -176,10 +192,32 @@ def file2Output(alg, output):
return outputFile

def createDestDir(alg, toFile):
""" Generates an mkdir command for GRASS7 script """
# Creates the destination directory
command = "{} {}".format(
"MD" if isWindows() else "mkdir -p",

def moveFile(alg, fromFile, toFile):
# move the file
if isWindows():
command = "MOVE /Y {} {}".format(fromFile, toFile)
command = "mv -f {} {}".format(fromFile, toFile)
""" Generates a move command for GRASS7 script """
createDestDir(alg, toFile)
command = "{} {} {}".format(
"MOVE /Y" if isWindows() else "mv -f",

def copyFile(alg, fromFile, toFile):
""" Generates a copy command for GRASS7 script """
createDestDir(alg, toFile)
command = "{} {} {}".format(
"COPY /Y" if isWindows() else "cp -f",
@@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-

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')

0 comments on commit 7bc5338

Please sign in to comment.
You can’t perform that action at this time.