temporal_raster_algebra.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """!@package grass.temporal
  2. Temporal raster algebra
  3. (C) 2013 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Thomas Leppelt and Soeren Gebbert
  8. .. code-block:: python
  9. >>> p = TemporalRasterAlgebraLexer()
  10. >>> p.build()
  11. >>> p.debug = True
  12. >>> expression = 'R = A[0,1,0] / B[0,0,1] * 20 + C[0,1,1] - 2.45'
  13. >>> p.test(expression)
  14. R = A[0,1,0] / B[0,0,1] * 20 + C[0,1,1] - 2.45
  15. LexToken(NAME,'R',1,0)
  16. LexToken(EQUALS,'=',1,2)
  17. LexToken(NAME,'A',1,4)
  18. LexToken(L_SPAREN,'[',1,5)
  19. LexToken(INT,0,1,6)
  20. LexToken(COMMA,',',1,7)
  21. LexToken(INT,1,1,8)
  22. LexToken(COMMA,',',1,9)
  23. LexToken(INT,0,1,10)
  24. LexToken(R_SPAREN,']',1,11)
  25. LexToken(DIV,'/',1,13)
  26. LexToken(NAME,'B',1,15)
  27. LexToken(L_SPAREN,'[',1,16)
  28. LexToken(INT,0,1,17)
  29. LexToken(COMMA,',',1,18)
  30. LexToken(INT,0,1,19)
  31. LexToken(COMMA,',',1,20)
  32. LexToken(INT,1,1,21)
  33. LexToken(R_SPAREN,']',1,22)
  34. LexToken(MULT,'*',1,24)
  35. LexToken(INT,20,1,26)
  36. LexToken(ADD,'+',1,29)
  37. LexToken(NAME,'C',1,31)
  38. LexToken(L_SPAREN,'[',1,32)
  39. LexToken(INT,0,1,33)
  40. LexToken(COMMA,',',1,34)
  41. LexToken(INT,1,1,35)
  42. LexToken(COMMA,',',1,36)
  43. LexToken(INT,1,1,37)
  44. LexToken(R_SPAREN,']',1,38)
  45. LexToken(SUB,'-',1,40)
  46. LexToken(FLOAT,2.45,1,42)
  47. """
  48. from temporal_raster_base_algebra import *
  49. ###############################################################################
  50. class TemporalRasterAlgebraParser(TemporalRasterBaseAlgebraParser):
  51. """The temporal raster algebra class"""
  52. def __init__(self, pid=None, run=False, debug=True, spatial = False, nprocs = 1, register_null = False):
  53. TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs, register_null)
  54. self.m_mapcalc = pymod.Module('r.mapcalc')
  55. self.m_mremove = pymod.Module('g.remove')
  56. def parse(self, expression, basename = None, overwrite=False):
  57. self.lexer = TemporalRasterAlgebraLexer()
  58. self.lexer.build()
  59. self.parser = yacc.yacc(module=self, debug=self.debug)
  60. self.overwrite = overwrite
  61. self.count = 0
  62. self.stdstype = "strds"
  63. self.maptype = "rast"
  64. self.mapclass = RasterDataset
  65. self.basename = basename
  66. self.expression = expression
  67. self.parser.parse(expression)
  68. def remove_empty_maps(self):
  69. """! Removes the intermediate raster maps.
  70. """
  71. if self.empty_maps:
  72. self.msgr.message(_("Removing empty raster maps"))
  73. namelist = self.empty_maps.values()
  74. max = 100
  75. chunklist = [namelist[i:i + max] for i in range(0, len(namelist), max)]
  76. for chunk in chunklist:
  77. stringlist = ",".join(chunk)
  78. if self.run:
  79. m = copy.deepcopy(self.m_mremove)
  80. m.inputs["type"].value = "rast"
  81. m.inputs["name"].value = stringlist
  82. m.flags["f"].value = True
  83. m.run()
  84. ######################### Temporal functions ##############################
  85. def p_statement_assign(self, t):
  86. # The expression should always return a list of maps.
  87. """
  88. statement : stds EQUALS expr
  89. """
  90. TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
  91. def p_ts_neighbour_operation(self, t):
  92. # Examples:
  93. # A[1,0]
  94. # B[-2]
  95. # C[-2,1,3]
  96. """
  97. expr : stds L_SPAREN number COMMA number R_SPAREN
  98. | stds L_SPAREN number R_SPAREN
  99. | stds L_SPAREN number COMMA number COMMA number R_SPAREN
  100. """
  101. # Check input stds.
  102. maplist = self.check_stds(t[1])
  103. row_neigbour = None
  104. col_neigbour = None
  105. if len(t) == 5:
  106. t_neighbour = t[3]
  107. elif len(t) == 7:
  108. t_neighbour = 0
  109. row_neigbour = t[3]
  110. col_neigbour = t[5]
  111. elif len(t) == 9:
  112. t_neighbour = t[7]
  113. row_neigbour = t[3]
  114. col_neigbour = t[5]
  115. if self.run:
  116. resultlist = []
  117. max_index = len(maplist)
  118. for map_i in maplist:
  119. # Get map index and temporal extent.
  120. map_index = maplist.index(map_i)
  121. new_index = map_index + t_neighbour
  122. if new_index < max_index and new_index >= 0:
  123. map_i_t_extent = map_i.get_temporal_extent()
  124. # Get neighbouring map and set temporal extent.
  125. map_n = maplist[new_index]
  126. # Generate an intermediate map for the result map list.
  127. map_new = self.generate_new_map(map_n, bool_op = 'and', copy = True)
  128. map_new.set_temporal_extent(map_i_t_extent)
  129. # Create r.mapcalc expression string for the operation.
  130. if "cmd_list" in dir(map_new) and len(t) == 5:
  131. cmdstring = "%s" %(map_new.cmd_list)
  132. elif "cmd_list" not in dir(map_new) and len(t) == 5:
  133. cmdstring = "%s" %(map_n.get_id())
  134. elif "cmd_list" in dir(map_new) and len(t) in (7, 9):
  135. cmdstring = "%s[%s,%s]" %(map_new.cmd_list, row_neigbour, col_neigbour)
  136. elif "cmd_list" not in dir(map_new) and len(t) in (7, 9):
  137. cmdstring = "%s[%s,%s]" %(map_n.get_id(), row_neigbour, col_neigbour)
  138. # Set new command list for map.
  139. map_new.cmd_list = cmdstring
  140. # Append map with updated command list to result list.
  141. resultlist.append(map_new)
  142. t[0] = resultlist
  143. ###############################################################################
  144. if __name__ == "__main__":
  145. import doctest
  146. doctest.testmod()