temporal_raster3d_algebra.py 5.2 KB

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