r_horizon.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. ***************************************************************************
  3. r_horizon.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. import os
  21. import math
  22. def checkParameterValuesBeforeExecuting(alg, parameters, context):
  23. """ Verify if we have the right parameters """
  24. start = alg.parameterAsDouble(parameters, 'start', context)
  25. end = alg.parameterAsDouble(parameters, 'end', context)
  26. step = alg.parameterAsDouble(parameters, 'step', context)
  27. if start >= end:
  28. return False, alg.tr("The start position must be inferior to the end position!")
  29. if step == 0.0:
  30. return False, alg.tr("The step must be greater than zero!")
  31. return True, None
  32. def processOutputs(alg, parameters, context, feedback):
  33. # Inspired from GRASS implementation
  34. def getNumberDecimals(number):
  35. """ Return the number of decimals of a number (int or float) """
  36. if int(number) == number:
  37. return 0
  38. return len(str(number).split(".")[-1])
  39. def doubleToBaseName(number, nDecimals):
  40. """
  41. Format filename, according to GRASS implementation,
  42. based on provided number and number of decimals
  43. """
  44. number += 0.0001
  45. # adapted from GRASS https://github.com/OSGeo/grass/blob/6253da1bd6ce48d23419e99e8b503edf46178490/lib/gis/basename.c#L97-L101
  46. if nDecimals == 0:
  47. return f'{int(number):03}'
  48. int_part = int(number)
  49. dec_part = int((number - int_part) * pow(10, nDecimals))
  50. return f'{int_part:03}_{str(dec_part).rjust(nDecimals, "0")}'
  51. # There will be as many outputs as the difference between start and end divided by steps
  52. start = alg.parameterAsDouble(parameters, 'start', context)
  53. end = alg.parameterAsDouble(parameters, 'end', context)
  54. step = alg.parameterAsDouble(parameters, 'step', context)
  55. direction = alg.parameterAsDouble(parameters, 'direction', context)
  56. first_rad = math.radians(start + direction)
  57. nDecimals = getNumberDecimals(step)
  58. dfr_rad = math.radians(step)
  59. arrayNumInt = int((end - start) / abs(step))
  60. directory = alg.parameterAsString(parameters, 'output', context)
  61. # Needed if output to a temporary directory
  62. os.makedirs(directory, exist_ok=True)
  63. for k in range(arrayNumInt):
  64. angle_deg = math.degrees(first_rad + dfr_rad * k)
  65. baseName = doubleToBaseName(angle_deg, nDecimals)
  66. grassName = f'output{alg.uniqueSuffix}_{baseName}'
  67. fileName = f'{os.path.join(directory, baseName)}.tif'
  68. alg.exportRasterLayer(grassName, fileName)