r_colors.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """
  2. ***************************************************************************
  3. r_colors.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 grassprovider.Grass7Utils import Grass7Utils
  22. from processing.tools.system import getTempFilename
  23. def checkParameterValuesBeforeExecuting(alg, parameters, context):
  24. """ Verify if we have the right parameters """
  25. txtRules = alg.parameterAsString(parameters, 'rules_txt', context)
  26. rules = alg.parameterAsString(parameters, 'rules', context)
  27. if txtRules and rules:
  28. return False, alg.tr("You need to set either inline rules or a rules file!")
  29. return True, None
  30. def processInputs(alg, parameters, context, feedback):
  31. # import all rasters with their color tables (and their bands)
  32. # We need to import all the bands and color tables of the input rasters
  33. rasters = alg.parameterAsLayerList(parameters, 'map', context)
  34. for idx, layer in enumerate(rasters):
  35. layerName = 'map_{}'.format(idx)
  36. # Add a raster layer
  37. alg.loadRasterLayer(layerName, layer, context, False, None)
  38. # Optional raster layer to copy from
  39. raster = alg.parameterAsString(parameters, 'raster', context)
  40. if raster:
  41. alg.loadRasterLayerFromParameter('raster', parameters, context, False, None)
  42. alg.postInputs(context)
  43. def processCommand(alg, parameters, context, feedback):
  44. # Handle inline rules
  45. txtRules = alg.parameterAsString(parameters, 'txtrules', context)
  46. if txtRules:
  47. # Creates a temporary txt file
  48. tempRulesName = getTempFilename(context=context)
  49. # Inject rules into temporary txt file
  50. with open(tempRulesName, "w") as tempRules:
  51. tempRules.write(txtRules)
  52. alg.removeParameter('txtrules')
  53. parameters['rules'] = tempRulesName
  54. alg.processCommand(parameters, context, feedback, True)
  55. def processOutputs(alg, parameters, context, feedback):
  56. createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
  57. metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
  58. # Export all rasters with their color tables (and their bands)
  59. rasters = alg.parameterAsLayerList(parameters, 'map', context)
  60. outputDir = alg.parameterAsString(parameters, 'output_dir', context)
  61. for idx, raster in enumerate(rasters):
  62. rasterName = 'map_{}'.format(idx)
  63. fileName = os.path.join(outputDir, rasterName)
  64. outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
  65. alg.exportRasterLayer(alg.exportedLayers[rasterName], fileName, True,
  66. outFormat, createOpt, metaOpt)