temporal_raster3d_algebra.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. """
  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, nprocs = 1, register_null = False):
  14. TemporalRasterBaseAlgebraParser.__init__(self, pid, run, debug, spatial, nprocs, register_null)
  15. self.m_mapcalc = pymod.Module('r3.mapcalc')
  16. self.m_mremove = pymod.Module('g.remove')
  17. def parse(self, expression, basename = None, overwrite=False):
  18. # Check for space time dataset type definitions from temporal algebra
  19. l = TemporalRasterAlgebraLexer()
  20. l.build()
  21. l.lexer.input(expression)
  22. while True:
  23. tok = l.lexer.token()
  24. if not tok: break
  25. if tok.type == "STVDS" or tok.type == "STRDS" or tok.type == "STR3DS":
  26. raise SyntaxError("Syntax error near '%s'" %(tok.type))
  27. self.lexer = TemporalRasterAlgebraLexer()
  28. self.lexer.build()
  29. self.parser = yacc.yacc(module=self, debug=self.debug)
  30. self.overwrite = overwrite
  31. self.count = 0
  32. self.stdstype = "str3ds"
  33. self.maptype = "raster_3d"
  34. self.mapclass = Raster3DDataset
  35. self.basename = basename
  36. self.expression = expression
  37. self.parser.parse(expression)
  38. ######################### Temporal functions ##############################
  39. def p_statement_assign(self, t):
  40. # The expression should always return a list of maps.
  41. """
  42. statement : stds EQUALS expr
  43. """
  44. TemporalRasterBaseAlgebraParser.p_statement_assign(self, t)
  45. def p_ts_neighbor_operation(self, t):
  46. # Examples:
  47. # A[1,0,-1]
  48. # B[-2]
  49. # C[1,-2,1,3]
  50. """
  51. expr : stds L_SPAREN number COMMA number COMMA number R_SPAREN
  52. | stds L_SPAREN number R_SPAREN
  53. | stds L_SPAREN number COMMA number COMMA number COMMA number R_SPAREN
  54. """
  55. # Check input stds.
  56. maplist = self.check_stds(t[1])
  57. t_neighbor = 0
  58. row_neighbor = None
  59. col_neighbor = None
  60. depth_neighbor = None
  61. if len(t) == 5:
  62. t_neighbor = t[3]
  63. elif len(t) == 9:
  64. row_neighbor = t[3]
  65. col_neighbor = t[5]
  66. depth_neighbor = t[7]
  67. elif len(t) == 11:
  68. t_neighbor = t[9]
  69. row_neighbor = t[3]
  70. col_neighbor = t[5]
  71. depth_neighbor = t[7]
  72. if self.run:
  73. resultlist = []
  74. max_index = len(maplist)
  75. for map_i in maplist:
  76. # Get map index and temporal extent.
  77. map_index = maplist.index(map_i)
  78. new_index = map_index + t_neighbor
  79. if new_index < max_index and new_index >= 0:
  80. map_i_t_extent = map_i.get_temporal_extent()
  81. # Get neighboring map and set temporal extent.
  82. map_n = maplist[new_index]
  83. # Generate an intermediate map for the result map list.
  84. map_new = self.generate_new_map(map_n, bool_op = 'and', copy = True)
  85. map_new.set_temporal_extent(map_i_t_extent)
  86. # Create r.mapcalc expression string for the operation.
  87. if "cmd_list" in dir(map_new) and len(t) == 5:
  88. cmdstring = "%s" %(map_new.cmd_list)
  89. elif "cmd_list" not in dir(map_new) and len(t) == 5:
  90. cmdstring = "%s" %(map_n.get_id())
  91. elif "cmd_list" in dir(map_new) and len(t) in (9,11):
  92. cmdstring = "%s[%s,%s,%s]" %(map_new.cmd_list, row_neighbor, col_neighbor, depth_neighbor)
  93. elif "cmd_list" not in dir(map_new) and len(t) in (9,11):
  94. cmdstring = "%s[%s,%s,%s]" %(map_n.get_id(), row_neighbor, col_neighbor, depth_neighbor)
  95. # Set new command list for map.
  96. map_new.cmd_list = cmdstring
  97. # Append map with updated command list to result list.
  98. resultlist.append(map_new)
  99. t[0] = resultlist
  100. ###############################################################################
  101. if __name__ == "__main__":
  102. import doctest
  103. doctest.testmod()