t.rast3d.algebra.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rast3d.algebra
  6. # AUTHOR(S): Thomas Leppelt, Soeren Gebbert
  7. #
  8. # PURPOSE: Provide temporal 3D raster algebra to perform spatial an temporal operations
  9. # for space time datasets by topological relationships to other space time
  10. # datasets.
  11. # COPYRIGHT: (C) 2014 by the GRASS Development Team
  12. #
  13. # This program is free software under the GNU General Public
  14. # License (version 2). Read the file COPYING that comes with GRASS
  15. # for details.
  16. #
  17. #############################################################################
  18. #%module
  19. #% description: Apply temporal and spatial operations on space time 3D raster datasets using temporal 3D raster algebra.
  20. #% keyword: temporal
  21. #% keyword: algebra
  22. #% keyword: raster3d
  23. #% keyword: voxel
  24. #% keyword: time
  25. #%end
  26. #%option
  27. #% key: expression
  28. #% type: string
  29. #% description: Algebraic expression for temporal and spatial analysis of space time 3D raster datasets
  30. #% required : yes
  31. #%end
  32. #%option
  33. #% key: basename
  34. #% type: string
  35. #% label: Basename of the new generated output maps
  36. #% description: A numerical suffix separated by an underscore will be attached to create a unique identifier
  37. #% required: yes
  38. #%end
  39. #%option
  40. #% key: nprocs
  41. #% type: integer
  42. #% description: Number of r3.mapcalc processes to run in parallel
  43. #% required: no
  44. #% multiple: no
  45. #% answer: 1
  46. #%end
  47. #%flag
  48. #% key: s
  49. #% description: Activate spatial topology
  50. #%end
  51. #%flag
  52. #% key: n
  53. #% description: Register Null maps
  54. #%end
  55. #%flag
  56. #% key: g
  57. #% description: Use granularity sampling instead of the temporal topology approach
  58. #%end
  59. import grass.script
  60. import grass.temporal as tgis
  61. import sys
  62. def main():
  63. expression = options['expression']
  64. basename = options['basename']
  65. nprocs = options["nprocs"]
  66. spatial = flags["s"]
  67. register_null = flags["n"]
  68. granularity = flags["g"]
  69. # Check for PLY istallation
  70. try:
  71. import ply.lex as lex
  72. import ply.yacc as yacc
  73. except:
  74. grass.script.fatal(_("Please install PLY (Lex and Yacc Python implementation) to use the temporal algebra modules. "
  75. "You can use t.rast3d.mapcalc that provides a limited but useful alternative to "
  76. "t.rast3d.mapcalc2 without PLY requirement."))
  77. tgis.init(True)
  78. p = tgis.TemporalRaster3DAlgebraParser(run = True, debug=False, spatial = spatial, nprocs = nprocs, register_null = register_null)
  79. if granularity:
  80. if not p.setup_common_granularity(expression=expression, stdstype = 'str3ds', lexer = tgis.TemporalRasterAlgebraLexer()):
  81. grass.script.fatal(_("Unable to process the expression in granularity algebra mode"))
  82. p.parse(expression, basename, grass.script.overwrite())
  83. if __name__ == "__main__":
  84. options, flags = grass.script.parser()
  85. sys.exit(main())