r_series_interp.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. """
  2. ***************************************************************************
  3. r_series_interp.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. def checkParameterValuesBeforeExecuting(alg, parameters, context):
  23. """ Verify if we have the right parameters """
  24. datapos = alg.parameterAsDouble(parameters, 'datapos', context)
  25. infile = alg.parameterAsString(parameters, 'infile', context)
  26. output = alg.parameterAsString(parameters, 'output', context)
  27. outfile = alg.parameterAsString(parameters, 'outfile', context)
  28. if datapos and infile:
  29. return False, alg.tr("You need to set either inline data positions or an input data positions file!")
  30. if output and outfile:
  31. return False, alg.tr("You need to set either sampling data positions or an output sampling data positions file!")
  32. if not (datapos or infile or output or outfile):
  33. return False, alg.tr("You need to set input and output data positions parameters!")
  34. return True, None
  35. def processCommand(alg, parameters, context, feedback):
  36. # We temporary remove the output directory
  37. alg.processCommand(parameters, context, feedback, True)
  38. def processOutputs(alg, parameters, context, feedback):
  39. # We take all the outputs and we export them to the output directory
  40. outputDir = alg.parameterAsString(parameters, 'output_dir', context)
  41. output = alg.parameterAsString(parameters, 'output', context)
  42. outfile = alg.parameterAsString(parameters, 'outfile', context)
  43. outs = []
  44. if output:
  45. outs = output.split(',')
  46. elif outfile:
  47. # Handle file manually to find the name of the layers
  48. with open(outfile) as f:
  49. for line in f:
  50. if '|' in line:
  51. outs.append(line.split('|')[0])
  52. createOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_OPT, context)
  53. metaOpt = alg.parameterAsString(parameters, alg.GRASS_RASTER_FORMAT_META, context)
  54. for out in outs:
  55. # We need to export the raster with all its bands and its color table
  56. fileName = os.path.join(outputDir, out)
  57. outFormat = Grass7Utils.getRasterFormatFromFilename(fileName)
  58. alg.exportRasterLayer(out, fileName, True,
  59. outFormat, createOpt, metaOpt)