t.remove.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.remove
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Remove space time datasets from the temporal database
  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: Removes space time datasets from temporal database.
  18. #% keywords: temporal
  19. #% keywords: remove
  20. #%end
  21. #%option G_OPT_STDS_INPUT
  22. #% required: no
  23. #%end
  24. #%option
  25. #% key: file
  26. #% type: string
  27. #% description: Input file with dataset names, one per line
  28. #% required: no
  29. #% multiple: no
  30. #%end
  31. #%option
  32. #% key: type
  33. #% type: string
  34. #% description: Type of the space time dataset, default is strds
  35. #% required: no
  36. #% options: strds, str3ds, stvds
  37. #% answer: strds
  38. #%end
  39. import grass.script as grass
  40. import grass.temporal as tgis
  41. ############################################################################
  42. def main():
  43. # Get the options
  44. datasets = options["input"]
  45. file = options["file"]
  46. type = options["type"]
  47. if datasets and file:
  48. core.fata(_("%s= and %s= are mutually exclusive") % ("input","file"))
  49. # Make sure the temporal database exists
  50. tgis.create_temporal_database()
  51. dbif = tgis.sql_database_interface_connection()
  52. dbif.connect()
  53. dataset_list = []
  54. # Dataset names as comma separated string
  55. if datasets:
  56. if datasets.find(",") == -1:
  57. dataset_list = (datasets,)
  58. else:
  59. dataset_list = tuple(datasets.split(","))
  60. # Read the dataset list from file
  61. if file:
  62. fd = open(file, "r")
  63. line = True
  64. while True:
  65. line = fd.readline()
  66. if not line:
  67. break
  68. line_list = line.split("\n")
  69. dataset_name = line_list[0]
  70. dataset_list.append(dataset_name)
  71. mapset = grass.gisenv()["MAPSET"]
  72. statement = ""
  73. for name in dataset_list:
  74. name = name.strip()
  75. # Check for the mapset in name
  76. if name.find("@") < 0:
  77. id = name + "@" + mapset
  78. else:
  79. id = name
  80. sp = tgis.dataset_factory(type, id)
  81. if sp.is_in_db(dbif) == False:
  82. dbif.close()
  83. grass.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  84. statement += sp.delete(dbif=dbif, execute=False)
  85. # Execute the collected SQL statenents
  86. dbif.execute_transaction(statement)
  87. dbif.close()
  88. if __name__ == "__main__":
  89. options, flags = grass.parser()
  90. main()