r_statistics.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. ***************************************************************************
  3. r_statistics.py
  4. ---------------
  5. Date : September 2017
  6. Copyright : (C) 2017 by Médéric Ribreux
  7. Email : medspx at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'September 2017'
  19. __copyright__ = '(C) 2017, Médéric Ribreux'
  20. from qgis.core import QgsProcessingParameterString
  21. from grassprovider.Grass7Utils import Grass7Utils
  22. def processCommand(alg, parameters, context, feedback):
  23. # We had a new "output" parameter
  24. out = 'output{}'.format(alg.uniqueSuffix)
  25. p = QgsProcessingParameterString('~output', None, out, False, False)
  26. alg.addParameter(p)
  27. # We need to remove all outputs
  28. alg.processCommand(parameters, context, feedback, True)
  29. # Then we add a new command for treating results
  30. calcExpression = 'correctedoutput{}=@{}'.format(
  31. alg.uniqueSuffix, out)
  32. command = 'r.mapcalc expression="{}"'.format(calcExpression)
  33. alg.commands.append(command)
  34. def processOutputs(alg, parameters, context, feedback):
  35. createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
  36. metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
  37. # Export the results from correctedoutput
  38. grassName = 'correctedoutput{}'.format(alg.uniqueSuffix)
  39. fileName = alg.parameterAsOutputLayer(
  40. parameters, 'routput', context)
  41. outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
  42. alg.exportRasterLayer(grassName, fileName, True,
  43. outFormat, createOpt, metaOpt)