t.support.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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: support
  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. #%end
  34. #%option
  35. #% key: title
  36. #% type: string
  37. #% description: Title of the space time dataset
  38. #% required: no
  39. #% multiple: no
  40. #%end
  41. #%option
  42. #% key: description
  43. #% type: string
  44. #% description: Description of the space time dataset
  45. #% required: no
  46. #% multiple: no
  47. #%end
  48. #%flag
  49. #% key: m
  50. #% 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.
  51. #%end
  52. #%flag
  53. #% key: u
  54. #% description: Update metadata information, temporal and spatial extent from registered maps
  55. #%end
  56. import grass.temporal as tgis
  57. import grass.script as grass
  58. ############################################################################
  59. def main():
  60. # Get the options
  61. name = options["input"]
  62. type = options["type"]
  63. title = options["title"]
  64. description = options["description"]
  65. semantic = options["semantictype"]
  66. update = flags["u"]
  67. map_update = flags["m"]
  68. # Make sure the temporal database exists
  69. tgis.create_temporal_database()
  70. #Get the current mapset to create the id of the space time dataset
  71. mapset = grass.gisenv()["MAPSET"]
  72. if name.find("@") >= 0:
  73. id = name
  74. else:
  75. id = name + "@" + mapset
  76. dbif = tgis.sql_database_interface_connection()
  77. dbif.connect()
  78. stds = tgis.dataset_factory(type, id)
  79. if stds.is_in_db(dbif=dbif) == False:
  80. dbif.close()
  81. grass.fatal(_("Space time %s dataset <%s> not found") % (stds.get_new_map_instance(None).get_type(), id))
  82. stds.select(dbif=dbif)
  83. update = False
  84. if title:
  85. stds.metadata.set_title(title=title)
  86. update = True
  87. # Update only non-null entries
  88. if description:
  89. stds.metadata.set_description(description=description)
  90. update = True
  91. if semantic:
  92. stds.base.set_semantic_type(semantic_type=semantic)
  93. update = True
  94. if update:
  95. stds.update(dbif=dbif)
  96. if map_update:
  97. #Update the registered maps from the grass spatial database
  98. statement = ""
  99. # This dict stores the datasets that must be updated
  100. dataset_dict = {}
  101. count = 0
  102. maps = stds.get_registered_maps_as_objects(dbif=dbif)
  103. # We collect the delete and update statements
  104. for map in maps:
  105. grass.percent(count, len(maps), 1)
  106. count += 1
  107. map.select(dbif=dbif)
  108. # Check if the map is present in the grass spatial database
  109. # Update if present, delete if not present
  110. if map.map_exists():
  111. # Read new metadata from the spatial database
  112. map.load()
  113. statement += map.update(dbif=dbif, execute=False)
  114. else:
  115. # Delete the map from the temporal database
  116. # We need to update all effected space time datasets
  117. rows = map.get_registered_datasets(dbif)
  118. if rows:
  119. for row in rows:
  120. dataset_dict[row["id"]] = row["id"]
  121. # Collect the delete statements
  122. statement += map.delete(dbif=dbif, update=False, execute=False)
  123. # Execute the collected SQL statements
  124. dbif.execute_transaction(statement)
  125. # Update the effected space time datasets
  126. for id in dataset_dict:
  127. stds_new = stds.get_new_instance(id)
  128. stds_new.select(dbif=dbif)
  129. stds_new.update_from_registered_maps(dbif=dbif)
  130. if map_update or update:
  131. stds.update_from_registered_maps(dbif=dbif)
  132. dbif.close()
  133. if __name__ == "__main__":
  134. options, flags = grass.parser()
  135. main()