t.remove.py 2.8 KB

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