224 lines
8.0 KiB
Python
Raw Normal View History

2016-04-24 12:38:06 +02:00
# -*- coding: utf-8 -*-
"""
***************************************************************************
i.py
----
Date : February 2016
Copyright : (C) 2016 by Médéric Ribreux
Email : medspx at medspx dot fr
***************************************************************************
* *
* 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__ = 'March 2016'
__copyright__ = '(C) 2016, Médéric Ribreux'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from processing.core.parameters import ParameterRaster, getParameterFromString
from processing.tools.system import isWindows
2016-05-02 14:15:33 +02:00
from ..Grass7Utils import Grass7Utils
from os import path
2016-04-24 12:38:06 +02:00
def multipleOutputDir(alg, field, basename=None):
"""
Handle multiple output of rasters into a
directory.
"""
# 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".format(basename)]
commands.append(" r.out.gdal -c -t 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):
"""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)
for idx in range(len(rasters)):
layer = rasters[idx]
if layer in alg.exportedLayers.keys():
continue
else:
destFilename = '{}{}'.format(rootFilename, idx + 1)
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 = \
unicode(alg.getParameterValue(alg.GRASS_REGION_EXTENT_PARAMETER))
regionCoords = region.split(',')
command = 'g.region'
command += ' -a'
command += ' n=' + unicode(regionCoords[3])
command += ' s=' + unicode(regionCoords[2])
command += ' e=' + unicode(regionCoords[1])
command += ' w=' + unicode(regionCoords[0])
cellsize = alg.getParameterValue(alg.GRASS_REGION_CELLSIZE_PARAMETER)
if cellsize:
command += ' res=' + unicode(cellsize)
else:
command += ' res=' + unicode(alg.getDefaultCellsize())
alignToResolution = \
alg.getParameterValue(alg.GRASS_REGION_ALIGN_TO_RESOLUTION)
if alignToResolution:
command += ' -a'
alg.commands.append(command)
return rootFilename
2016-05-02 14:15:33 +02:00
def regroupRasters(alg, field, groupField, subgroupField=None, sigsetField=None):
2016-04-24 12:38:06 +02:00
"""
Group multiple input rasters into a group
2016-05-02 14:15:33 +02:00
* If there is a subgroupField, a subgroup will automatically created.
* When a sigset file is provided, the file is copied into the sigset
directory of the subgroup.
2016-04-24 12:38:06 +02:00
"""
# List of rasters names
rasters = alg.getParameterFromName(field)
rastersList = rasters.value.split(';')
alg.parameters.remove(rasters)
# Insert a i.group command
group = getParameterFromString("ParameterString|{}|group of rasters|None|False|False".format(groupField))
group.value = alg.getTempFilename()
alg.addParameter(group)
if subgroupField:
subgroup = getParameterFromString("ParameterString|{}|subgroup of rasters|None|False|False".format(subgroupField))
subgroup.value = alg.getTempFilename()
alg.addParameter(subgroup)
command = 'i.group group={}{} input={}'.format(
group.value,
' subgroup={}'.format(subgroup.value) if subgroup else '',
','.join([alg.exportedLayers[f] for f in rastersList])
)
alg.commands.append(command)
2016-05-02 14:15:33 +02:00
# If there is a sigset and a subgroupField, we copy the sigset file
if subgroupField and sigsetField:
sigsetFile = alg.getParameterValue(sigsetField)
shortSigsetFile = path.basename(sigsetFile)
if sigsetFile:
interSigsetFile = path.join(Grass7Utils.grassMapsetFolder(),
'PERMANENT',
'group', group.value,
'subgroup', subgroup.value,
'sigset', shortSigsetFile)
copyFile(alg, sigsetFile, interSigsetFile)
alg.setParameterValue(sigsetField, shortSigsetFile)
2016-04-24 12:38:06 +02:00
# modify parameters values
alg.processCommand()
# Re-add input rasters
alg.addParameter(rasters)
# Delete group:
alg.parameters.remove(group)
if subgroupField:
alg.parameters.remove(subgroup)
return group.value, subgroup.value
return group.value
def exportInputRasters(alg, rasterDic):
"""
Export input rasters
Use a dict to make input/output link:
{ 'inputName1': 'outputName1', 'inputName2': 'outputName2'}
"""
# Get inputs and outputs
for inputName, outputName in rasterDic.iteritems():
inputRaster = alg.getParameterValue(inputName)
outputRaster = alg.getOutputFromName(outputName)
command = 'r.out.gdal -c -t --overwrite createopt="TFW=YES,COMPRESS=LZW" input={} output=\"{}\"'.format(
alg.exportedLayers[inputRaster],
outputRaster.value
)
alg.commands.append(command)
alg.outputCommands.append(command)
def verifyRasterNum(alg, rasters, mini, maxi=None):
"""Verify if we have at least n rasters in multipleInput"""
num = len(alg.getParameterValue(rasters).split(';'))
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)
# Create output parameter
param = getParameterFromString("ParameterString|{}|output file|None|False|False".format(output))
param.value = outputFile.value
alg.addParameter(param)
return outputFile
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(
"MD" if isWindows() else "mkdir -p",
path.dirname(toFile)
)
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(
"MOVE /Y" if isWindows() else "mv -f",
fromFile,
toFile
)
alg.commands.append(command)
def copyFile(alg, fromFile, toFile):
""" Generates a copy command for GRASS7 script """
createDestDir(alg, toFile)
command = "{} {} {}".format(
"COPY /Y" if isWindows() else "cp -f",
fromFile,
toFile)
2016-04-24 12:38:06 +02:00
alg.commands.append(command)