t.rename.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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(_("Space time %s dataset <%s> can not be renamed. "
  66. "Mapset of the new identifier differs from the current "
  67. "mapset.") % (stds.get_new_map_instance(None).get_type(),
  68. old_id))
  69. if not stds.is_in_db(dbif=dbif):
  70. dbif.close()
  71. grass.fatal(_("Space time %s dataset <%s> not found") % (
  72. stds.get_new_map_instance(None).get_type(), old_id))
  73. # Check if the new id is in the database
  74. new_stds = tgis.dataset_factory(type, new_id)
  75. if new_stds.is_in_db(dbif=dbif) and not grass.overwrite():
  76. dbif.close()
  77. grass.fatal(_("Unable to rename Space time %s dataset <%s>. Name <%s> "
  78. "is in use, please use the overwrite flag.") % (
  79. stds.get_new_map_instance(None).get_type(), old_id, new_id))
  80. # Remove an already existing space time dataset
  81. if new_stds.is_in_db(dbif=dbif):
  82. new_stds.delete(dbif=dbif)
  83. stds.select(dbif=dbif)
  84. stds.rename(ident=new_id, dbif=dbif)
  85. stds.update_command_string(dbif=dbif)
  86. if __name__ == "__main__":
  87. options, flags = grass.parser()
  88. main()