t.support.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #% description: Update the metadata information and spatial extent of registered maps from the grass spatial database. Check for removed maps and delete them from the temporal database and all effected space time datasets.
  53. #%end
  54. #%flag
  55. #% key: u
  56. #% description: Update metadata information, temporal and spatial extent from registered maps
  57. #%end
  58. import grass.temporal as tgis
  59. import grass.script as grass
  60. ############################################################################
  61. def main():
  62. # Get the options
  63. name = options["input"]
  64. type = options["type"]
  65. title = options["title"]
  66. description = options["description"]
  67. semantic = options["semantictype"]
  68. update = flags["u"]
  69. map_update = flags["m"]
  70. # Make sure the temporal database exists
  71. tgis.init()
  72. dbif = tgis.SQLDatabaseInterfaceConnection()
  73. dbif.connect()
  74. stds = tgis.open_old_space_time_dataset(name, type, dbif)
  75. update = False
  76. if title:
  77. stds.metadata.set_title(title=title)
  78. update = True
  79. # Update only non-null entries
  80. if description:
  81. stds.metadata.set_description(description=description)
  82. update = True
  83. if semantic:
  84. stds.base.set_semantic_type(semantic_type=semantic)
  85. update = True
  86. if update:
  87. stds.update(dbif=dbif)
  88. if map_update:
  89. #Update the registered maps from the grass spatial database
  90. statement = ""
  91. # This dict stores the datasets that must be updated
  92. dataset_dict = {}
  93. count = 0
  94. maps = stds.get_registered_maps_as_objects(dbif=dbif)
  95. # We collect the delete and update statements
  96. for map in maps:
  97. count += 1
  98. if count%10 == 0:
  99. grass.percent(count, len(maps), 1)
  100. map.select(dbif=dbif)
  101. # Check if the map is present in the grass spatial database
  102. # Update if present, delete if not present
  103. if map.map_exists():
  104. # Read new metadata from the spatial database
  105. map.load()
  106. statement += map.update(dbif=dbif, execute=False)
  107. else:
  108. # Delete the map from the temporal database
  109. # We need to update all effected space time datasets
  110. datasets = map.get_registered_stds(dbif)
  111. if datasets:
  112. for dataset in datasets:
  113. dataset_dict[dataset] = dataset
  114. # Collect the delete statements
  115. statement += map.delete(dbif=dbif, update=False, execute=False)
  116. # Execute the collected SQL statements
  117. dbif.execute_transaction(statement)
  118. # Update the effected space time datasets
  119. for id in dataset_dict:
  120. stds_new = stds.get_new_instance(id)
  121. stds_new.select(dbif=dbif)
  122. stds_new.update_from_registered_maps(dbif=dbif)
  123. if map_update or update:
  124. stds.update_from_registered_maps(dbif=dbif)
  125. stds.update_command_string(dbif=dbif)
  126. dbif.close()
  127. if __name__ == "__main__":
  128. options, flags = grass.parser()
  129. main()