|
| 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