t.rename.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.rename
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Renames a space time dataset
  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: Renames a space time dataset
  24. # % keyword: temporal
  25. # % keyword: map management
  26. # % keyword: rename
  27. # % keyword: time
  28. # %end
  29. # %option G_OPT_STDS_INPUT
  30. # %end
  31. # %option G_OPT_STDS_OUTPUT
  32. # %end
  33. # %option G_OPT_STDS_TYPE
  34. # % guidependency: input
  35. # % guisection: Required
  36. # %end
  37. import grass.script as grass
  38. ############################################################################
  39. def main():
  40. # lazy imports
  41. import grass.temporal as tgis
  42. # Get the options
  43. input = options["input"]
  44. output = options["output"]
  45. type = options["type"]
  46. # Make sure the temporal database exists
  47. tgis.init()
  48. # Get the current mapset to create the id of the space time dataset
  49. mapset = grass.gisenv()["MAPSET"]
  50. if input.find("@") >= 0:
  51. old_id = input
  52. else:
  53. old_id = input + "@" + mapset
  54. if output.find("@") >= 0:
  55. new_id = output
  56. else:
  57. new_id = output + "@" + mapset
  58. # Do not overwrite yourself
  59. if new_id == old_id:
  60. return
  61. dbif = tgis.SQLDatabaseInterfaceConnection()
  62. dbif.connect()
  63. stds = tgis.dataset_factory(type, old_id)
  64. if new_id.split("@")[1] != mapset:
  65. grass.fatal(
  66. _(
  67. "Space time %s dataset <%s> can not be renamed. "
  68. "Mapset of the new identifier differs from the current "
  69. "mapset."
  70. )
  71. % (stds.get_new_map_instance(None).get_type(), old_id)
  72. )
  73. if not stds.is_in_db(dbif=dbif):
  74. dbif.close()
  75. grass.fatal(
  76. _("Space time %s dataset <%s> not found")
  77. % (stds.get_new_map_instance(None).get_type(), old_id)
  78. )
  79. # Check if the new id is in the database
  80. new_stds = tgis.dataset_factory(type, new_id)
  81. if new_stds.is_in_db(dbif=dbif) and not grass.overwrite():
  82. dbif.close()
  83. grass.fatal(
  84. _(
  85. "Unable to rename Space time %s dataset <%s>. Name <%s> "
  86. "is in use, please use the overwrite flag."
  87. )
  88. % (stds.get_new_map_instance(None).get_type(), old_id, new_id)
  89. )
  90. # Remove an already existing space time dataset
  91. if new_stds.is_in_db(dbif=dbif):
  92. new_stds.delete(dbif=dbif)
  93. stds.select(dbif=dbif)
  94. stds.rename(ident=new_id, dbif=dbif)
  95. stds.update_command_string(dbif=dbif)
  96. if __name__ == "__main__":
  97. options, flags = grass.parser()
  98. main()