Skip to content

Commit

Permalink
Add i.cca algorithm (segfaults from GRASS)
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 02bbcbb commit 1d15342
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 18 deletions.
6 changes: 6 additions & 0 deletions python/plugins/processing/algs/grass7/description/i.cca.txt
@@ -0,0 +1,6 @@
i.cca
Canonical components analysis (CCA) program for image processing.
Imagery (i.*)
ParameterMultipleInput|input|Input rasters (2 to 8)|3|False
ParameterFile|signature|File containing spectral signatures|False|False
OutputDirectory|output|Output Directory
43 changes: 26 additions & 17 deletions python/plugins/processing/algs/grass7/ext/i.py
Expand Up @@ -4,9 +4,9 @@
***************************************************************************
i.py
----
Date : February 2016
Date : April 2016
Copyright : (C) 2016 by Médéric Ribreux
Email : medspx at medspx dot fr
Email : mederic dot ribreux at medspx dot fr
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
Expand All @@ -18,7 +18,7 @@
"""

__author__ = 'Médéric Ribreux'
__date__ = 'March 2016'
__date__ = 'April 2016'
__copyright__ = '(C) 2016, Médéric Ribreux'

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


def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None):
def regroupRasters(alg, field, groupField, subgroupField=None, extFile=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
* When an external file is provided, the file is copied into the respective
directory of the subgroup.
* extFile is a dict of the form 'parameterName':'directory name'.
"""
# List of rasters names
rasters = alg.getParameterFromName(field)
Expand All @@ -123,24 +124,32 @@ def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=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)
# Handle external files
origExtParams = {}
if subgroupField and extFile:
for ext in extFile.keys():
extFileName = alg.getParameterValue(ext)
if extFileName:
shortFileName = path.basename(extFileName)
destPath = path.join(Grass7Utils.grassMapsetFolder(),
'PERMANENT',
'group', group.value,
'subgroup', subgroup.value,
extFile[ext], shortFileName)
copyFile(alg, extFileName, destPath)
origExtParams[ext] = extFileName
alg.setParameterValue(ext, shortFileName)

# modify parameters values
alg.processCommand()

# Re-add input rasters
alg.addParameter(rasters)

# replace external files value with original value
for param in origExtParams.keys():
alg.setParameterValue(param, origExtParams[param])

# Delete group:
alg.parameters.remove(group)
if subgroupField:
Expand Down
58 changes: 58 additions & 0 deletions python/plugins/processing/algs/grass7/ext/i_cca.py
@@ -0,0 +1,58 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
i_cca.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 multipleOutputDir, verifyRasterNum, regroupRasters
from processing.core.parameters import getParameterFromString


def checkParameterValuesBeforeExecuting(alg):
return verifyRasterNum(alg, 'input', 2, 8)


def processCommand(alg):
# Remove output
output = alg.getOutputFromName('output')
alg.removeOutputFromName('output')

# Create output parameter
param = getParameterFromString("ParameterString|output|output basename|None|False|False")
param.value = alg.getTempFilename()
alg.addParameter(param)

# Regroup rasters
regroupRasters(alg, 'input', 'group', 'subgroup', {'signature': 'sig'})

# re-add output
alg.addOutput(output)


def processOutputs(alg):
param = alg.getParameterFromName('output')
multipleOutputDir(alg, 'output', param.value)

# Delete output parameter
alg.parameters.remove(param)
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/grass7/ext/i_smap.py
Expand Up @@ -30,4 +30,4 @@

def processCommand(alg):
# Regroup rasters
regroupRasters(alg, 'input', 'group', 'subgroup', 'signaturefile')
regroupRasters(alg, 'input', 'group', 'subgroup', {'signaturefile': 'sigset'})

0 comments on commit 1d15342

Please sign in to comment.