Skip to content
Permalink
Browse files

Add i.cca algorithm (segfaults from GRASS)

  • Loading branch information
Médéric Ribreux Médéric RIBREUX
Médéric Ribreux authored and Médéric RIBREUX committed May 3, 2016
1 parent 02bbcbb commit 1d1534279fdaea829c39187e5d0a0640666400b7
@@ -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
@@ -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 *
@@ -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
@@ -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)
@@ -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:
@@ -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)
@@ -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.
You can’t perform that action at this time.