Skip to content

Commit

Permalink
Add i.smap algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric Ribreux authored and Médéric RIBREUX committed May 29, 2016
1 parent 297c632 commit 7bc5338
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
9 changes: 9 additions & 0 deletions 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
54 changes: 46 additions & 8 deletions python/plugins/processing/algs/grass7/ext/i.py
Expand Up @@ -27,6 +27,8 @@

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):
Expand Down Expand Up @@ -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)
Expand All @@ -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()

Expand All @@ -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
Expand Down Expand Up @@ -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",
path.dirname(toFile)
)
alg.commands.append(command)


def moveFile(alg, fromFile, toFile):
# move the file
if isWindows():
command = "MOVE /Y {} {}".format(fromFile, toFile)
else:
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",
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)
33 changes: 33 additions & 0 deletions 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')

0 comments on commit 7bc5338

Please sign in to comment.