217 lines
8.1 KiB
Python
Raw Normal View History

2016-04-24 12:38:06 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
i.py
----
Date : April 2016
2016-04-24 12:38:06 +02:00
Copyright : (C) 2016 by Médéric Ribreux
Email : mederic dot ribreux at medspx dot fr
2016-04-24 12:38:06 +02:00
***************************************************************************
* *
* 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__ = 'April 2016'
2016-04-24 12:38:06 +02:00
__copyright__ = '(C) 2016, Médéric Ribreux'
2017-12-29 17:13:30 +01:00
import os
from processing.tools.system import (isWindows, getTempFilename)
2021-06-20 16:33:11 +03:00
from grassprovider.Grass7Utils import Grass7Utils
from qgis.PyQt.QtCore import QDir
2017-12-29 17:13:30 +01:00
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.
2016-04-24 12:38:06 +02:00
"""
2017-12-29 17:13:30 +01:00
rootFilename = 'rast_{}.'.format(os.path.basename(getTempFilename()))
# parameters[tgt] = rootFilename
2017-12-29 17:13:30 +01:00
param = QgsProcessingParameterString(tgt, 'virtual input',
rootFilename, False, False)
alg.addParameter(param)
rasters = alg.parameterAsLayerList(parameters, src, context)
2016-05-04 15:18:27 +02:00
# Handle specific range
if numSeq is None:
2016-09-21 18:24:26 +02:00
numSeq = list(range(1, len(rasters) + 1))
2017-12-29 17:13:30 +01:00
for idx, raster in enumerate(rasters):
rasterName = '{}{}'.format(rootFilename, numSeq[idx])
alg.loadRasterLayer(rasterName, raster, False, None, rasterName)
2016-05-04 15:18:27 +02:00
2017-12-29 17:13:30 +01:00
# Don't forget to remove the old input parameter
alg.removeParameter(src)
2016-04-24 12:38:06 +02:00
2017-12-29 17:13:30 +01:00
def regroupRasters(alg, parameters, context, src, group, subgroup=None, extFile=None):
2016-04-24 12:38:06 +02:00
"""
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
2017-12-29 17:13:30 +01:00
directory of the subgroup.
:param parameters:
2017-12-29 17:13:30 +01:00
: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
2016-04-24 12:38:06 +02:00
"""
2017-12-29 17:13:30 +01:00
# Create a group parameter
groupName = 'group_{}'.format(os.path.basename(getTempFilename()))
param = QgsProcessingParameterString(group, 'virtual group',
groupName, False, False)
alg.addParameter(param)
2017-05-22 14:07:53 +10:00
2017-12-29 17:13:30 +01:00
# 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])
2016-04-24 12:38:06 +02:00
# Insert a i.group command
command = 'i.group group={}{} input={}'.format(
2017-12-29 17:13:30 +01:00
groupName,
' subgroup={}'.format(subgroupName) if subgroup else '',
','.join(rasterNames))
2016-04-24 12:38:06 +02:00
alg.commands.append(command)
# Handle external files
2017-12-29 17:13:30 +01:00
# 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)
2017-12-29 17:13:30 +01:00
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):
2016-04-24 12:38:06 +02:00
"""
Export input rasters
Use a dict to make input/output link:
{ 'inputName1': 'outputName1', 'inputName2': 'outputName2'}
"""
2017-12-29 17:13:30 +01:00
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
2016-04-24 12:38:06 +02:00
# Get inputs and outputs
2017-12-29 17:13:30 +01:00
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)
2016-04-24 12:38:06 +02:00
def verifyRasterNum(alg, parameters, context, rasters, mini, maxi=None):
2017-12-29 17:13:30 +01:00
"""Verify that we have at least n rasters in multipleInput"""
num = len(alg.parameterAsLayerList(parameters, rasters, context))
2016-04-24 12:38:06 +02:00
if num < mini:
return False, 'You need to set at least {} input rasters for this algorithm!'.format(mini)
2016-04-24 12:38:06 +02:00
if maxi and num > maxi:
return False, 'You need to set a maximum of {} input rasters for this algorithm!'.format(maxi)
return True, None
2016-04-24 12:38:06 +02:00
2017-12-29 17:13:30 +01:00
# def file2Output(alg, output):
# """Transform an OutputFile to a parameter"""
# # Get the outputFile
# outputFile = alg.getOutputFromName(output)
# alg.removeOutputFromName(output)
2016-04-24 12:38:06 +02:00
2017-12-29 17:13:30 +01:00
# # Create output parameter
# param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output), 'GrassAlgorithm')
2017-12-29 17:13:30 +01:00
# param.value = outputFile.value
# alg.addParameter(param)
2016-04-24 12:38:06 +02:00
2017-12-29 17:13:30 +01:00
# return outputFile
2016-04-24 12:38:06 +02:00
2016-05-02 14:15:33 +02:00
def createDestDir(alg, toFile):
""" Generates an mkdir command for GRASS7 script """
# Creates the destination directory
command = "{} \"{}\"".format(
2016-05-02 14:15:33 +02:00
"MD" if isWindows() else "mkdir -p",
QDir.toNativeSeparators(os.path.dirname(toFile))
2016-05-02 14:15:33 +02:00
)
alg.commands.append(command)
2016-04-24 12:38:06 +02:00
def moveFile(alg, fromFile, toFile):
2016-05-02 14:15:33 +02:00
""" Generates a move command for GRASS7 script """
createDestDir(alg, toFile)
command = "{} \"{}\" \"{}\"".format(
2016-05-02 14:15:33 +02:00
"MOVE /Y" if isWindows() else "mv -f",
QDir.toNativeSeparators(fromFile),
QDir.toNativeSeparators(toFile)
2016-05-02 14:15:33 +02:00
)
alg.commands.append(command)
def copyFile(alg, fromFile, toFile):
""" Generates a copy command for GRASS7 script """
createDestDir(alg, toFile)
command = "{} \"{}\" \"{}\"".format(
2016-05-02 14:15:33 +02:00
"COPY /Y" if isWindows() else "cp -f",
QDir.toNativeSeparators(fromFile),
QDir.toNativeSeparators(toFile))
2016-04-24 12:38:06 +02:00
alg.commands.append(command)