Skip to content

Commit 7802436

Browse files
author
Médéric RIBREUX
committed
Add r.colors algorithm
1 parent e12609e commit 7802436

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
r.colors
2+
Creates/modifies the color table associated with a raster map.
3+
Raster (r.*)
4+
ParameterMultipleInput|map|Name of raster maps(s)|3|False
5+
ParameterSelection|color|Name of color table|not selected;aspect;aspectcolr;bcyr;bgyr;blues;byg;byr;celsius;corine;curvature;differences;elevation;etopo2;evi;fahrenheit;gdd;greens;grey;grey.eq;grey.log;grey1.0;grey255;gyr;haxby;kelvin;ndvi;ndwi;oranges;population;population_dens;precipitation;precipitation_daily;precipitation_monthly;rainbow;ramp;random;reds;rstcurv;ryb;ryg;sepia;slope;srtm;srtm_plus;terrain;wave|0|False|True
6+
ParameterString|rules_txt|Color rules|None|True|True
7+
ParameterFile|rules|Color rules file|True
8+
ParameterRaster|raster|Raster map from which to copy color table|True
9+
ParameterBoolean|-r|Remove existing color table|False
10+
ParameterBoolean|-w|Only write new color table if it does not already exist|False
11+
ParameterBoolean|-n|Invert colors|False
12+
ParameterBoolean|-g|Logarithmic scaling|False
13+
ParameterBoolean|-a|Logarithmic-absolute scaling|False
14+
ParameterBoolean|-e|Histogram equalization|False
15+
OutputDirectory|output_dir|Output Directory
16+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# -*- coding: utf-8 -*-
2+
3+
"""
4+
***************************************************************************
5+
r_colors.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+
import os
29+
30+
31+
def checkParameterValuesBeforeExecuting(alg):
32+
""" Verify if we have the right parameters """
33+
if alg.getParameterValue(u'rules_txt') and alg.getParameterValue(u'rules'):
34+
return alg.tr("You need to set either inline rules or a rules file !")
35+
36+
return None
37+
38+
39+
def processInputs(alg):
40+
# import all rasters with their color tables (and their bands)
41+
# We need to import all the bands and color tables of the input rasters
42+
rasters = alg.getParameterValue('map').split(',')
43+
for raster in rasters:
44+
if raster in alg.exportedLayers.keys():
45+
continue
46+
47+
alg.setSessionProjectionFromLayer(raster, alg.commands)
48+
destFilename = alg.getTempFilename()
49+
alg.exportedLayers[raster] = destFilename
50+
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
51+
alg.commands.append(command)
52+
53+
alg.setSessionProjectionFromProject(alg.commands)
54+
55+
region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
56+
regionCoords = region.split(',')
57+
command = 'g.region'
58+
command += ' -a'
59+
command += ' n=' + unicode(regionCoords[3])
60+
command += ' s=' + unicode(regionCoords[2])
61+
command += ' e=' + unicode(regionCoords[1])
62+
command += ' w=' + unicode(regionCoords[0])
63+
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
64+
if cellsize:
65+
command += ' res=' + unicode(cellsize)
66+
else:
67+
command += ' res=' + unicode(alg.getDefaultCellsize())
68+
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
69+
if alignToResolution:
70+
command += ' -a'
71+
alg.commands.append(command)
72+
73+
74+
def processCommand(alg):
75+
# remove output before processCommand
76+
output = alg.getOutputFromName('output_dir')
77+
alg.removeOutputFromName('output_dir')
78+
color = alg.getParameterFromName('color')
79+
if color.value == 0:
80+
alg.parameters.remove(color)
81+
82+
# TODO Handle rules
83+
alg.processCommand()
84+
85+
# re-add the previous output
86+
alg.addOutput(output)
87+
88+
89+
def processOutputs(alg):
90+
# Export all rasters with their color tables (and their bands)
91+
rasters = [alg.exportedLayers[f] for f in alg.getParameterValue('map').split(',')]
92+
output_dir = alg.getOutputValue('output_dir')
93+
for raster in rasters:
94+
command = u"r.out.gdal createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
95+
raster,
96+
os.path.join(output_dir, raster + '.tif')
97+
)
98+
alg.commands.append(command)
99+
alg.outputCommands.append(command)

0 commit comments

Comments
 (0)