Skip to content

Commit 0e47d53

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

File tree

4 files changed

+188
-0
lines changed

4 files changed

+188
-0
lines changed
 
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
r.blend
2+
r.blend.combine - Blends color components of two raster maps by a given ratio and export into a unique raster.
3+
Raster (r.*)
4+
ParameterRaster|first|Name of first raster map for blending|False
5+
ParameterRaster|second|Name of second raster map for blending|False
6+
ParameterNumber|percent|Percentage weight of first map for color blending|0.0|100.0|50.0|True
7+
Hardcoded|-c
8+
OutputRaster|output|Blended
9+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
r.blend
2+
r.blend.rgb - Blends color components of two raster maps by a given ratio and exports into three rasters.
3+
Raster (r.*)
4+
ParameterRaster|first|Name of first raster map for blending|False
5+
ParameterRaster|second|Name of second raster map for blending|False
6+
ParameterNumber|percent|Percentage weight of first map for color blending|0.0|100.0|50.0|True
7+
OutputRaster|output_red|Blended Red
8+
OutputRaster|output_green|Blended Green
9+
OutputRaster|output_blue|Blended Blue
10+
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_blend.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+
# If there is another raster to copy categories from
31+
# we need to import it with r.in.gdal rather than r.external
32+
first = alg.getParameterValue(u'first')
33+
second = alg.getParameterValue(u'second')
34+
if first in alg.exportedLayers.keys() and second in alg.exportedLayers.keys():
35+
return
36+
37+
for raster in [first, second]:
38+
alg.setSessionProjectionFromLayer(raster, alg.commands)
39+
40+
destFilename = alg.getTempFilename()
41+
alg.exportedLayers[raster] = destFilename
42+
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
43+
alg.commands.append(command)
44+
45+
alg.setSessionProjectionFromProject(alg.commands)
46+
47+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
48+
regionCoords = region.split(',')
49+
command = 'g.region'
50+
command += ' -a'
51+
command += ' n=' + unicode(regionCoords[3])
52+
command += ' s=' + unicode(regionCoords[2])
53+
command += ' e=' + unicode(regionCoords[1])
54+
command += ' w=' + unicode(regionCoords[0])
55+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
56+
if cellsize:
57+
command += ' res=' + unicode(cellsize)
58+
else:
59+
command += ' res=' + unicode(alg.getDefaultCellsize())
60+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
61+
if alignToResolution:
62+
command += ' -a'
63+
alg.commands.append(command)
64+
65+
66+
def processOutputs(alg):
67+
# Keep color table
68+
output = alg.getOutputValue(u'output')
69+
command = u"r.out.gdal -t createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
70+
alg.exportedLayers[output],
71+
output
72+
)
73+
alg.commands.append(command)
74+
alg.outputCommands.append(command)
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_blend_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+
from processing.core.outputs import getOutputFromString
29+
30+
31+
def processInputs(alg):
32+
# If there is another raster to copy categories from
33+
# we need to import it with r.in.gdal rather than r.external
34+
first = alg.getParameterValue(u'first')
35+
second = alg.getParameterValue(u'second')
36+
if first in alg.exportedLayers.keys() and second in alg.exportedLayers.keys():
37+
return
38+
39+
for raster in [first, second]:
40+
alg.setSessionProjectionFromLayer(raster, alg.commands)
41+
42+
destFilename = alg.getTempFilename()
43+
alg.exportedLayers[raster] = destFilename
44+
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
45+
alg.commands.append(command)
46+
47+
alg.setSessionProjectionFromProject(alg.commands)
48+
49+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
50+
regionCoords = region.split(',')
51+
command = 'g.region'
52+
command += ' -a'
53+
command += ' n=' + unicode(regionCoords[3])
54+
command += ' s=' + unicode(regionCoords[2])
55+
command += ' e=' + unicode(regionCoords[1])
56+
command += ' w=' + unicode(regionCoords[0])
57+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
58+
if cellsize:
59+
command += ' res=' + unicode(cellsize)
60+
else:
61+
command += ' res=' + unicode(alg.getDefaultCellsize())
62+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
63+
if alignToResolution:
64+
command += ' -a'
65+
alg.commands.append(command)
66+
67+
68+
def processCommand(alg):
69+
# We need to remove all outputs
70+
basename = getOutputFromString('OutputRaster|output|Output basename')
71+
basename.value = 'output'
72+
alg.addOutput(basename)
73+
outputNames = ['output_{}'.format(f) for f in ['red', 'green', 'blue']]
74+
outputs = [alg.getOutputFromName(f) for f in outputNames]
75+
for output in outputNames:
76+
alg.exportedLayers[alg.getOutputValue(output)] = 'output' + alg.uniqueSufix
77+
alg.removeOutputFromName(output)
78+
79+
alg.processCommand()
80+
81+
# And to re-add them
82+
alg.removeOutputFromName('output')
83+
for output in outputs:
84+
alg.addOutput(output)
85+
86+
87+
def processOutputs(alg):
88+
# Export each color raster
89+
colors = ['red', 'green', 'blue']
90+
for color in colors:
91+
output = alg.getOutputValue('output_{}'.format(color))
92+
command = "r.out.gdal input={} output=\"{}\" createopt=\"TFW=YES,COMPRESS=LZW\"".format(
93+
alg.exportedLayers[output] + '.' + color[0], output)
94+
alg.commands.append(command)
95+
alg.outputCommands.append(command)

0 commit comments

Comments
 (0)
Please sign in to comment.