temporal_raster3d_algebra.py 4.1 KB

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