t.support.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.support
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Modify the metadata of 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: Modifies the metadata of a space time dataset.
  18. #% keywords: temporal
  19. #% keywords: metadata
  20. #%end
  21. #%option G_OPT_STDS_INPUT
  22. #%end
  23. #%option
  24. #% key: semantictype
  25. #% type: string
  26. #% description: Semantic type of the space time dataset
  27. #% required: no
  28. #% multiple: no
  29. #% options: min,max,sum,mean
  30. #% answer: mean
  31. #%end
  32. #%option G_OPT_STDS_TYPE
  33. #% guidependency: input
  34. #% guisection: Required
  35. #%end
  36. #%option
  37. #% key: title
  38. #% type: string
  39. #% description: Title of the space time dataset
  40. #% required: no
  41. #% multiple: no
  42. #%end
  43. #%option
  44. #% key: description
  45. #% type: string
  46. #% description: Description of the space time dataset
  47. #% required: no
  48. #% multiple: no
  49. #%end
  50. #%flag
  51. #% key: m
  52. #% label: Update the metadata information and spatial extent of registered maps from the GRASS spatial database
  53. #% description: Check for removed maps and delete them from the temporal database and all effected space time datasets
  54. #%end
  55. #%flag
  56. #% key: u
  57. #% description: Update metadata information, temporal and spatial extent from registered maps
  58. #%end
  59. import grass.temporal as tgis
  60. import grass.script as grass
  61. ############################################################################
  62. def main():
  63. # Get the options
  64. name = options["input"]
  65. type = options["type"]
  66. title = options["title"]
  67. description = options["description"]
  68. semantic = options["semantictype"]
  69. update = flags["u"]
  70. map_update = flags["m"]
  71. # Make sure the temporal database exists
  72. tgis.init()
  73. dbif = tgis.SQLDatabaseInterfaceConnection()
  74. dbif.connect()
  75. stds = tgis.open_old_stds(name, type, dbif)
  76. update = False
  77. if title:
  78. stds.metadata.set_title(title=title)
  79. update = True
  80. # Update only non-null entries
  81. if description:
  82. stds.metadata.set_description(description=description)
  83. update = True
  84. if semantic:
  85. stds.base.set_semantic_type(semantic_type=semantic)
  86. update = True
  87. if update:
  88. stds.update(dbif=dbif)
  89. if map_update:
  90. #Update the registered maps from the grass spatial database
  91. statement = ""
  92. # This dict stores the datasets that must be updated
  93. dataset_dict = {}
  94. count = 0
  95. maps = stds.get_registered_maps_as_objects(dbif=dbif)
  96. # We collect the delete and update statements
  97. for map in maps:
  98. count += 1
  99. if count%10 == 0:
  100. grass.percent(count, len(maps), 1)
  101. map.select(dbif=dbif)
  102. # Check if the map is present in the grass spatial database
  103. # Update if present, delete if not present
  104. if map.map_exists():
  105. # Read new metadata from the spatial database
  106. map.load()
  107. statement += map.update(dbif=dbif, execute=False)
  108. else:
  109. # Delete the map from the temporal database
  110. # We need to update all effected space time datasets
  111. datasets = map.get_registered_stds(dbif)
  112. if datasets:
  113. for dataset in datasets:
  114. dataset_dict[dataset] = dataset
  115. # Collect the delete statements
  116. statement += map.delete(dbif=dbif, update=False, execute=False)
  117. # Execute the collected SQL statements
  118. dbif.execute_transaction(statement)
  119. # Update the effected space time datasets
  120. for id in dataset_dict:
  121. stds_new = stds.get_new_instance(id)
  122. stds_new.select(dbif=dbif)
  123. stds_new.update_from_registered_maps(dbif=dbif)
  124. if map_update or update:
  125. stds.update_from_registered_maps(dbif=dbif)
  126. stds.update_command_string(dbif=dbif)
  127. dbif.close()
  128. if __name__ == "__main__":
  129. options, flags = grass.parser()
  130. main()