t.rename.py 2.9 KB

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