t.rename.py 2.8 KB

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