From 61fc094054b8f455b29a44e170b6cba941179826 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A9d=C3=A9ric=20RIBREUX?= Date: Sun, 14 Feb 2016 15:34:22 +0100 Subject: [PATCH] Add r.support (combined with r.timestamp) algorithm --- .../algs/grass7/description/r.support.txt | 14 ++++ .../processing/algs/grass7/ext/r_support.py | 66 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 python/plugins/processing/algs/grass7/description/r.support.txt create mode 100644 python/plugins/processing/algs/grass7/ext/r_support.py diff --git a/python/plugins/processing/algs/grass7/description/r.support.txt b/python/plugins/processing/algs/grass7/description/r.support.txt new file mode 100644 index 000000000000..610657069936 --- /dev/null +++ b/python/plugins/processing/algs/grass7/description/r.support.txt @@ -0,0 +1,14 @@ +r.support +Allows creation and/or modification of raster map layer support files (metadata). +Raster (r.*) +ParameterRaster|map|Name of raster map|False +ParameterString|title|Title for resultant raster map|None|False|True +ParameterString|timestamp|r.timestamp date: Datetime, datetime1/datetime2, or 'none' to remove'|None|False|True +ParameterString|history|Text to append to the next line of the map's metadata file|None|True|True +ParameterString|units|Text to use for map data units|None|False|True +ParameterString|vdatum|Text to use for map vertical datum|None|False|True +ParameterString|source1|Text to use for data source, line 1|None|False|True +ParameterString|source2|Text to use for data source, line 2|None|False|True +ParameterString|description|Text to use for data description or keyword(s)|None|False|True +*ParameterFile|loadhistory|Text file from which to load history|False +OutputRaster|output|Metadata diff --git a/python/plugins/processing/algs/grass7/ext/r_support.py b/python/plugins/processing/algs/grass7/ext/r_support.py new file mode 100644 index 000000000000..fa08b14fa83f --- /dev/null +++ b/python/plugins/processing/algs/grass7/ext/r_support.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- + +""" +*************************************************************************** + r_support.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__ = 'February 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$' + +import os + + +def processCommand(alg): + # We temporary remove the output + out = alg.getOutputFromName('output') + mapParam = alg.getParameterValue('map') + alg.exportedLayers[out.value] = alg.exportedLayers[mapParam] + alg.removeOutputFromName('output') + timestamp = alg.getParameterValue('timestamp') + if timestamp: + command = "r.timestamp map={} date='{}'".format(alg.exportedLayers[mapParam], + timestamp) + alg.commands.append(command) + alg.parameters.remove(alg.getParameterFromName('timestamp')) + alg.processCommand() + + # We re-add the new output + alg.addOutput(out) + + +def processOutputs(alg): + tags = {'title': 'TIFFTAG_DOCUMENTNAME', 'description': 'TIFFTAG_IMAGEDESCRIPTION', + 'creator': 'TIFFTAG_ARTIST', 'timestamp': 'TIFFTAG_DATETIME', + 'source1': 'TIFFTAG_COPYRIGHT', 'units': 'TIFFTAG_RESOLUTIONUNIT', + 'source2': 'GRASS_SOURCE2', 'comments': 'GRASS_HISTORY', 'vdatum': 'GRASS_VDATUM'} + awk = "awk -F '=' '" + for support, tag in tags.items(): + awk = '{0} /{1}=".+"/{{ print \"{2}=\"substr($0,{3},length($0) - {3})"," }}'.format( + awk, support, tag, len(support) + 3) + + # Output results ('from' table and output table) + out = alg.getOutputValue('output') + command = u"SDF=$(r.info -e map={} | {}')".format(alg.exportedLayers[out], awk) + alg.commands.append(command) + command = u"r.out.gdal --overwrite -c createopt=\"TFW=YES,COMPRESS=LZW\" input={} output=\"{}\" metaopt=\"${{SDF%%,}}\"".format( + alg.exportedLayers[out], out) + alg.commands.append(command) + alg.outputCommands.append(command)