Skip to content
Permalink
Browse files

Fix all i.* ext scripts

  • Loading branch information
Médéric RIBREUX
Médéric RIBREUX committed Dec 29, 2017
1 parent e712ee6 commit 486bcd6f240fd0d61973e2df2d775d3c0bfc12d4
@@ -646,26 +646,28 @@ def loadRasterLayerFromParameter(self, name, parameters, context, external=True,
layer = self.parameterAsRasterLayer(parameters, name, context)
self.loadRasterLayer(name, layer, external, band)

def loadRasterLayer(self, name, layer, external=True, band=1):
def loadRasterLayer(self, name, layer, external=True, band=1, destName=None):
"""
Creates a dedicated command to load a raster into
the temporary GRASS DB.
:param name: name of the parameter.
:param layer: QgsMapLayer for the raster layer.
:param external: True if using r.external.
:param band: imports only specified band. None for all bands.
:param destName: force the destination name of the raster.
"""
self.inputLayers.append(layer)
self.setSessionProjectionFromLayer(layer)
destFilename = 'a' + os.path.basename(getTempFilename())
self.exportedLayers[name] = destFilename
if not destName:
destName = 'rast_{}'.format(os.path.basename(getTempFilename()))
self.exportedLayers[name] = destName
command = '{0} input="{1}" {2}output="{3}" --overwrite -o'.format(
'r.external' if external else 'r.in.gdal',
os.path.normpath(layer.source()),
'band={} '.format(band) if band else '',
destFilename)
destName)
self.commands.append(command)

def exportRasterLayerFromParameter(self, name, parameters, context, colorTable=True):
"""
Creates a dedicated command to export a raster from
@@ -714,19 +716,22 @@ def exportRasterLayer(self, grassName, fileName,
)
)

def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True):
def exportRasterLayersIntoDirectory(self, name, parameters, context, colorTable=True, wholeDB=False):
"""
Creates a dedicated loop command to export rasters from
temporary GRASS DB into a directory via gdal.
:param name: name of the output directory parameter.
:param parameters: Algorithm parameters dict.
:param context: Algorithm context.
:param colorTable: preserve color Table.
:param wholeDB: export every raster layer from the GRASSDB
"""
# Grab directory name and temporary basename
outDir = os.path.normpath(
self.parameterAsString(parameters, name, context))
basename = name + self.uniqueSuffix
basename = ''
if not wholeDB:
basename = name + self.uniqueSuffix

# Add a loop export from the basename
for cmd in [self.commands, self.outputCommands]:
@@ -1,6 +1,6 @@
i.in.spotvgt
Imports SPOT VGT NDVI data into a raster map.
Imagery (i.*)
QgsProcessingParameterRasterLayer|input|Name of input SPOT VGT NDVI HDF file|None|False
QgsProcessingParameterFile|input|Name of input SPOT VGT NDVI HDF file|False|hdf|None|False
*QgsProcessingParameterBoolean|-a|Also import quality map (SM status map layer) and filter NDVI map|False
QgsProcessingParameterRasterDestination|output|SPOT NDVI Raster
@@ -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)

@@ -28,11 +28,12 @@
from .i import verifyRasterNum


def checkParameterValuesBeforeExecuting(alg):
if alg.getParameterValue('-m'):
return verifyRasterNum(alg, 'input', 7)
elif alg.getParameterValue('-n'):
return verifyRasterNum(alg, 'input', 2)
elif alg.getParameterValue('-l') or alg.getParameterValue('-a'):
return verifyRasterNum(alg, 'input', 6)
def checkParameterValuesBeforeExecuting(alg, parameters, context):
if alg.parameterAsBool(parameters, '-m', context):
return verifyRasterNum(alg, parameters, context, 'input', 7)
elif alg.parameterAsBool(parameters, '-n', context):
return verifyRasterNum(alg, parameters, context, 'input', 2)
elif (alg.parameterAsBool(parameters, '-l', context)
or alg.parameterAsBool(parameters, '-a', context)):
return verifyRasterNum(alg, parameters, context, 'input', 6)
return None

0 comments on commit 486bcd6

Please sign in to comment.
You can’t perform that action at this time.