t.remove.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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-2017 by the GRASS Development Team
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. #############################################################################
  22. #%module
  23. #% description: Removes space time datasets from temporal database.
  24. #% keyword: temporal
  25. #% keyword: map management
  26. #% keyword: remove
  27. #% keyword: time
  28. #%end
  29. #%option G_OPT_STDS_INPUTS
  30. #% guisection: Input
  31. #% required: no
  32. #%end
  33. #%option
  34. #% key: type
  35. #% type: string
  36. #% description: Type of the space time dataset, default is strds
  37. #% guidependency: inputs
  38. #% guisection: Input
  39. #% required: no
  40. #% options: strds, str3ds, stvds
  41. #% answer: strds
  42. #%end
  43. #%option G_OPT_F_INPUT
  44. #% key: file
  45. #% description: Input file with dataset names, one per line
  46. #% guisection: Input
  47. #% required: no
  48. #%end
  49. #%flag
  50. #% key: r
  51. #% description: Remove all registered maps from the temporal and also from the spatial database
  52. #%end
  53. #%flag
  54. #% key: f
  55. #% description: Force recursive removing
  56. #%end
  57. import grass.script as grass
  58. # lazy imports at the end of the file
  59. ############################################################################
  60. def main():
  61. # Get the options
  62. datasets = options["inputs"]
  63. file = options["file"]
  64. type = options["type"]
  65. recursive = flags["r"]
  66. force = flags["f"]
  67. if recursive and not force:
  68. grass.fatal(_("The recursive flag works only in conjunction with the force flag: use -rf"))
  69. if datasets and file:
  70. grass.fatal(_("%s= and %s= are mutually exclusive") % ("input", "file"))
  71. # Make sure the temporal database exists
  72. tgis.init()
  73. dbif = tgis.SQLDatabaseInterfaceConnection()
  74. dbif.connect()
  75. dataset_list = []
  76. # Dataset names as comma separated string
  77. if datasets:
  78. if datasets.find(",") == -1:
  79. dataset_list = (datasets,)
  80. else:
  81. dataset_list = tuple(datasets.split(","))
  82. # Read the dataset list from file
  83. if file:
  84. fd = open(file, "r")
  85. line = True
  86. while True:
  87. line = fd.readline()
  88. if not line:
  89. break
  90. line_list = line.split("\n")
  91. dataset_name = line_list[0]
  92. dataset_list.append(dataset_name)
  93. statement = ""
  94. # Create the pygrass Module object for g.remove
  95. remove = pyg.Module("g.remove", quiet=True, flags='f', run_=False)
  96. for name in dataset_list:
  97. name = name.strip()
  98. sp = tgis.open_old_stds(name, type, dbif)
  99. if recursive and force:
  100. grass.message(_("Removing registered maps and %s" % type))
  101. maps = sp.get_registered_maps_as_objects(dbif=dbif)
  102. map_statement = ""
  103. count = 1
  104. name_list = []
  105. for map in maps:
  106. map.select(dbif)
  107. # We may have multiple layer for a single map, hence we need
  108. # to avoid multiple deletation of the same map,
  109. # but the database entries are still present and must be removed
  110. if map.get_name() not in name_list:
  111. name_list.append(str(map.get_name()))
  112. map_statement += map.delete(dbif=dbif, execute=False)
  113. count += 1
  114. # Delete every 100 maps
  115. if count%100 == 0:
  116. dbif.execute_transaction(map_statement)
  117. if type == "strds":
  118. remove(type="raster", name=name_list, run_=True)
  119. if type == "stvds":
  120. remove(type="vector", name=name_list, run_=True)
  121. if type == "str3ds":
  122. remove(type="raster_3d", name=name_list, run_=True)
  123. map_statement = ""
  124. name_list = []
  125. if map_statement:
  126. dbif.execute_transaction(map_statement)
  127. if name_list:
  128. if type == "strds":
  129. remove(type="raster", name=name_list, run_=True)
  130. if type == "stvds":
  131. remove(type="vector", name=name_list, run_=True)
  132. if type == "str3ds":
  133. remove(type="raster_3d", name=name_list, run_=True)
  134. else:
  135. grass.message(_("Note: registered maps themselves have not been removed, only the %s" % type))
  136. statement += sp.delete(dbif=dbif, execute=False)
  137. # Execute the collected SQL statenents
  138. dbif.execute_transaction(statement)
  139. dbif.close()
  140. if __name__ == "__main__":
  141. options, flags = grass.parser()
  142. # lazy imports
  143. import grass.temporal as tgis
  144. import grass.pygrass.modules as pyg
  145. tgis.profile_function(main)