Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add r.colors algorithm
  • Loading branch information
Médéric RIBREUX committed Feb 28, 2016
1 parent e12609e commit 7802436
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
16 changes: 16 additions & 0 deletions python/plugins/processing/algs/grass7/description/r.colors.txt
@@ -0,0 +1,16 @@
r.colors
Creates/modifies the color table associated with a raster map.
Raster (r.*)
ParameterMultipleInput|map|Name of raster maps(s)|3|False
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
ParameterString|rules_txt|Color rules|None|True|True
ParameterFile|rules|Color rules file|True
ParameterRaster|raster|Raster map from which to copy color table|True
ParameterBoolean|-r|Remove existing color table|False
ParameterBoolean|-w|Only write new color table if it does not already exist|False
ParameterBoolean|-n|Invert colors|False
ParameterBoolean|-g|Logarithmic scaling|False
ParameterBoolean|-a|Logarithmic-absolute scaling|False
ParameterBoolean|-e|Histogram equalization|False
OutputDirectory|output_dir|Output Directory

99 changes: 99 additions & 0 deletions python/plugins/processing/algs/grass7/ext/r_colors.py
@@ -0,0 +1,99 @@
# -*- coding: utf-8 -*-

"""
***************************************************************************
r_colors.py
-----------
Date : February 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__ = 'February 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$'

import os


def checkParameterValuesBeforeExecuting(alg):
""" Verify if we have the right parameters """
if alg.getParameterValue(u'rules_txt') and alg.getParameterValue(u'rules'):
return alg.tr("You need to set either inline rules or a rules file !")

return None


def processInputs(alg):
# import all rasters with their color tables (and their bands)
# We need to import all the bands and color tables of the input rasters
rasters = alg.getParameterValue('map').split(',')
for raster in rasters:
if raster in alg.exportedLayers.keys():
continue

alg.setSessionProjectionFromLayer(raster, alg.commands)
destFilename = alg.getTempFilename()
alg.exportedLayers[raster] = destFilename
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, destFilename)
alg.commands.append(command)

alg.setSessionProjectionFromProject(alg.commands)

region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(',')
command = 'g.region'
command += ' -a'
command += ' n=' + unicode(regionCoords[3])
command += ' s=' + unicode(regionCoords[2])
command += ' e=' + unicode(regionCoords[1])
command += ' w=' + unicode(regionCoords[0])
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
if cellsize:
command += ' res=' + unicode(cellsize)
else:
command += ' res=' + unicode(alg.getDefaultCellsize())
alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
alg.commands.append(command)


def processCommand(alg):
# remove output before processCommand
output = alg.getOutputFromName('output_dir')
alg.removeOutputFromName('output_dir')
color = alg.getParameterFromName('color')
if color.value == 0:
alg.parameters.remove(color)

# TODO Handle rules
alg.processCommand()

# re-add the previous output
alg.addOutput(output)


def processOutputs(alg):
# Export all rasters with their color tables (and their bands)
rasters = [alg.exportedLayers[f] for f in alg.getParameterValue('map').split(',')]
output_dir = alg.getOutputValue('output_dir')
for raster in rasters:
command = u"r.out.gdal createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" --overwrite".format(
raster,
os.path.join(output_dir, raster + '.tif')
)
alg.commands.append(command)
alg.outputCommands.append(command)

0 comments on commit 7802436

Please sign in to comment.