r_stats_quantile_rast.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """
  2. ***************************************************************************
  3. r_stats_quantile_rast.py
  4. ------------------------
  5. Date : February 2016
  6. Copyright : (C) 2016 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__ = 'February 2016'
  19. __copyright__ = '(C) 2016, Médéric Ribreux'
  20. import os
  21. from qgis.core import QgsProcessingParameterString
  22. from grassprovider.Grass7Utils import Grass7Utils
  23. def processCommand(alg, parameters, context, feedback):
  24. # We create the output sequence according to percentiles number
  25. quantiles = alg.parameterAsInt(parameters, 'quantiles', context) - 1
  26. outputs = []
  27. for i in range(0, int(quantiles)):
  28. outputs.append('output_{}'.format(i))
  29. param = QgsProcessingParameterString(
  30. 'output', 'virtual output',
  31. ','.join(outputs), False, False)
  32. alg.addParameter(param)
  33. # Removes outputs
  34. alg.processCommand(parameters, context, feedback, True)
  35. def processOutputs(alg, parameters, context, feedback):
  36. createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
  37. metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
  38. outputDir = alg.parameterAsString(parameters, 'output_dir', context)
  39. outputParam = alg.parameterAsString(parameters, 'output', context)
  40. outputs = outputParam.split(',')
  41. # We need to export each of the output
  42. for output in outputs:
  43. fileName = os.path.join(outputDir, output)
  44. outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
  45. alg.exportRasterLayer(output, fileName, True,
  46. outFormat, createOpt, metaOpt)