Skip to content

Commit 1d15342

Browse files
Médéric RibreuxMédéric RIBREUX
Médéric Ribreux
authored and
Médéric RIBREUX
committed
Add i.cca algorithm (segfaults from GRASS)
1 parent 02bbcbb commit 1d15342

File tree

4 files changed

+91
-18
lines changed

4 files changed

+91
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
i.cca
2+
Canonical components analysis (CCA) program for image processing.
3+
Imagery (i.*)
4+
ParameterMultipleInput|input|Input rasters (2 to 8)|3|False
5+
ParameterFile|signature|File containing spectral signatures|False|False
6+
OutputDirectory|output|Output Directory

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

+26-17
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
***************************************************************************
55
i.py
66
----
7-
Date : February 2016
7+
Date : April 2016
88
Copyright : (C) 2016 by Médéric Ribreux
9-
Email : medspx at medspx dot fr
9+
Email : mederic dot ribreux at medspx dot fr
1010
***************************************************************************
1111
* *
1212
* This program is free software; you can redistribute it and/or modify *
@@ -18,7 +18,7 @@
1818
"""
1919

2020
__author__ = 'Médéric Ribreux'
21-
__date__ = 'March 2016'
21+
__date__ = 'April 2016'
2222
__copyright__ = '(C) 2016, Médéric Ribreux'
2323

2424
# This will get replaced with a git SHA1 when you do a git archive
@@ -94,12 +94,13 @@ def orderedInput(alg, inputParameter, targetParameterDef):
9494
return rootFilename
9595

9696

97-
def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None):
97+
def regroupRasters(alg, field, groupField, subgroupField=None, extFile=None):
9898
"""
9999
Group multiple input rasters into a group
100100
* If there is a subgroupField, a subgroup will automatically created.
101-
* When a sigset file is provided, the file is copied into the sigset
101+
* When an external file is provided, the file is copied into the respective
102102
directory of the subgroup.
103+
* extFile is a dict of the form 'parameterName':'directory name'.
103104
"""
104105
# List of rasters names
105106
rasters = alg.getParameterFromName(field)
@@ -123,24 +124,32 @@ def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None)
123124
)
124125
alg.commands.append(command)
125126

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)
127+
# Handle external files
128+
origExtParams = {}
129+
if subgroupField and extFile:
130+
for ext in extFile.keys():
131+
extFileName = alg.getParameterValue(ext)
132+
if extFileName:
133+
shortFileName = path.basename(extFileName)
134+
destPath = path.join(Grass7Utils.grassMapsetFolder(),
135+
'PERMANENT',
136+
'group', group.value,
137+
'subgroup', subgroup.value,
138+
extFile[ext], shortFileName)
139+
copyFile(alg, extFileName, destPath)
140+
origExtParams[ext] = extFileName
141+
alg.setParameterValue(ext, shortFileName)
138142

139143
# modify parameters values
140144
alg.processCommand()
141145

142146
# Re-add input rasters
143147
alg.addParameter(rasters)
148+
149+
# replace external files value with original value
150+
for param in origExtParams.keys():
151+
alg.setParameterValue(param, origExtParams[param])
152+
144153
# Delete group:
145154
alg.parameters.remove(group)
146155
if subgroupField:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
i_cca.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 multipleOutputDir, verifyRasterNum, regroupRasters
29+
from processing.core.parameters import getParameterFromString
30+
31+
32+
def checkParameterValuesBeforeExecuting(alg):
33+
return verifyRasterNum(alg, 'input', 2, 8)
34+
35+
36+
def processCommand(alg):
37+
# Remove output
38+
output = alg.getOutputFromName('output')
39+
alg.removeOutputFromName('output')
40+
41+
# Create output parameter
42+
param = getParameterFromString("ParameterString|output|output basename|None|False|False")
43+
param.value = alg.getTempFilename()
44+
alg.addParameter(param)
45+
46+
# Regroup rasters
47+
regroupRasters(alg, 'input', 'group', 'subgroup', {'signature': 'sig'})
48+
49+
# re-add output
50+
alg.addOutput(output)
51+
52+
53+
def processOutputs(alg):
54+
param = alg.getParameterFromName('output')
55+
multipleOutputDir(alg, 'output', param.value)
56+
57+
# Delete output parameter
58+
alg.parameters.remove(param)

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030

3131
def processCommand(alg):
3232
# Regroup rasters
33-
regroupRasters(alg, 'input', 'group', 'subgroup', 'signaturefile')
33+
regroupRasters(alg, 'input', 'group', 'subgroup', {'signaturefile': 'sigset'})

0 commit comments

Comments
 (0)