Skip to content

Commit e12609e

Browse files
author
Médéric RIBREUX
committedFeb 28, 2016
Add r.rgb algorithm
1 parent 0e47d53 commit e12609e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
 
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
r.rgb
2+
Splits a raster map into red, green and blue maps.
3+
Raster (r.*)
4+
ParameterRaster|input|Name of input raster map|False
5+
OutputRaster|red|Red
6+
OutputRaster|green|Green
7+
OutputRaster|blue|Blue
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_rgb.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__ = 'February 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+
29+
def processInputs(alg):
30+
# We need to import all the bands and color tables of the input raster
31+
raster = alg.getParameterValue('input')
32+
if raster in alg.exportedLayers.keys():
33+
return
34+
35+
alg.setSessionProjectionFromLayer(raster, alg.commands)
36+
destFilename = alg.getTempFilename()
37+
alg.exportedLayers[raster] = destFilename
38+
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
39+
alg.commands.append(command)
40+
41+
alg.setSessionProjectionFromProject(alg.commands)
42+
43+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
44+
regionCoords = region.split(',')
45+
command = 'g.region'
46+
command += ' -a'
47+
command += ' n=' + unicode(regionCoords[3])
48+
command += ' s=' + unicode(regionCoords[2])
49+
command += ' e=' + unicode(regionCoords[1])
50+
command += ' w=' + unicode(regionCoords[0])
51+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
52+
if cellsize:
53+
command += ' res=' + unicode(cellsize)
54+
else:
55+
command += ' res=' + unicode(alg.getDefaultCellsize())
56+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
57+
if alignToResolution:
58+
command += ' -a'
59+
alg.commands.append(command)
60+
61+
62+
def processCommand(alg):
63+
# We need to introduce something clever:
64+
# if the input raster is multiband: export each component directly
65+
raster = alg.exportedLayers[alg.getParameterValue('input')]
66+
for color in ['red', 'green', 'blue']:
67+
alg.exportedLayers[alg.getOutputValue(color)] = color + alg.uniqueSufix
68+
69+
commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)]
70+
commands.append(" r.rgb input={} red={} green={} blue={} --overwrite".format(
71+
raster,
72+
alg.exportedLayers[alg.getOutputValue('red')],
73+
alg.exportedLayers[alg.getOutputValue('green')],
74+
alg.exportedLayers[alg.getOutputValue('blue')]
75+
))
76+
commands.append("fi")
77+
alg.commands.extend(commands)
78+
79+
80+
def processOutputs(alg):
81+
raster = alg.exportedLayers[alg.getParameterValue('input')]
82+
commands = ["if [ $(g.list type=rast pattern='{}.*' | wc -l) -eq \"0\" ]; then".format(raster)]
83+
for color in ['red', 'green', 'blue']:
84+
commands.append(" r.out.gdal -t input={} output={} createopt=\"TFW=YES,COMPRESS=LZW\" --overwrite".format(
85+
alg.exportedLayers[alg.getOutputValue(color)],
86+
alg.getOutputValue(color)
87+
))
88+
commands.append("else")
89+
for color in ['red', 'green', 'blue']:
90+
commands.append(" r.out.gdal -t input={} output={} createopt=\"TFW=YES,COMPRESS=LZW\" --overwrite".format(
91+
'{}.{}'.format(raster, color),
92+
alg.getOutputValue(color)
93+
))
94+
commands.append("fi")
95+
alg.commands.extend(commands)

0 commit comments

Comments
 (0)
Please sign in to comment.