t.merge.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.merge
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Merge several space time datasets into a single one
  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: Merges several space time datasets into a single space time dataset.
  18. #% keywords: temporal
  19. #% keywords: merge
  20. #%end
  21. #%option G_OPT_STDS_INPUTS
  22. #%end
  23. #%option G_OPT_STDS_OUTPUT
  24. #%end
  25. #%option G_OPT_STDS_TYPE
  26. #% guidependency: inputs
  27. #% guisection: Required
  28. #%end
  29. import grass.temporal as tgis
  30. import grass.script as grass
  31. ############################################################################
  32. grass.set_raise_on_error(True)
  33. def main():
  34. # Get the options
  35. inputs = options["inputs"]
  36. output = options["output"]
  37. type = options["type"]
  38. # Make sure the temporal database exists
  39. tgis.init()
  40. #Get the current mapset to create the id of the space time dataset
  41. mapset = grass.gisenv()["MAPSET"]
  42. inputs_split = inputs.split(",")
  43. input_ids = []
  44. for input in inputs_split:
  45. if input.find("@") >= 0:
  46. input_ids.append(input)
  47. else:
  48. input_ids.append(input + "@" + mapset)
  49. # Set the output name correct
  50. if output.find("@") >= 0:
  51. out_mapset = output.split("@")[1]
  52. if out_mapset != mapset:
  53. grass.fatal(_("Output space time dataset <%s> must be located in this mapset") % (output))
  54. else:
  55. output_id = output + "@" + mapset
  56. dbif = tgis.SQLDatabaseInterfaceConnection()
  57. dbif.connect()
  58. stds_list = []
  59. first = None
  60. for id in input_ids:
  61. stds = tgis.open_old_stds(id, type, dbif)
  62. if first is None:
  63. first = stds
  64. if first.get_temporal_type() != stds.get_temporal_type():
  65. dbif.close()
  66. grass.fatal(_("Space time datasets to merge must have the same temporal type"))
  67. stds_list.append(stds)
  68. # Do nothing if nothing to merge
  69. if first is None:
  70. dbif.close()
  71. return
  72. # Check if the new id is in the database
  73. output_stds = tgis.dataset_factory(type, output_id)
  74. output_exists = output_stds.is_in_db(dbif=dbif)
  75. if output_exists == True and grass.overwrite() == False:
  76. dbif.close()
  77. grass.fatal(_("Unable to merge maps into space time %s dataset <%s> "\
  78. "please use the overwrite flag.") % \
  79. (stds.get_new_map_instance(None).get_type(), output_id))
  80. if not output_exists:
  81. output_stds = tgis.open_new_stds(output, type,
  82. first.get_temporal_type(),
  83. "Merged space time dataset",
  84. "Merged space time dataset",
  85. "mean", dbif=dbif, overwrite=False)
  86. else:
  87. output_stds.select(dbif=dbif)
  88. registered_output_maps = {}
  89. # Maps that are already registered in an existing dataset
  90. # are not registered again
  91. if output_exists == True:
  92. rows = output_stds.get_registered_maps(columns="id", dbif=dbif)
  93. if rows:
  94. for row in rows:
  95. registered_output_maps[row["id"]] = row["id"]
  96. for stds in stds_list:
  97. # Avoid merging of already registered maps
  98. if stds.get_id() != output_stds.get_id():
  99. maps = stds.get_registered_maps_as_objects(dbif=dbif)
  100. if maps:
  101. for map in maps:
  102. # Jump over already registered maps
  103. if map.get_id() in registered_output_maps:
  104. continue
  105. map.select(dbif=dbif)
  106. output_stds.register_map(map=map, dbif=dbif)
  107. # Update the registered map list
  108. registered_output_maps[map.get_id()] = map.get_id()
  109. output_stds.update_from_registered_maps(dbif=dbif)
  110. if output_exists == True:
  111. output_stds.update_command_string(dbif=dbif)
  112. if __name__ == "__main__":
  113. options, flags = grass.parser()
  114. main()