r_category.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """
  2. ***************************************************************************
  3. r_category.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. from processing.tools.system import getTempFilename
  21. from grassprovider.Grass7Utils import Grass7Utils
  22. def checkParameterValuesBeforeExecuting(alg, parameters, context):
  23. """ Verify if we have the right parameters """
  24. rules = alg.parameterAsString(parameters, 'rules', context)
  25. txtrules = alg.parameterAsString(parameters, 'txtrules', context)
  26. raster = alg.parameterAsString(parameters, 'raster', context)
  27. if rules and txtrules:
  28. return False, alg.tr("You need to set either a rules file or write directly the rules!")
  29. elif (rules and raster) or (txtrules and raster):
  30. return False, alg.tr("You need to set either rules or a raster from which to copy categories!")
  31. return True, None
  32. def processInputs(alg, parameters, context, feedback):
  33. # If there is another raster to copy categories from
  34. # we need to import it with r.in.gdal rather than r.external
  35. raster = alg.parameterAsString(parameters, 'raster', context)
  36. if raster:
  37. alg.loadRasterLayerFromParameter('raster',
  38. parameters, context,
  39. False, None)
  40. alg.loadRasterLayerFromParameter('map', parameters, context)
  41. alg.postInputs(context)
  42. def processCommand(alg, parameters, context, feedback):
  43. # Handle inline rules
  44. txtRules = alg.parameterAsString(parameters, 'txtrules', context)
  45. if txtRules:
  46. # Creates a temporary txt file
  47. tempRulesName = getTempFilename(context=context)
  48. # Inject rules into temporary txt file
  49. with open(tempRulesName, "w") as tempRules:
  50. tempRules.write(txtRules)
  51. alg.removeParameter('txtrules')
  52. parameters['rules'] = tempRulesName
  53. alg.processCommand(parameters, context, feedback, True)
  54. def processOutputs(alg, parameters, context, feedback):
  55. # Output results ('map' layer)
  56. createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
  57. metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
  58. # We need to export the raster with all its bands and its color table
  59. fileName = alg.parameterAsOutputLayer(parameters, 'output', context)
  60. outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
  61. grassName = alg.exportedLayers['map']
  62. alg.exportRasterLayer(grassName, fileName, True,
  63. outFormat, createOpt, metaOpt)