t.merge.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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-2017 by the GRASS Development Team
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. #############################################################################
  22. #%module
  23. #% description: Merges several space time datasets into a single space time dataset.
  24. #% keyword: temporal
  25. #% keyword: time management
  26. #% keyword: merge
  27. #% keyword: time
  28. #%end
  29. #%option G_OPT_STDS_INPUTS
  30. #%end
  31. #%option G_OPT_STDS_OUTPUT
  32. #%end
  33. #%option G_OPT_STDS_TYPE
  34. #% guidependency: inputs
  35. #% guisection: Required
  36. #%end
  37. import grass.script as grass
  38. ############################################################################
  39. grass.set_raise_on_error(True)
  40. def main():
  41. # lazy imports
  42. import grass.temporal as tgis
  43. # Get the options
  44. inputs = options["inputs"]
  45. output = options["output"]
  46. type = options["type"]
  47. # Make sure the temporal database exists
  48. tgis.init()
  49. #Get the current mapset to create the id of the space time dataset
  50. mapset = grass.gisenv()["MAPSET"]
  51. inputs_split = inputs.split(",")
  52. input_ids = []
  53. for input in inputs_split:
  54. if input.find("@") >= 0:
  55. input_ids.append(input)
  56. else:
  57. input_ids.append(input + "@" + mapset)
  58. # Set the output name correct
  59. if output.find("@") >= 0:
  60. out_mapset = output.split("@")[1]
  61. if out_mapset != mapset:
  62. grass.fatal(_("Output space time dataset <%s> must be located in this mapset") % (output))
  63. else:
  64. output_id = output + "@" + mapset
  65. dbif = tgis.SQLDatabaseInterfaceConnection()
  66. dbif.connect()
  67. stds_list = []
  68. first = None
  69. for id in input_ids:
  70. stds = tgis.open_old_stds(id, type, dbif)
  71. if first is None:
  72. first = stds
  73. if first.get_temporal_type() != stds.get_temporal_type():
  74. dbif.close()
  75. grass.fatal(_("Space time datasets to merge must have the same temporal type"))
  76. stds_list.append(stds)
  77. # Do nothing if nothing to merge
  78. if first is None:
  79. dbif.close()
  80. return
  81. # Check if the new id is in the database
  82. output_stds = tgis.dataset_factory(type, output_id)
  83. output_exists = output_stds.is_in_db(dbif=dbif)
  84. if output_exists == True and grass.overwrite() == False:
  85. dbif.close()
  86. grass.fatal(_("Unable to merge maps into space time %s dataset <%s> "\
  87. "please use the overwrite flag.") % \
  88. (stds.get_new_map_instance(None).get_type(), output_id))
  89. if not output_exists:
  90. output_stds = tgis.open_new_stds(output, type,
  91. first.get_temporal_type(),
  92. "Merged space time dataset",
  93. "Merged space time dataset",
  94. "mean", dbif=dbif, overwrite=False)
  95. else:
  96. output_stds.select(dbif=dbif)
  97. registered_output_maps = {}
  98. # Maps that are already registered in an existing dataset
  99. # are not registered again
  100. if output_exists == True:
  101. rows = output_stds.get_registered_maps(columns="id", dbif=dbif)
  102. if rows:
  103. for row in rows:
  104. registered_output_maps[row["id"]] = row["id"]
  105. for stds in stds_list:
  106. # Avoid merging of already registered maps
  107. if stds.get_id() != output_stds.get_id():
  108. maps = stds.get_registered_maps_as_objects(dbif=dbif)
  109. if maps:
  110. for map in maps:
  111. # Jump over already registered maps
  112. if map.get_id() in registered_output_maps:
  113. continue
  114. map.select(dbif=dbif)
  115. output_stds.register_map(map=map, dbif=dbif)
  116. # Update the registered map list
  117. registered_output_maps[map.get_id()] = map.get_id()
  118. output_stds.update_from_registered_maps(dbif=dbif)
  119. if output_exists == True:
  120. output_stds.update_command_string(dbif=dbif)
  121. if __name__ == "__main__":
  122. options, flags = grass.parser()
  123. main()