Skip to content

Commit 651302e

Browse files
author
Médéric RIBREUX
committed
Add i.albedo and i.cluster algorithms
1 parent 1b06324 commit 651302e

File tree

5 files changed

+301
-0
lines changed

5 files changed

+301
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
i.albedo
2+
Computes broad band albedo from surface reflectance.
3+
Imagery (i.*)
4+
ParameterMultipleInput|input|Name of input raster maps|3|False
5+
ParameterBoolean|-m|MODIS (7 input bands:1,2,3,4,5,6,7)|False
6+
ParameterBoolean|-n|NOAA AVHRR (2 input bands:1,2)|False
7+
ParameterBoolean|-l|Landsat 5+7 (6 input bands:1,2,3,4,5,7)|False
8+
ParameterBoolean|-a|ASTER (6 input bands:1,3,5,6,8,9)|False
9+
ParameterBoolean|-c|Aggressive mode (Landsat)|False
10+
ParameterBoolean|-d|Soft mode (MODIS)|False
11+
OutputRaster|output|Albedo
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
i.cluster
2+
Generates spectral signatures for land cover types in an image using a clustering algorithm.
3+
Imagery (i.*)
4+
ParameterMultipleInput|input|Input rasters|3|False
5+
ParameterNumber|classes|Initial number of classes (1-255)|1|255|1|True
6+
ParameterFile|seed|Name of file containing initial signatures|False|True
7+
ParameterString|sample|Sampling intervals (by row and col)|None|False|True
8+
ParameterNumber|iterations|Maximum number of iterations|1|None|30|True
9+
ParameterNumber|convergence|Percent convergence|0.0|100.0|98.0|True
10+
ParameterNumber|separation|Cluster separation|0.0|None|0.0|True
11+
ParameterNumber|min_size|Minimum number of pixels in a class|1|None|17|True
12+
OutputFile|signaturefile|Signature File
13+
OutputFile|reportfile|Final Report File
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
i.py
6+
----
7+
Date : February 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 processing.core.parameters import ParameterRaster, getParameterFromString
29+
from processing.tools.system import isWindows
30+
31+
32+
def multipleOutputDir(alg, field, basename=None):
33+
"""
34+
Handle multiple output of rasters into a
35+
directory.
36+
"""
37+
# We need to know where is the output directory
38+
outputDir = alg.getOutputValue(field)
39+
40+
# We need to grab the variable basename
41+
if basename:
42+
commands = ["for r in $(g.list type=rast pattern='{}*'); do".format(basename)]
43+
# Otherwise, export everything
44+
else:
45+
commands = ["for r in $(g.list type=rast); do".format(basename)]
46+
commands.append(" r.out.gdal -c -t input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\"".format(outputDir))
47+
commands.append("done")
48+
alg.commands.extend(commands)
49+
alg.outputCommands.extend(commands)
50+
51+
52+
def orderedInput(alg, inputParameter, targetParameterDef):
53+
"""Inport multiple rasters in the order"""
54+
rasters = alg.getParameterValue(inputParameter).split(';')
55+
# TODO: make targetParameter
56+
inputParameter = getParameterFromString(targetParameterDef)
57+
rootFilename = '{}_'.format(alg.getTempFilename())
58+
inputParameter.value = rootFilename
59+
alg.addParameter(inputParameter)
60+
for idx in range(len(rasters)):
61+
layer = rasters[idx]
62+
if layer in alg.exportedLayers.keys():
63+
continue
64+
else:
65+
destFilename = '{}{}'.format(rootFilename, idx + 1)
66+
alg.setSessionProjectionFromLayer(layer, alg.commands)
67+
alg.exportedLayers[layer] = destFilename
68+
command = 'r.external input={} band=1 output={} --overwrite -o'.format(layer, destFilename)
69+
alg.commands.append(command)
70+
71+
alg.setSessionProjectionFromProject(alg.commands)
72+
73+
region = \
74+
unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
75+
regionCoords = region.split(',')
76+
command = 'g.region'
77+
command += ' -a'
78+
command += ' n=' + unicode(regionCoords[3])
79+
command += ' s=' + unicode(regionCoords[2])
80+
command += ' e=' + unicode(regionCoords[1])
81+
command += ' w=' + unicode(regionCoords[0])
82+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
83+
if cellsize:
84+
command += ' res=' + unicode(cellsize)
85+
else:
86+
command += ' res=' + unicode(alg.getDefaultCellsize())
87+
alignToResolution = \
88+
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
89+
if alignToResolution:
90+
command += ' -a'
91+
alg.commands.append(command)
92+
return rootFilename
93+
94+
95+
def regroupRasters(alg, field, groupField, subgroupField=None):
96+
"""
97+
Group multiple input rasters into a group
98+
"""
99+
# List of rasters names
100+
rasters = alg.getParameterFromName(field)
101+
rastersList = rasters.value.split(';')
102+
alg.parameters.remove(rasters)
103+
104+
# Insert a i.group command
105+
group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField))
106+
group.value = alg.getTempFilename()
107+
alg.addParameter(group)
108+
109+
if subgroupField:
110+
subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField))
111+
subgroup.value = alg.getTempFilename()
112+
alg.addParameter(subgroup)
113+
114+
command = 'i.group group={}{} input={}'.format(
115+
group.value,
116+
' subgroup={}'.format(subgroup.value) if subgroup else '',
117+
','.join([alg.exportedLayers[f] for f in rastersList])
118+
)
119+
alg.commands.append(command)
120+
121+
# modify parameters values
122+
alg.processCommand()
123+
124+
# Re-add input rasters
125+
alg.addParameter(rasters)
126+
# Delete group:
127+
alg.parameters.remove(group)
128+
if subgroupField:
129+
alg.parameters.remove(subgroup)
130+
131+
if subgroupField:
132+
return group.value, subgroup.value
133+
134+
return group.value
135+
136+
137+
def exportInputRasters(alg, rasterDic):
138+
"""
139+
Export input rasters
140+
Use a dict to make input/output link:
141+
{ 'inputName1': 'outputName1', 'inputName2': 'outputName2'}
142+
"""
143+
# Get inputs and outputs
144+
for inputName, outputName in rasterDic.iteritems():
145+
inputRaster = alg.getParameterValue(inputName)
146+
outputRaster = alg.getOutputFromName(outputName)
147+
command = 'r.out.gdal -c -t --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format(
148+
alg.exportedLayers[inputRaster],
149+
outputRaster.value
150+
)
151+
alg.commands.append(command)
152+
alg.outputCommands.append(command)
153+
154+
155+
def verifyRasterNum(alg, rasters, mini, maxi=None):
156+
"""Verify if we have at least n rasters in multipleInput"""
157+
num = len(alg.getParameterValue(rasters).split(';'))
158+
if num < mini:
159+
return 'You need to set at least {} input rasters for this algorithm!'.format(mini)
160+
if maxi and num > maxi:
161+
return 'You need to set a maximum of {} input rasters for this algorithm!'.format(maxi)
162+
return None
163+
164+
165+
def file2Output(alg, output):
166+
"""Transform an OutputFile to a parameter"""
167+
# Get the outputFile
168+
outputFile = alg.getOutputFromName(output)
169+
alg.removeOutputFromName(output)
170+
171+
# Create output parameter
172+
param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output))
173+
param.value = outputFile.value
174+
alg.addParameter(param)
175+
176+
return outputFile
177+
178+
179+
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)
185+
alg.commands.append(command)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
i_albedo.py
6+
-----------
7+
Date : February 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 verifyRasterNum
29+
30+
31+
def checkParameterValuesBeforeExecuting(alg):
32+
if alg.getParameterValue('-m'):
33+
return verifyRasterNum(alg, 'input', 7)
34+
elif alg.getParameterValue('-n'):
35+
return verifyRasterNum(alg, 'input', 2)
36+
elif alg.getParameterValue('-l') or alg.getParameterValue('-a'):
37+
return verifyRasterNum(alg, 'input', 6)
38+
return None
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
i_cluster.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, moveFile
29+
from os import path
30+
from ..Grass7Utils import Grass7Utils
31+
32+
33+
def processCommand(alg):
34+
# We need to extract the basename of the signature file
35+
signatureFile = alg.getOutputFromName('signaturefile')
36+
origSigFile = signatureFile.value
37+
shortSigFile = path.basename(origSigFile)
38+
alg.setOutputValue('signaturefile', shortSigFile)
39+
40+
# Transform output files in string parameters
41+
signatureFile = file2Output(alg, 'signaturefile')
42+
reportFile = file2Output(alg, 'reportfile')
43+
44+
# Regroup rasters
45+
group, subgroup = regroupRasters(alg, 'input', 'group', 'subgroup')
46+
47+
# Re-add signature files
48+
alg.addOutput(signatureFile)
49+
alg.addOutput(reportFile)
50+
51+
# Find Grass directory
52+
interSig = path.join(Grass7Utils.grassMapsetFolder(), 'PERMANENT', 'group', group, 'subgroup', subgroup, 'sig', shortSigFile)
53+
moveFile(alg, interSig, origSigFile)
54+
alg.setOutputValue('signaturefile', origSigFile)

0 commit comments

Comments
 (0)