t.remove.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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_INPUTS
  23. #% guisection: Input
  24. #% required: no
  25. #%end
  26. #%option
  27. #% key: type
  28. #% type: string
  29. #% description: Type of the space time dataset, default is strds
  30. #% guidependency: inputs
  31. #% guisection: Input
  32. #% required: no
  33. #% options: strds, str3ds, stvds
  34. #% answer: strds
  35. #%end
  36. #%option G_OPT_F_INPUT
  37. #% key: file
  38. #% description: Input file with dataset names, one per line
  39. #% guisection: Input
  40. #% required: no
  41. #%end
  42. #%flag
  43. #% key: r
  44. #% description: Remove all registered maps from the temporal and spatial database
  45. #%end
  46. #%flag
  47. #% key: f
  48. #% description: Force recursive removing
  49. #%end
  50. import grass.script as grass
  51. import grass.temporal as tgis
  52. import grass.pygrass.modules as pyg
  53. ############################################################################
  54. def main():
  55. # Get the options
  56. datasets = options["inputs"]
  57. file = options["file"]
  58. type = options["type"]
  59. recursive = flags["r"]
  60. force = flags["f"]
  61. if recursive and not force:
  62. grass.fatal(_("The recursive flag works only in conjunction with the force flag: use -rf"))
  63. if datasets and file:
  64. grass.fatal(_("%s= and %s= are mutually exclusive") % ("input", "file"))
  65. # Make sure the temporal database exists
  66. tgis.init()
  67. dbif = tgis.SQLDatabaseInterfaceConnection()
  68. dbif.connect()
  69. dataset_list = []
  70. # Dataset names as comma separated string
  71. if datasets:
  72. if datasets.find(",") == -1:
  73. dataset_list = (datasets,)
  74. else:
  75. dataset_list = tuple(datasets.split(","))
  76. # Read the dataset list from file
  77. if file:
  78. fd = open(file, "r")
  79. line = True
  80. while True:
  81. line = fd.readline()
  82. if not line:
  83. break
  84. line_list = line.split("\n")
  85. dataset_name = line_list[0]
  86. dataset_list.append(dataset_name)
  87. statement = ""
  88. # Create the pygrass Module object for g.remove
  89. remove = pyg.Module("g.remove", quiet=True, run_=False)
  90. for name in dataset_list:
  91. name = name.strip()
  92. sp = tgis.open_old_space_time_dataset(name, type, dbif)
  93. if recursive and force:
  94. grass.message(_("Removing registered maps"))
  95. maps = sp.get_registered_maps_as_objects(dbif=dbif)
  96. map_statement = ""
  97. count = 1
  98. name_list = []
  99. for map in maps:
  100. map.select(dbif)
  101. # We may have multiple layer for a single map, hence we need
  102. # to avoid multiple deletation of the same map,
  103. # but the database entries are still present and must be removed
  104. if map.get_name() not in name_list:
  105. name_list.append(map.get_name())
  106. map_statement += map.delete(dbif=dbif, execute=False)
  107. count += 1
  108. # Delete every 100 maps
  109. if count%100 == 0:
  110. dbif.execute_transaction(map_statement)
  111. remove(rast=name_list, run_=True)
  112. map_statement = ""
  113. name_list = []
  114. if map_statement:
  115. dbif.execute_transaction(map_statement)
  116. if name_list:
  117. if type == "strds":
  118. remove(rast=name_list, run_=True)
  119. if type == "stvds":
  120. remove(vect=name_list, run_=True)
  121. if type == "str3ds":
  122. remove(rast3d=name_list, run_=True)
  123. statement += sp.delete(dbif=dbif, execute=False)
  124. # Execute the collected SQL statenents
  125. dbif.execute_transaction(statement)
  126. dbif.close()
  127. if __name__ == "__main__":
  128. options, flags = grass.parser()
  129. main()