Skip to content

Commit

Permalink
Fix r.li.* description files and ext scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Médéric RIBREUX committed Dec 27, 2017
1 parent 9b6f98a commit 1f8fbf3
Show file tree
Hide file tree
Showing 53 changed files with 242 additions and 410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|first|Name of first raster map for blending|None|False
QgsProcessingParameterRasterLayer|second|Name of second raster map for blending|None|False
QgsProcessingParameterNumber|percent|Percentage weight of first map for color blending|QgsProcessingParameterNumber.Double|50.0|True|0.0|100.0
Hardcoded|output=blended
QgsProcessingParameterRasterDestination|output_red|Blended Red
QgsProcessingParameterRasterDestination|output_green|Blended Green
QgsProcessingParameterRasterDestination|output_blue|Blended Blue
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFile|path|Name of file that contains the weight to calculate the index|False|txt|None|False
QgsProcessingParameterFileDestination|output|CWED|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|CWED|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Dominance|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Dominance|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ QgsProcessingParameterString|config_txt|Landscape structure configuration|None|T
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterString|patch_type|The value of the patch type|None|False|True
QgsProcessingParameterBoolean|-b|Exclude border edges|False
QgsProcessingParameterFileDestination|output|Edge Density|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Edge Density|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Mean Pixel Attribute|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Mean Pixel Attribute|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None
QgsProcessingParameterFileDestination|output|Mean Patch Size|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Mean Patch Size|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|PADCV|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|PADCV|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Pad Range|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Pad Range|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Patch Area SD|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Patch Area SD|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Patch Density|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Patch Density|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Patch Number|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Patch Number|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Pielou|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Pielou|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterString|alpha|Alpha value is the order of the generalized entropy|None|False|False
QgsProcessingParameterFileDestination|output|Renyi|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Renyi|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Richness|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Richness|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Shannon|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Shannon|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Shape|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Shape|Txt files (*.txt)|None|False
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ Raster (r.*)
QgsProcessingParameterRasterLayer|input|Name of input raster map|None|False
QgsProcessingParameterString|config_txt|Landscape structure configuration|None|True|True
QgsProcessingParameterFile|config|Landscape structure configuration file|False|txt|None|True
QgsProcessingParameterFileDestination|output|Simpson|Txt files (*.txt)|None|False
QgsProcessingParameterFileDestination|output_txt|Simpson|Txt files (*.txt)|None|False
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/grass7/ext/r_blend_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

"""
***************************************************************************
r_blend.py
----------
r_blend_combine.py
------------------
Date : February 2016
Copyright : (C) 2016 by Médéric Ribreux
Email : medspx at medspx dot fr
Expand All @@ -29,7 +29,8 @@
def processInputs(alg, parameters, context):
if 'first' and 'second' in alg.exportedLayers:
return


# Use v.in.ogr
for name in ['first', 'second']:
alg.loadRasterLayerFromParameter(name, parameters, context, False, None)
alg.postInputs()
Expand Down
80 changes: 22 additions & 58 deletions python/plugins/processing/algs/grass7/ext/r_blend_rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,71 +25,35 @@

__revision__ = '$Format:%H$'

from processing.core.outputs import getOutputFromString

import os
from processing.algs.grass7.Grass7Utils import Grass7Utils

def processInputs(alg):
# If there is another raster to copy categories from
# we need to import it with r.in.gdal rather than r.external
first = alg.getParameterValue(u'first')
second = alg.getParameterValue(u'second')
if first in list(alg.exportedLayers.keys()) and second in list(alg.exportedLayers.keys()):
def processInputs(alg, parameters, context):
if 'first' and 'second' in alg.exportedLayers:
return

# Use v.in.ogr
for name in ['first', 'second']:
alg.loadRasterLayerFromParameter(name, parameters, context, False, None)
alg.postInputs()

for raster in [first, second]:
alg.setSessionProjectionFromLayer(raster, alg.commands)

destFilename = alg.getTempFilename()
alg.exportedLayers[raster] = destFilename
command = 'r.in.gdal input={} output={} --overwrite -o'.format(raster, 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)


def processCommand(alg, parameters):
def processCommand(alg, parameters, context):
# We need to remove all outputs
basename = getOutputFromString('OutputRaster|output|Output basename')
basename.value = 'output'
alg.addOutput(basename)
outputNames = ['output_{}'.format(f) for f in ['red', 'green', 'blue']]
outputs = [alg.getOutputFromName(f) for f in outputNames]
for output in outputNames:
alg.exportedLayers[alg.getOutputValue(output)] = 'output' + alg.uniqueSuffix
alg.removeOutputFromName(output)

alg.processCommand()

# And to re-add them
alg.removeOutputFromName('output')
for output in outputs:
alg.addOutput(output)
alg.processCommand(parameters, context, True)

def processOutputs(alg, parameters, context):
createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)

def processOutputs(alg):
# Export each color raster
colors = ['red', 'green', 'blue']
for color in colors:
output = alg.getOutputValue('output_{}'.format(color))
command = "r.out.gdal input={} output=\"{}\" createopt=\"TFW=YES,COMPRESS=LZW\"".format(
alg.exportedLayers[output] + '.' + color[0], output)
alg.commands.append(command)
alg.outputCommands.append(command)
fileName = os.path.normpath(
alg.parameterAsOutputLayer(parameters, 'output_{}'.format(color), context))
outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
alg.exportRasterLayer('blended.{}'.format(color[0]),
fileName, True, outFormat, createOpt, metaOpt)



72 changes: 32 additions & 40 deletions python/plugins/processing/algs/grass7/ext/r_li.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
__revision__ = '$Format:%H$'

import shutil
from processing.tools.system import isWindows, mkdir
from processing.core.parameters import getParameterFromString
from qgis.core import QgsProcessingParameterString
from processing.tools.system import (isWindows, mkdir,
getTempFilename)
import os
from copy import deepcopy

# for MS-Windows users who have MBCS chars in their name:
if os.name == 'nt':
Expand Down Expand Up @@ -85,69 +85,61 @@ def checkMovingWindow(alg, parameters, context, outputTxt=False):


def configFile(alg, parameters, context, outputTxt=False):
""" Handle inline configuration
"""Handle inline configuration
:param parameters:
"""
# Where is the GRASS7 user directory ?

new_parameters = deepcopy(parameters)

userGrass7Path = rliPath()
if not os.path.isdir(userGrass7Path):
mkdir(userGrass7Path)
if not os.path.isdir(os.path.join(userGrass7Path, 'output')):
mkdir(os.path.join(userGrass7Path, 'output'))
origConfigFile = new_parameters['config']

# If we have a configuration file, we need to copy it into user dir
if parameters['config']:
fileName = alg.parameterAsString(parameters, 'config', context)
configFilePath = os.path.join(userGrass7Path, os.path.basename(fileName))
# Copy the file
shutil.copy(parameters['config'], configFilePath)
# Change the parameter value
parameters['config'] = os.path.basename(configFilePath)
# Handle inline configuration
if new_parameters['config_txt']:
elif parameters['config_txt']:
# Creates a temporary txt file in user r.li directory
tempConfig = alg.getTempFilename()
tempConfig = os.path.basename(getTempFilename())
configFilePath = os.path.join(userGrass7Path, tempConfig)
# Inject rules into temporary txt file
with open(configFilePath, "w") as f:
f.write(new_parameters['config_txt'])
f.write(alg.parameterAsString(parameters, 'config_txt', context))
f.write("\n")

# Use temporary file as rules file
new_parameters['config'] = os.path.basename(configFilePath)
del new_parameters['config_txt']

# If we have a configuration file, we need to copy it into user dir
if origConfigFile:
configFilePath = os.path.join(userGrass7Path, os.path.basename(origConfigFile))
# Copy the file
shutil.copy(origConfigFile, configFilePath)
parameters['config'] = os.path.basename(configFilePath)
alg.removeParameter('config_txt')

# Change the parameter value
new_parameters['config'] = os.path.basename(configFilePath)

origOutput = alg.getOutputFromName('output')
if new_parameters['output']:
param = getParameterFromString("ParameterString|output|txt output|None|False|True")
new_parameters[param.name()] = origOutput.value
alg.removeOutputFromName('output')
# For ascii output, we need a virtual output
if outputTxt:
param = QgsProcessingParameterString(
'output', 'virtual output',
'a' + os.path.basename(getTempFilename()),
False, False)
alg.addParameter(param)

alg.processCommand(new_parameters, context)
alg.processCommand(parameters, context, outputTxt)

# Remove Config file:
removeConfigFile(alg, new_parameters, context)

# re-add configTxt
if outputTxt:
alg.addOutput(origOutput)

removeConfigFile(alg, parameters, context)

def moveOutputTxtFile(alg, parameters, context):
# Find output file name:
origOutput = alg.getOutputValue('output')
txtPath = alg.parameterAsString(parameters, 'output_txt', context)
userGrass7Path = rliPath()

outputDir = os.path.join(userGrass7Path, 'output')
output = os.path.join(outputDir, os.path.basename(origOutput))

output = os.path.join(userGrass7Path, 'output',
alg.parameterAsString(parameters, 'output', context))
# move the file
if isWindows():
command = "MOVE /Y {} {}".format(output, origOutput)
command = "MOVE /Y {} {}".format(output, txtPath)
else:
command = "mv -f {} {}".format(output, origOutput)
command = "mv -f {} {}".format(output, txtPath)
alg.commands.append(command)
8 changes: 4 additions & 4 deletions python/plugins/processing/algs/grass7/ext/r_li_cwed.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
from .r_li import checkMovingWindow, configFile


def checkParameterValuesBeforeExecuting(alg):
return checkMovingWindow(alg)
def checkParameterValuesBeforeExecuting(alg, parameters, context):
return checkMovingWindow(alg, parameters, context)


def processCommand(alg, parameters):
configFile(alg, parameters)
def processCommand(alg, parameters, context):
configFile(alg, parameters, context)
12 changes: 6 additions & 6 deletions python/plugins/processing/algs/grass7/ext/r_li_cwed_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@
from .r_li import checkMovingWindow, configFile, moveOutputTxtFile


def checkParameterValuesBeforeExecuting(alg):
return checkMovingWindow(alg, True)
def checkParameterValuesBeforeExecuting(alg, parameters, context):
return checkMovingWindow(alg, parameters, context, True)


def processCommand(alg, parameters):
configFile(alg, parameters, True)
def processCommand(alg, parameters, context):
configFile(alg, parameters, context, True)


def processOutputs(alg):
moveOutputTxtFile(alg)
def processOutputs(alg, parameters, context):
moveOutputTxtFile(alg, parameters, context)
9 changes: 5 additions & 4 deletions python/plugins/processing/algs/grass7/ext/r_li_dominance.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
from .r_li import checkMovingWindow, configFile


def checkParameterValuesBeforeExecuting(alg):
return checkMovingWindow(alg)
def checkParameterValuesBeforeExecuting(alg, parameters, context):
return checkMovingWindow(alg, parameters, context)


def processCommand(alg, parameters):
configFile(alg, parameters)
def processCommand(alg, parameters, context):
configFile(alg, parameters, context)

Loading

0 comments on commit 1f8fbf3

Please sign in to comment.