Skip to content

Commit 7bc5338

Browse files
Médéric RibreuxMédéric RIBREUX
authored andcommitted
Add i.smap algorithm
1 parent 297c632 commit 7bc5338

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
i.smap
2+
Performs contextual image classification using sequential maximum a posteriori (SMAP) estimation.
3+
Imagery (i.*)
4+
ParameterMultipleInput|input|Input rasters|3|False
5+
ParameterFile|signaturefile|Name of input file containing signatures|False|False
6+
ParameterNumber|blocksize|Size of submatrix to process at one time|1|None|1024|True
7+
*ParameterBoolean|-m|Use maximum likelihood estimation (instead of smap)|False
8+
OutputRaster|output|Classification
9+
OutputRaster|goodness|Goodness_of_fit

python/plugins/processing/algs/grass7/ext/i.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727

2828
from processing.core.parameters import ParameterRaster, getParameterFromString
2929
from processing.tools.system import isWindows
30+
from ..Grass7Utils import Grass7Utils
31+
from os import path
3032

3133

3234
def multipleOutputDir(alg, field, basename=None):
@@ -92,9 +94,12 @@ def orderedInput(alg, inputParameter, targetParameterDef):
9294
return rootFilename
9395

9496

95-
def regroupRasters(alg, field, groupField, subgroupField=None):
97+
def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None):
9698
"""
9799
Group multiple input rasters into a group
100+
* If there is a subgroupField, a subgroup will automatically created.
101+
* When a sigset file is provided, the file is copied into the sigset
102+
directory of the subgroup.
98103
"""
99104
# List of rasters names
100105
rasters = alg.getParameterFromName(field)
@@ -118,6 +123,19 @@ def regroupRasters(alg, field, groupField, subgroupField=None):
118123
)
119124
alg.commands.append(command)
120125

126+
# If there is a sigset and a subgroupField, we copy the sigset file
127+
if subgroupField and sigsetField:
128+
sigsetFile = alg.getParameterValue(sigsetField)
129+
shortSigsetFile = path.basename(sigsetFile)
130+
if sigsetFile:
131+
interSigsetFile = path.join(Grass7Utils.grassMapsetFolder(),
132+
'PERMANENT',
133+
'group', group.value,
134+
'subgroup', subgroup.value,
135+
'sigset', shortSigsetFile)
136+
copyFile(alg, sigsetFile, interSigsetFile)
137+
alg.setParameterValue(sigsetField, shortSigsetFile)
138+
121139
# modify parameters values
122140
alg.processCommand()
123141

@@ -127,8 +145,6 @@ def regroupRasters(alg, field, groupField, subgroupField=None):
127145
alg.parameters.remove(group)
128146
if subgroupField:
129147
alg.parameters.remove(subgroup)
130-
131-
if subgroupField:
132148
return group.value, subgroup.value
133149

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

178194

195+
def createDestDir(alg, toFile):
196+
""" Generates an mkdir command for GRASS7 script """
197+
# Creates the destination directory
198+
command = "{} {}".format(
199+
"MD" if isWindows() else "mkdir -p",
200+
path.dirname(toFile)
201+
)
202+
alg.commands.append(command)
203+
204+
179205
def moveFile(alg, fromFile, toFile):
180-
# move the file
181-
if isWindows():
182-
command = "MOVE /Y {} {}".format(fromFile, toFile)
183-
else:
184-
command = "mv -f {} {}".format(fromFile, toFile)
206+
""" Generates a move command for GRASS7 script """
207+
createDestDir(alg, toFile)
208+
command = "{} {} {}".format(
209+
"MOVE /Y" if isWindows() else "mv -f",
210+
fromFile,
211+
toFile
212+
)
213+
alg.commands.append(command)
214+
215+
216+
def copyFile(alg, fromFile, toFile):
217+
""" Generates a copy command for GRASS7 script """
218+
createDestDir(alg, toFile)
219+
command = "{} {} {}".format(
220+
"COPY /Y" if isWindows() else "cp -f",
221+
fromFile,
222+
toFile)
185223
alg.commands.append(command)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
i_smap.py
6+
---------
7+
Date : March 2016
8+
Copyright : (C) 2016 by Médéric Ribreux
9+
Email : medspx at medspx dot fr
10+
***************************************************************************
11+
* *
12+
* This program is free software; you can redistribute it and/or modify *
13+
* it under the terms of the GNU General Public License as published by *
14+
* the Free Software Foundation; either version 2 of the License, or *
15+
* (at your option) any later version. *
16+
* *
17+
***************************************************************************
18+
"""
19+
20+
__author__ = 'Médéric Ribreux'
21+
__date__ = 'March 2016'
22+
__copyright__ = '(C) 2016, Médéric Ribreux'
23+
24+
# This will get replaced with a git SHA1 when you do a git archive
25+
26+
__revision__ = '$Format:%H$'
27+
28+
from i import regroupRasters, file2Output
29+
30+
31+
def processCommand(alg):
32+
# Regroup rasters
33+
regroupRasters(alg, 'input', 'group', 'subgroup', 'signaturefile')

0 commit comments

Comments
 (0)