t.rast.out.vtk.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 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: expdir
  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: -10.0
  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. ############################################################################
  61. def main():
  62. # Get the options
  63. input = options["input"]
  64. elevation = options["elevation"]
  65. expdir = options["expdir"]
  66. where = options["where"]
  67. null = options["null"]
  68. use_pdata = flags["p"]
  69. coorcorr = flags["c"]
  70. use_granularity = flags["g"]
  71. # Make sure the temporal database exists
  72. tgis.init()
  73. if not os.path.exists(expdir):
  74. grass.fatal(_("Export directory <%s> not found.") % expdir)
  75. os.chdir(expdir)
  76. mapset = grass.gisenv()["MAPSET"]
  77. if input.find("@") >= 0:
  78. id = input
  79. else:
  80. id = input + "@" + mapset
  81. sp = tgis.SpaceTimeRasterDataset(id)
  82. if sp.is_in_db() == False:
  83. grass.fatal(_("Space time %s dataset <%s> not found") % (
  84. sp.get_new_map_instance(None).get_type(), id))
  85. sp.select()
  86. if use_granularity:
  87. # Attention: A list of lists of maps will be returned
  88. maps = sp.get_registered_maps_as_objects_by_granularity()
  89. # Create a NULL map in case of granularity support
  90. null_map = "temporary_null_map_%i" % os.getpid()
  91. grass.mapcalc("%s = null()" % (null_map))
  92. else:
  93. maps = sp.get_registered_maps_as_objects(where, "start_time", None)
  94. # To have scalar values with the same name, we need to copy the
  95. # raster maps using a single name
  96. map_name = "%s_%i" % (sp.base.get_name(), os.getpid())
  97. count = 0
  98. if maps is not None:
  99. for map in maps:
  100. if use_granularity:
  101. if map and len(map) > 0:
  102. id = map[0].get_map_id()
  103. else:
  104. id = map.get_map_id()
  105. # None ids will be replaced by NULL maps
  106. if id is None:
  107. id = null_map
  108. grass.run_command("g.copy", rast="%s,%s" % (id, map_name),
  109. overwrite=True)
  110. out_name = "%6.6i_%s.vtk" % (count, sp.base.get_name())
  111. mflags = ""
  112. if use_pdata:
  113. mflags += "p"
  114. if coorcorr:
  115. mflags += "c"
  116. # Export the raster map with r.out.vtk
  117. if elevation:
  118. ret = grass.run_command("r.out.vtk", flags=mflags, null=null,
  119. input=map_name, elevation=elevation,
  120. output=out_name,
  121. overwrite=grass.overwrite())
  122. else:
  123. ret = grass.run_command("r.out.vtk", flags=mflags, null=null,
  124. input=map_name, output=out_name,
  125. overwrite=grass.overwrite())
  126. if ret != 0:
  127. grass.fatal(_("Unable to export raster map <%s>" % name))
  128. count += 1
  129. if use_granularity:
  130. grass.run_command("g.remove", rast=null_map)
  131. grass.run_command("g.remove", rast=map_name)
  132. if __name__ == "__main__":
  133. options, flags = grass.parser()
  134. main()