r.mapcalc.simple.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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: Calculate new raster map from a r.mapcalc expression
  16. #% keyword: raster
  17. #% keyword: algebra
  18. #%end
  19. #%option
  20. #% key: expression
  21. #% type: string
  22. #% description: Formula (e.g. A-B or A*C+B)
  23. #% required : yes
  24. #%end
  25. #%option G_OPT_R_INPUT
  26. #% key: a
  27. #% description: A
  28. #% required : no
  29. #%end
  30. #%option G_OPT_R_INPUT
  31. #% key: b
  32. #% description: B
  33. #% required : no
  34. #%end
  35. #%option G_OPT_R_INPUT
  36. #% key: c
  37. #% description: C
  38. #% required : no
  39. #%end
  40. #%option G_OPT_R_INPUT
  41. #% key: d
  42. #% description: D
  43. #% required : no
  44. #%end
  45. #%option G_OPT_R_INPUT
  46. #% key: e
  47. #% description: E
  48. #% required : no
  49. #%end
  50. #%option G_OPT_R_INPUT
  51. #% key: f
  52. #% description: F
  53. #% required : no
  54. #%end
  55. #%option
  56. #% key: output
  57. #% description: Name for output raster map
  58. #% required : yes
  59. #%end
  60. #%option
  61. #% key: seed
  62. #% type: integer
  63. #% required: no
  64. #% multiple: no
  65. #% description: Seed for rand() function
  66. #%end
  67. #%flag
  68. #% key: s
  69. #% description: Generate random seed (result is non-deterministic)
  70. #%end
  71. #%flag
  72. #% key: q
  73. #% description: Quote the map names
  74. #%end
  75. #%flag
  76. #% key: c
  77. #% description: Case sensitive variable names
  78. #%end
  79. import sys
  80. import re
  81. import grass.script as gs
  82. def name_quote(name):
  83. return '"{}"'.format(name)
  84. def main():
  85. options, flags = gs.parser()
  86. expr = options['expression']
  87. if not expr:
  88. gs.fatal(_("The expression is an empty string"))
  89. output = options['output']
  90. quote = flags['q']
  91. re_flags = 0
  92. if flags['c']:
  93. re_flags = re.IGNORECASE
  94. if quote:
  95. output = name_quote(output)
  96. seed = None
  97. if options['seed']:
  98. seed = options['seed']
  99. elif flags['s']:
  100. seed = 'auto'
  101. variables = []
  102. for key in "ABCDEF":
  103. name = options[key.lower()]
  104. if name:
  105. if quote:
  106. name = name_quote(name)
  107. variables.append((key, name))
  108. for key, name in variables:
  109. find = r'([^a-zA-Z0-9]|^){key}([^a-zA-Z0-9]|$)'.format(key=key)
  110. replace = r'\1{}\2'.format(name)
  111. # we need to do the substitution twice because we are matching
  112. # also the char before and after which fails when there is only
  113. # one char between the two usages of the same var (e.g. A*A)
  114. expr = re.sub(find, replace, expr, flags=re_flags)
  115. expr = re.sub(find, replace, expr, flags=re_flags)
  116. expr = '{lhs} = {rhs}'.format(lhs=output, rhs=expr)
  117. gs.verbose(_("Expression: {}").format(expr))
  118. gs.mapcalc(expr, seed=seed)
  119. # g.message -e "Calculating $GIS_OPT_OUTFILE. Try expert mode."
  120. return 0
  121. if __name__ == "__main__":
  122. sys.exit(main())