t.rename.py 2.9 KB

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