t.rast.out.vtk.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rast.out.vtk
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Export space time raster dataset as VTK time series
  9. # COPYRIGHT: (C) 2011-2014 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (version 2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%module
  17. #% description: Exports space time raster dataset as VTK time series.
  18. #% keywords: temporal
  19. #% keywords: export
  20. #% keywords: raster
  21. #% keywords: VTK
  22. #%end
  23. #%option G_OPT_STRDS_INPUT
  24. #%end
  25. #%option
  26. #% key: directory
  27. #% type: string
  28. #% description: Path to the export directory
  29. #% required: yes
  30. #% multiple: no
  31. #%end
  32. #%option G_OPT_R_ELEV
  33. #% required: no
  34. #%end
  35. #%option G_OPT_T_WHERE
  36. #%end
  37. #%option
  38. #% key: null
  39. #% type: double
  40. #% description: Value to represent no data cell
  41. #% required: no
  42. #% multiple: no
  43. #% answer: -99999.99
  44. #%end
  45. #%flag
  46. #% key: p
  47. #% description: Create VTK point data instead of VTK cell data (if no elevation map is given)
  48. #%end
  49. #%flag
  50. #% key: c
  51. #% description: Correct the coordinates to fit the VTK-OpenGL precision
  52. #%end
  53. #%flag
  54. #% key: g
  55. #% description: Export files using the space time dataset granularity for equidistant time between maps, where statement will be ignored
  56. #%end
  57. import os
  58. import grass.script as grass
  59. import grass.temporal as tgis
  60. from grass.exceptions import CalledModuleError
  61. ############################################################################
  62. def main():
  63. # Get the options
  64. input = options["input"]
  65. elevation = options["elevation"]
  66. expdir = options["directory"]
  67. where = options["where"]
  68. null = options["null"]
  69. use_pdata = flags["p"]
  70. coorcorr = flags["c"]
  71. use_granularity = flags["g"]
  72. # Make sure the temporal database exists
  73. tgis.init()
  74. if not os.path.exists(expdir):
  75. grass.fatal(_("Export directory <%s> not found.") % expdir)
  76. os.chdir(expdir)
  77. sp = tgis.open_old_stds(input, "strds")
  78. if use_granularity:
  79. # Attention: A list of lists of maps will be returned
  80. maps = sp.get_registered_maps_as_objects_by_granularity()
  81. # Create a NULL map in case of granularity support
  82. null_map = "temporary_null_map_%i" % os.getpid()
  83. grass.mapcalc("%s = null()" % (null_map))
  84. else:
  85. maps = sp.get_registered_maps_as_objects(where, "start_time", None)
  86. # To have scalar values with the same name, we need to copy the
  87. # raster maps using a single name
  88. map_name = "%s_%i" % (sp.base.get_name(), os.getpid())
  89. count = 0
  90. if maps is not None:
  91. for map in maps:
  92. if use_granularity:
  93. if map and len(map) > 0:
  94. id = map[0].get_map_id()
  95. else:
  96. id = map.get_map_id()
  97. # None ids will be replaced by NULL maps
  98. if id is None:
  99. id = null_map
  100. grass.run_command("g.copy", raster="%s,%s" % (id, map_name),
  101. overwrite=True)
  102. out_name = "%6.6i_%s.vtk" % (count, sp.base.get_name())
  103. mflags = ""
  104. if use_pdata:
  105. mflags += "p"
  106. if coorcorr:
  107. mflags += "c"
  108. # Export the raster map with r.out.vtk
  109. try:
  110. if elevation:
  111. grass.run_command("r.out.vtk", flags=mflags, null=null,
  112. input=map_name, elevation=elevation,
  113. output=out_name,
  114. overwrite=grass.overwrite())
  115. else:
  116. grass.run_command("r.out.vtk", flags=mflags, null=null,
  117. input=map_name, output=out_name,
  118. overwrite=grass.overwrite())
  119. except CalledModuleError:
  120. grass.fatal(_("Unable to export raster map <%s>" % map_name))
  121. count += 1
  122. if use_granularity:
  123. grass.run_command("g.remove", flags='f', type='raster', name=null_map)
  124. grass.run_command("g.remove", flags='f', type='raster', name=map_name)
  125. if __name__ == "__main__":
  126. options, flags = grass.parser()
  127. main()