temporal_raster3d_algebra.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. @author Thomas Leppelt and Soeren Gebbert
  8. """
  9. from temporal_raster_base_algebra import *
  10. ###############################################################################
  11. class TemporalRaster3DAlgebraParser(TemporalRasterBaseAlgebraParser):
  12. """The temporal raster algebra class"""
  13. def __init__(self, pid=None, run=False, debug=True, spatial = False):
  14. TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial)
  15. self.m_mapcalc = pygrass.Module('r3.mapcalc')
  16. def parse(self, expression, basename = None, overwrite=False):
  17. self.lexer = TemporalRasterAlgebraLexer()
  18. self.lexer.build()
  19. self.parser = yacc.yacc(module=self, debug=self.debug)
  20. self.overwrite = overwrite
  21. self.count = 0
  22. self.stdstype = "str3ds"
  23. self.basename = basename
  24. self.expression = expression
  25. self.parser.parse(expression)
  26. ######################### Temporal functions ##############################
  27. def p_statement_assign(self, t):
  28. # The expression should always return a list of maps.
  29. """
  30. statement : stds EQUALS expr
  31. """
  32. TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
  33. def p_ts_neighbor_operation(self, t):
  34. # Examples:
  35. # A[1,0,-1]
  36. # B[-2]
  37. # C[1,-2,1,3]
  38. """
  39. expr : stds L_SPAREN number COMMA number COMMA number R_SPAREN
  40. | stds L_SPAREN number R_SPAREN
  41. | stds L_SPAREN number COMMA number COMMA number COMMA number R_SPAREN
  42. """
  43. # Check input stds.
  44. maplist = self.check_stds(t[1])
  45. t_neighbor = 0
  46. row_neighbor = None
  47. col_neighbor = None
  48. depth_neighbor = None
  49. if len(t) == 5:
  50. t_neighbor = t[3]
  51. elif len(t) == 9:
  52. row_neighbor = t[3]
  53. col_neighbor = t[5]
  54. depth_neighbor = t[7]
  55. elif len(t) == 11:
  56. t_neighbor = t[9]
  57. row_neighbor = t[3]
  58. col_neighbor = t[5]
  59. depth_neighbor = t[7]
  60. if self.run:
  61. resultlist = []
  62. max_index = len(maplist)
  63. for map_i in maplist:
  64. # Get map index and temporal extent.
  65. map_index = maplist.index(map_i)
  66. new_index = map_index + t_neighbor
  67. if new_index < max_index and new_index >= 0:
  68. map_i_t_extent = map_i.get_temporal_extent()
  69. # Get neighboring map and set temporal extent.
  70. map_n = maplist[new_index]
  71. # Generate an intermediate map for the result map list.
  72. map_new = self.generate_new_map(map_n, bool_op = 'and', copy = True)
  73. map_new.set_temporal_extent(map_i_t_extent)
  74. # Create r.mapcalc expression string for the operation.
  75. if "cmd_list" in dir(map_new) and len(t) == 5:
  76. cmdstring = "%s" %(map_new.cmd_list)
  77. elif "cmd_list" not in dir(map_new) and len(t) == 5:
  78. cmdstring = "%s" %(map_n.get_id())
  79. elif "cmd_list" in dir(map_new) and len(t) in (9,11):
  80. cmdstring = "%s[%s,%s,%s]" %(map_new.cmd_list, row_neighbor, col_neighbor, depth_neighbor)
  81. elif "cmd_list" not in dir(map_new) and len(t) in (9,11):
  82. cmdstring = "%s[%s,%s,%s]" %(map_n.get_id(), row_neighbor, col_neighbor, depth_neighbor)
  83. # Set new command list for map.
  84. map_new.cmd_list = cmdstring
  85. # Append map with updated command list to result list.
  86. resultlist.append(map_new)
  87. t[0] = resultlist
  88. ###############################################################################
  89. if __name__ == "__main__":
  90. import doctest
  91. doctest.testmod()