|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + r_category.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 codecs |
| 29 | +from processing.tools.system import getTempFilename |
| 30 | + |
| 31 | +def checkParameterValuesBeforeExecuting(alg): |
| 32 | + """ Verify if we have the right parameters """ |
| 33 | + rules = alg.getParameterValue(u'rules') |
| 34 | + txtrules = alg.getParameterValue(u'txtrules') |
| 35 | + raster = alg.getParameterValue(u'raster') |
| 36 | + |
| 37 | + if rules and txtrules: |
| 38 | + return alg.tr("You need to set either a rules file or write directly the rules !") |
| 39 | + elif (rules and raster) or (txtrules and raster): |
| 40 | + return alg.tr("You need to set either rules or a raster from which to copy categories !") |
| 41 | + |
| 42 | + return None |
| 43 | + |
| 44 | +def processInputs(alg): |
| 45 | + # If there is another raster to copy categories from |
| 46 | + # we need to import it with r.in.gdal rather than r.external |
| 47 | + inputRaster = alg.getParameterValue(u'map') |
| 48 | + copyRaster = alg.getParameterValue(u'raster') |
| 49 | + if copyRaster: |
| 50 | + if copyRaster in alg.exportedLayers.keys(): |
| 51 | + return |
| 52 | + |
| 53 | + for raster,method in (inputRaster, 'r.external'),(copyRaster, 'r.in.gdal'): |
| 54 | + alg.setSessionProjectionFromLayer(raster, alg.commands) |
| 55 | + |
| 56 | + destFilename = alg.getTempFilename() |
| 57 | + alg.exportedLayers[raster] = destFilename |
| 58 | + command = '{} input={} output={} band=1 --overwrite -o'.format(method, raster, destFilename) |
| 59 | + alg.commands.append(command) |
| 60 | + |
| 61 | + alg.setSessionProjectionFromProject(alg.commands) |
| 62 | + |
| 63 | + region = unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER)) |
| 64 | + regionCoords = region.split(',') |
| 65 | + command = 'g.region' |
| 66 | + command += ' -a' |
| 67 | + command += ' n=' + unicode(regionCoords[3]) |
| 68 | + command += ' s=' + unicode(regionCoords[2]) |
| 69 | + command += ' e=' + unicode(regionCoords[1]) |
| 70 | + command += ' w=' + unicode(regionCoords[0]) |
| 71 | + cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER) |
| 72 | + if cellsize: |
| 73 | + command += ' res=' + unicode(cellsize) |
| 74 | + else: |
| 75 | + command += ' res=' + unicode(alg.getDefaultCellsize()) |
| 76 | + alignToResolution = alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION) |
| 77 | + if alignToResolution: |
| 78 | + command += ' -a' |
| 79 | + alg.commands.append(command) |
| 80 | + else: |
| 81 | + alg.processInputs() |
| 82 | + |
| 83 | + |
| 84 | +def processCommand(alg): |
| 85 | + # We temporary remove the output |
| 86 | + out = alg.getOutputFromName('output') |
| 87 | + mapParam = alg.getParameterValue('map') |
| 88 | + alg.exportedLayers[out.value] = alg.exportedLayers[mapParam] |
| 89 | + alg.removeOutputFromName('output') |
| 90 | + txtRulesParam = alg.getParameterFromName(u'txtrules') |
| 91 | + rules = alg.getParameterFromName(u'rules') |
| 92 | + |
| 93 | + # Handle inline rules |
| 94 | + if txtRulesParam.value: |
| 95 | + # Creates a temporary txt file |
| 96 | + tempRulesName = getTempFilename('txt') |
| 97 | + |
| 98 | + # Inject rules into temporary txt file |
| 99 | + with codecs.open(tempRulesName, 'w', 'utf-8') as tempRules: |
| 100 | + tempRules.write(txtRulesParam.value) |
| 101 | + |
| 102 | + # Replace rules with temporary file |
| 103 | + rules.value = tempRulesName |
| 104 | + alg.parameters.remove(txtRulesParam) |
| 105 | + |
| 106 | + alg.processCommand() |
| 107 | + |
| 108 | + # We re-add the new output |
| 109 | + alg.addOutput(out) |
| 110 | + |
| 111 | + |
| 112 | +def processOutputs(alg): |
| 113 | + # Output results ('from' table and output table) |
| 114 | + out = alg.getOutputValue('output') |
| 115 | + command = u"r.out.gdal --overwrite -t -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\"".format( |
| 116 | + alg.exportedLayers[out], out) |
| 117 | + alg.commands.append(command) |
| 118 | + alg.outputCommands.append(command) |
0 commit comments