t.rename.py 2.9 KB

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