temporal_raster3d_algebra.py 5.1 KB

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