|
@@ -25,180 +25,176 @@ |
|
|
|
|
|
__revision__ = '$Format:%H$' |
|
|
|
|
|
from processing.core.parameters import getParameterFromString |
|
|
from processing.tools.system import isWindows |
|
|
from ..Grass7Utils import Grass7Utils |
|
|
from os import path |
|
|
from copy import deepcopy |
|
|
|
|
|
|
|
|
def multipleOutputDir(alg, field, basename=None): |
|
|
""" |
|
|
Handle multiple output of rasters into a |
|
|
directory. |
|
|
import os |
|
|
from processing.tools.system import (isWindows, getTempFilename) |
|
|
from processing.algs.grass7.Grass7Utils import Grass7Utils |
|
|
from qgis.core import QgsProcessingParameterString |
|
|
from qgis.core import QgsMessageLog |
|
|
|
|
|
|
|
|
def orderedInput(alg, parameters, context, src, tgt, numSeq=None): |
|
|
"""Import multiple rasters in order |
|
|
:param alg: algorithm object. |
|
|
:param parameters: algorithm parameters dict. |
|
|
:param context: algorithm context. |
|
|
:param src: Name of the source parameter. |
|
|
:param tgt: Name of a new input parameter. |
|
|
:param numSeq: List of a sequence for naming layers. |
|
|
""" |
|
|
# We need to know where is the output directory |
|
|
outputDir = alg.getOutputValue(field) |
|
|
|
|
|
# We need to grab the variable basename |
|
|
if basename: |
|
|
commands = ["for r in $(g.list type=rast pattern='{}*'); do".format(basename)] |
|
|
# Otherwise, export everything |
|
|
else: |
|
|
commands = ["for r in $(g.list type=rast); do"] |
|
|
commands.append(" r.out.gdal -c -t -f input=${{r}} output={}/${{r}}.tif createopt=\"TFW=YES,COMPRESS=LZW\"".format(outputDir)) |
|
|
commands.append("done") |
|
|
alg.commands.extend(commands) |
|
|
alg.outputCommands.extend(commands) |
|
|
|
|
|
|
|
|
def orderedInput(alg, inputParameter, targetParameterDef, numSeq=None): |
|
|
"""Inport multiple rasters in the order""" |
|
|
rasters = alg.getParameterValue(inputParameter).split(';') |
|
|
# TODO: make targetParameter |
|
|
inputParameter = getParameterFromString(targetParameterDef) |
|
|
rootFilename = '{}_'.format(alg.getTempFilename()) |
|
|
inputParameter.value = rootFilename |
|
|
alg.addParameter(inputParameter) |
|
|
rootFilename = 'rast_{}.'.format(os.path.basename(getTempFilename())) |
|
|
#parameters[tgt] = rootFilename |
|
|
param = QgsProcessingParameterString(tgt, 'virtual input', |
|
|
rootFilename, False, False) |
|
|
alg.addParameter(param) |
|
|
|
|
|
rasters = alg.parameterAsLayerList(parameters, src, context) |
|
|
# Handle specific range |
|
|
if numSeq is None: |
|
|
numSeq = list(range(1, len(rasters) + 1)) |
|
|
|
|
|
for idx, raster in enumerate(rasters): |
|
|
rasterName = '{}{}'.format(rootFilename, numSeq[idx]) |
|
|
alg.loadRasterLayer(rasterName, raster, False, None, rasterName) |
|
|
|
|
|
for idx in range(len(rasters)): |
|
|
layer = rasters[idx] |
|
|
if layer in list(alg.exportedLayers.keys()): |
|
|
continue |
|
|
else: |
|
|
destFilename = '{}{}'.format(rootFilename, numSeq[idx]) |
|
|
alg.setSessionProjectionFromLayer(layer, alg.commands) |
|
|
alg.exportedLayers[layer] = destFilename |
|
|
command = 'r.external input={} band=1 output={} --overwrite -o'.format(layer, destFilename) |
|
|
alg.commands.append(command) |
|
|
|
|
|
alg.setSessionProjectionFromProject(alg.commands) |
|
|
|
|
|
region = \ |
|
|
str(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER)) |
|
|
regionCoords = region.split(',') |
|
|
command = 'g.region' |
|
|
command += ' -a' |
|
|
command += ' n=' + str(regionCoords[3]) |
|
|
command += ' s=' + str(regionCoords[2]) |
|
|
command += ' e=' + str(regionCoords[1]) |
|
|
command += ' w=' + str(regionCoords[0]) |
|
|
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER) |
|
|
if cellsize: |
|
|
command += ' res=' + str(cellsize) |
|
|
else: |
|
|
command += ' res=' + str(alg.getDefaultCellsize(parameters, context)) |
|
|
alignToResolution = \ |
|
|
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION) |
|
|
if alignToResolution: |
|
|
command += ' -a' |
|
|
alg.commands.append(command) |
|
|
return rootFilename |
|
|
# Don't forget to remove the old input parameter |
|
|
alg.removeParameter(src) |
|
|
|
|
|
|
|
|
|
|
|
def regroupRasters(alg, parameters, field, groupField, subgroupField=None, extFile=None): |
|
|
def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=None): |
|
|
""" |
|
|
Group multiple input rasters into a group |
|
|
* If there is a subgroupField, a subgroup will automatically be created. |
|
|
* When an external file is provided, the file is copied into the respective |
|
|
directory of the subgroup. |
|
|
* extFile is a dict of the form 'parameterName':'directory name'. |
|
|
directory of the subgroup. |
|
|
:param parameters: |
|
|
:param context: |
|
|
:param src: name of input parameter with multiple rasters. |
|
|
:param group: name of group. |
|
|
:param subgroup: name of subgroup. |
|
|
:param extFile: dict : parameterName:directory name |
|
|
""" |
|
|
# List of rasters names |
|
|
|
|
|
new_parameters = deepcopy(parameters) |
|
|
# Create a group parameter |
|
|
groupName = 'group_{}'.format(os.path.basename(getTempFilename())) |
|
|
param = QgsProcessingParameterString(group, 'virtual group', |
|
|
groupName, False, False) |
|
|
alg.addParameter(param) |
|
|
|
|
|
rasters = alg.getParameterFromName(field) |
|
|
rastersList = rasters.value.split(';') |
|
|
del new_parameters[field] |
|
|
# Create a subgroup |
|
|
subgroupName = None |
|
|
if subgroup: |
|
|
subgroupName = 'subgroup_{}'.format(os.path.basename(getTempFilename())) |
|
|
param = QgsProcessingParameterString(subgroup, 'virtual subgroup', |
|
|
subgroupName, False, False) |
|
|
alg.addParameter(param) |
|
|
|
|
|
# Compute raster names |
|
|
rasters = alg.parameterAsLayerList(parameters, src, context) |
|
|
rasterNames = [] |
|
|
for idx, raster in enumerate(rasters): |
|
|
name = '{}_{}'.format(src, idx) |
|
|
if name in alg.exportedLayers: |
|
|
rasterNames.append(alg.exportedLayers[name]) |
|
|
|
|
|
# Insert a i.group command |
|
|
group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField)) |
|
|
new_parameters[group.name()] = alg.getTempFilename() |
|
|
|
|
|
if subgroupField: |
|
|
subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField)) |
|
|
new_parameters[subgroup.name()] = alg.getTempFilename() |
|
|
|
|
|
command = 'i.group group={}{} input={}'.format( |
|
|
new_parameters[group.name()], |
|
|
' subgroup={}'.format(new_parameters[subgroup.name()]) if subgroupField else '', |
|
|
','.join([alg.exportedLayers[f] for f in rastersList]) |
|
|
) |
|
|
groupName, |
|
|
' subgroup={}'.format(subgroupName) if subgroup else '', |
|
|
','.join(rasterNames)) |
|
|
alg.commands.append(command) |
|
|
|
|
|
# Handle external files |
|
|
if subgroupField and extFile: |
|
|
for ext in list(extFile.keys()): |
|
|
extFileName = new_parameters[ext] |
|
|
if extFileName: |
|
|
shortFileName = path.basename(extFileName) |
|
|
destPath = path.join(Grass7Utils.grassMapsetFolder(), |
|
|
'PERMANENT', |
|
|
'group', new_parameters[group.name()], |
|
|
'subgroup', new_parameters[subgroup.name()], |
|
|
extFile[ext], shortFileName) |
|
|
copyFile(alg, extFileName, destPath) |
|
|
new_parameters[ext] = shortFileName |
|
|
|
|
|
# modify parameters values |
|
|
alg.processCommand(new_parameters) |
|
|
|
|
|
return group.value |
|
|
|
|
|
|
|
|
def exportInputRasters(alg, rasterDic): |
|
|
# if subgroupField and extFile: |
|
|
# for ext in extFile.keys(): |
|
|
# extFileName = new_parameters[ext] |
|
|
# if extFileName: |
|
|
# shortFileName = os.path.basename(extFileName) |
|
|
# destPath = os.path.join(Grass7Utils.grassMapsetFolder(), |
|
|
# 'PERMANENT', |
|
|
# 'group', new_parameters[group.name()], |
|
|
# 'subgroup', new_parameters[subgroup.name()], |
|
|
# extFile[ext], shortFileName) |
|
|
# copyFile(alg, extFileName, destPath) |
|
|
|
|
|
|
|
|
alg.removeParameter(src) |
|
|
|
|
|
return groupName, subgroupName |
|
|
|
|
|
|
|
|
def importSigFile(alg, group, subgroup, src, sigDir='sig'): |
|
|
""" |
|
|
Import a signature file into an |
|
|
internal GRASSDB folder |
|
|
""" |
|
|
shortSigFile = os.path.basename(src) |
|
|
interSig = os.path.join(Grass7Utils.grassMapsetFolder(), |
|
|
'PERMANENT', 'group', group, 'subgroup', |
|
|
subgroup, sigDir, shortSigFile) |
|
|
copyFile(alg, src, interSig) |
|
|
return shortSigFile |
|
|
|
|
|
|
|
|
def exportSigFile(alg, group, subgroup, dest, sigDir='sig'): |
|
|
""" |
|
|
Export a signature file from internal GRASSDB |
|
|
to final destination |
|
|
""" |
|
|
shortSigFile = os.path.basename(dest) |
|
|
interSig = os.path.join(Grass7Utils.grassMapsetFolder(), |
|
|
'PERMANENT', 'group', group, 'subgroup', |
|
|
subgroup, sigDir, shortSigFile) |
|
|
moveFile(alg, interSig, dest) |
|
|
return interSig |
|
|
|
|
|
|
|
|
def exportInputRasters(alg, parameters, context, rasterDic): |
|
|
""" |
|
|
Export input rasters |
|
|
Use a dict to make input/output link: |
|
|
{ 'inputName1': 'outputName1', 'inputName2': 'outputName2'} |
|
|
""" |
|
|
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context) |
|
|
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context) |
|
|
|
|
|
# Get inputs and outputs |
|
|
for inputName, outputName in list(rasterDic.items()): |
|
|
inputRaster = alg.getParameterValue(inputName) |
|
|
outputRaster = alg.getOutputFromName(outputName) |
|
|
command = 'r.out.gdal -c -t -f --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format( |
|
|
alg.exportedLayers[inputRaster], |
|
|
outputRaster.value |
|
|
) |
|
|
alg.commands.append(command) |
|
|
alg.outputCommands.append(command) |
|
|
for inputName, outputName in rasterDic.items(): |
|
|
fileName = os.path.normpath( |
|
|
alg.parameterAsOutputLayer(parameters, outputName, context)) |
|
|
grassName = alg.exportedLayers[inputName] |
|
|
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName) |
|
|
alg.exportRasterLayer(grassName, fileName, True, outFormat, createOpt, metaOpt) |
|
|
|
|
|
|
|
|
def verifyRasterNum(alg, parameters, context, rasters, mini, maxi=None): |
|
|
"""Verify if we have at least n rasters in multipleInput""" |
|
|
num = len(alg.parameterAsStrings(rasters).split(';')) |
|
|
"""Verify that we have at least n rasters in multipleInput""" |
|
|
num = len(alg.parameterAsLayerList(parameters, rasters, context)) |
|
|
if num < mini: |
|
|
return 'You need to set at least {} input rasters for this algorithm!'.format(mini) |
|
|
if maxi and num > maxi: |
|
|
return 'You need to set a maximum of {} input rasters for this algorithm!'.format(maxi) |
|
|
return None |
|
|
|
|
|
|
|
|
def file2Output(alg, output): |
|
|
"""Transform an OutputFile to a parameter""" |
|
|
# Get the outputFile |
|
|
outputFile = alg.getOutputFromName(output) |
|
|
alg.removeOutputFromName(output) |
|
|
# def file2Output(alg, output): |
|
|
# """Transform an OutputFile to a parameter""" |
|
|
# # Get the outputFile |
|
|
# outputFile = alg.getOutputFromName(output) |
|
|
# alg.removeOutputFromName(output) |
|
|
|
|
|
# Create output parameter |
|
|
param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) |
|
|
param.value = outputFile.value |
|
|
alg.addParameter(param) |
|
|
# # Create output parameter |
|
|
# param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output)) |
|
|
# param.value = outputFile.value |
|
|
# alg.addParameter(param) |
|
|
|
|
|
return outputFile |
|
|
# return outputFile |
|
|
|
|
|
|
|
|
def createDestDir(alg, toFile): |
|
|
""" Generates an mkdir command for GRASS7 script """ |
|
|
# Creates the destination directory |
|
|
command = "{} {}".format( |
|
|
"MD" if isWindows() else "mkdir -p", |
|
|
path.dirname(toFile) |
|
|
os.path.dirname(toFile) |
|
|
) |
|
|
alg.commands.append(command) |
|
|
|
|
|