|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +""" |
| 4 | +*************************************************************************** |
| 5 | + i.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__ = 'March 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.parameters import ParameterRaster, getParameterFromString |
| 29 | +from processing.tools.system import isWindows |
| 30 | + |
| 31 | + |
| 32 | +def multipleOutputDir(alg, field, basename=None): |
| 33 | + """ |
| 34 | + Handle multiple output of rasters into a |
| 35 | + directory. |
| 36 | + """ |
| 37 | + # We need to know where is the output directory |
| 38 | + outputDir = alg.getOutputValue(field) |
| 39 | + |
| 40 | + # We need to grab the variable basename |
| 41 | + if basename: |
| 42 | + commands = ["for r in $(g.list type=rast pattern='{}*'); do".format(basename)] |
| 43 | + # Otherwise, export everything |
| 44 | + else: |
| 45 | + commands = ["for r in $(g.list type=rast); do".format(basename)] |
| 46 | + commands.append(" r.out.gdal -c -t input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\"".format(outputDir)) |
| 47 | + commands.append("done") |
| 48 | + alg.commands.extend(commands) |
| 49 | + alg.outputCommands.extend(commands) |
| 50 | + |
| 51 | + |
| 52 | +def orderedInput(alg, inputParameter, targetParameterDef): |
| 53 | + """Inport multiple rasters in the order""" |
| 54 | + rasters = alg.getParameterValue(inputParameter).split(';') |
| 55 | + # TODO: make targetParameter |
| 56 | + inputParameter = getParameterFromString(targetParameterDef) |
| 57 | + rootFilename = '{}_'.format(alg.getTempFilename()) |
| 58 | + inputParameter.value = rootFilename |
| 59 | + alg.addParameter(inputParameter) |
| 60 | + for idx in range(len(rasters)): |
| 61 | + layer = rasters[idx] |
| 62 | + if layer in alg.exportedLayers.keys(): |
| 63 | + continue |
| 64 | + else: |
| 65 | + destFilename = '{}{}'.format(rootFilename, idx + 1) |
| 66 | + alg.setSessionProjectionFromLayer(layer, alg.commands) |
| 67 | + alg.exportedLayers[layer] = destFilename |
| 68 | + command = 'r.external input={} band=1 output={} --overwrite -o'.format(layer, destFilename) |
| 69 | + alg.commands.append(command) |
| 70 | + |
| 71 | + alg.setSessionProjectionFromProject(alg.commands) |
| 72 | + |
| 73 | + region = \ |
| 74 | + unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER)) |
| 75 | + regionCoords = region.split(',') |
| 76 | + command = 'g.region' |
| 77 | + command += ' -a' |
| 78 | + command += ' n=' + unicode(regionCoords[3]) |
| 79 | + command += ' s=' + unicode(regionCoords[2]) |
| 80 | + command += ' e=' + unicode(regionCoords[1]) |
| 81 | + command += ' w=' + unicode(regionCoords[0]) |
| 82 | + cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER) |
| 83 | + if cellsize: |
| 84 | + command += ' res=' + unicode(cellsize) |
| 85 | + else: |
| 86 | + command += ' res=' + unicode(alg.getDefaultCellsize()) |
| 87 | + alignToResolution = \ |
| 88 | + alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION) |
| 89 | + if alignToResolution: |
| 90 | + command += ' -a' |
| 91 | + alg.commands.append(command) |
| 92 | + return rootFilename |
| 93 | + |
| 94 | + |
| 95 | +def regroupRasters(alg, field, groupField, subgroupField=None): |
| 96 | + """ |
| 97 | + Group multiple input rasters into a group |
| 98 | + """ |
| 99 | + # List of rasters names |
| 100 | + rasters = alg.getParameterFromName(field) |
| 101 | + rastersList = rasters.value.split(';') |
| 102 | + alg.parameters.remove(rasters) |
| 103 | + |
| 104 | + # Insert a i.group command |
| 105 | + group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField)) |
| 106 | + group.value = alg.getTempFilename() |
| 107 | + alg.addParameter(group) |
| 108 | + |
| 109 | + if subgroupField: |
| 110 | + subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField)) |
| 111 | + subgroup.value = alg.getTempFilename() |
| 112 | + alg.addParameter(subgroup) |
| 113 | + |
| 114 | + command = 'i.group group={}{} input={}'.format( |
| 115 | + group.value, |
| 116 | + ' subgroup={}'.format(subgroup.value) if subgroup else '', |
| 117 | + ','.join([alg.exportedLayers[f] for f in rastersList]) |
| 118 | + ) |
| 119 | + alg.commands.append(command) |
| 120 | + |
| 121 | + # modify parameters values |
| 122 | + alg.processCommand() |
| 123 | + |
| 124 | + # Re-add input rasters |
| 125 | + alg.addParameter(rasters) |
| 126 | + # Delete group: |
| 127 | + alg.parameters.remove(group) |
| 128 | + if subgroupField: |
| 129 | + alg.parameters.remove(subgroup) |
| 130 | + |
| 131 | + if subgroupField: |
| 132 | + return group.value, subgroup.value |
| 133 | + |
| 134 | + return group.value |
| 135 | + |
| 136 | + |
| 137 | +def exportInputRasters(alg, rasterDic): |
| 138 | + """ |
| 139 | + Export input rasters |
| 140 | + Use a dict to make input/output link: |
| 141 | + { 'inputName1': 'outputName1', 'inputName2': 'outputName2'} |
| 142 | + """ |
| 143 | + # Get inputs and outputs |
| 144 | + for inputName, outputName in rasterDic.iteritems(): |
| 145 | + inputRaster = alg.getParameterValue(inputName) |
| 146 | + outputRaster = alg.getOutputFromName(outputName) |
| 147 | + command = 'r.out.gdal -c -t --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format( |
| 148 | + alg.exportedLayers[inputRaster], |
| 149 | + outputRaster.value |
| 150 | + ) |
| 151 | + alg.commands.append(command) |
| 152 | + alg.outputCommands.append(command) |
| 153 | + |
| 154 | + |
| 155 | +def verifyRasterNum(alg, rasters, mini, maxi=None): |
| 156 | + """Verify if we have at least n rasters in multipleInput""" |
| 157 | + num = len(alg.getParameterValue(rasters).split(';')) |
| 158 | + if num < mini: |
| 159 | + return 'You need to set at least {} input rasters for this algorithm!'.format(mini) |
| 160 | + if maxi and num > maxi: |
| 161 | + return 'You need to set a maximum of {} input rasters for this algorithm!'.format(maxi) |
| 162 | + return None |
| 163 | + |
| 164 | + |
| 165 | +def file2Output(alg, output): |
| 166 | + """Transform an OutputFile to a parameter""" |
| 167 | + # Get the outputFile |
| 168 | + outputFile = alg.getOutputFromName(output) |
| 169 | + alg.removeOutputFromName(output) |
| 170 | + |
| 171 | + # Create output parameter |
| 172 | + param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) |
| 173 | + param.value = outputFile.value |
| 174 | + alg.addParameter(param) |
| 175 | + |
| 176 | + return outputFile |
| 177 | + |
| 178 | + |
| 179 | +def moveFile(alg, fromFile, toFile): |
| 180 | + # move the file |
| 181 | + if isWindows(): |
| 182 | + command = "MOVE /Y {} {}".format(fromFile, toFile) |
| 183 | + else: |
| 184 | + command = "mv -f {} {}".format(fromFile, toFile) |
| 185 | + alg.commands.append(command) |
0 commit comments