r.mapcalc.simple.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/env python
  2. """
  3. MODULE: r.mapcalc.simple
  4. AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  5. R. Brunzema <r.brunzema web de> (original 5.0 version)
  6. Michael Barton <michael.barton asu edu> (update to GRASS 5.7)
  7. Huidae Cho <grass4u gmail com> (removed bashism)
  8. PURPOSE: Provides wrapper friendly wrapper to r.mapcalc
  9. COPYRIGHT: (C) 2018 by Vaclav Petras and the GRASS Development Team
  10. This program is free software under the GNU General Public
  11. License (>=v2). Read the file COPYING that comes with GRASS
  12. for details.
  13. """
  14. #%module
  15. #% description: Calculates a new raster map from a simple r.mapcalc expression.
  16. #% keyword: raster
  17. #% keyword: algebra
  18. #% keyword: simple
  19. #%end
  20. #%option
  21. #% key: expression
  22. #% type: string
  23. #% description: Formula (e.g. A-B or A*C+B)
  24. #% required : yes
  25. #%end
  26. #%option G_OPT_R_INPUT
  27. #% key: a
  28. #% description: Name of input A raster map
  29. #% required : no
  30. #% guisection: Input maps
  31. #%end
  32. #%option G_OPT_R_INPUT
  33. #% key: b
  34. #% description: Name of input B raster map
  35. #% required : no
  36. #% guisection: Input maps
  37. #%end
  38. #%option G_OPT_R_INPUT
  39. #% key: c
  40. #% description: Name of input C raster map
  41. #% required : no
  42. #% guisection: Input maps
  43. #%end
  44. #%option G_OPT_R_INPUT
  45. #% key: d
  46. #% description: Name of input D raster map
  47. #% required : no
  48. #% guisection: Input maps
  49. #%end
  50. #%option G_OPT_R_INPUT
  51. #% key: e
  52. #% description: Name of input E raster map
  53. #% required : no
  54. #% guisection: Input maps
  55. #%end
  56. #%option G_OPT_R_INPUT
  57. #% key: f
  58. #% description: Name of input F raster map
  59. #% required : no
  60. #% guisection: Input maps
  61. #%end
  62. #%option G_OPT_R_OUTPUT
  63. #%end
  64. #%option
  65. #% key: seed
  66. #% type: integer
  67. #% required: no
  68. #% multiple: no
  69. #% description: Seed for rand() function
  70. #% guisection: Random
  71. #%end
  72. #%flag
  73. #% key: s
  74. #% description: Generate random seed (result is non-deterministic)
  75. #% guisection: Random
  76. #%end
  77. #%flag
  78. #% key: q
  79. #% description: Quote the map names
  80. #% guisection: Input maps
  81. #%end
  82. #%flag
  83. #% key: c
  84. #% description: Case sensitive variable names
  85. #%end
  86. import sys
  87. import re
  88. import grass.script as gs
  89. def name_quote(name):
  90. return '"{}"'.format(name)
  91. def main():
  92. options, flags = gs.parser()
  93. expr = options['expression']
  94. if not expr:
  95. gs.fatal(_("The expression is an empty string"))
  96. output = options['output']
  97. quote = flags['q']
  98. re_flags = 0
  99. if flags['c']:
  100. re_flags = re.IGNORECASE
  101. if quote:
  102. output = name_quote(output)
  103. seed = None
  104. if options['seed']:
  105. seed = options['seed']
  106. elif flags['s']:
  107. seed = 'auto'
  108. variables = []
  109. for key in "ABCDEF":
  110. name = options[key.lower()]
  111. if name:
  112. if quote:
  113. name = name_quote(name)
  114. variables.append((key, name))
  115. for key, name in variables:
  116. find = r'([^a-zA-Z0-9]|^){key}([^a-zA-Z0-9]|$)'.format(key=key)
  117. replace = r'\1{}\2'.format(name)
  118. # we need to do the substitution twice because we are matching
  119. # also the char before and after which fails when there is only
  120. # one char between the two usages of the same var (e.g. A*A)
  121. expr = re.sub(find, replace, expr, flags=re_flags)
  122. expr = re.sub(find, replace, expr, flags=re_flags)
  123. expr = '{lhs} = {rhs}'.format(lhs=output, rhs=expr)
  124. gs.verbose(_("Expression: {}").format(expr))
  125. gs.mapcalc(expr, seed=seed)
  126. # g.message -e "Calculating $GIS_OPT_OUTFILE. Try expert mode."
  127. return 0
  128. if __name__ == "__main__":
  129. sys.exit(main())