t.support.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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: Modify 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: The 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
  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. descr = 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. sp = tgis.dataset_factory(type, id)
  79. if sp.is_in_db(dbif=dbif) == False:
  80. dbif.close()
  81. grass.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  82. sp.select(dbif=dbif)
  83. # Temporal type can not be changed
  84. ttype= sp.get_temporal_type()
  85. sp.set_initial_values(temporal_type=ttype, semantic_type=semantic, title=title, description=descr)
  86. # Update only non-null entries
  87. sp.update(dbif=dbif)
  88. if map_update:
  89. #Update the registered maps from the grass spatial database
  90. statement = ""
  91. count = 0
  92. maps = sp.get_registered_maps_as_objects(dbif=dbif)
  93. for map in maps:
  94. map.select(dbif=dbif)
  95. map.load()
  96. statement += map.update(dbif=dbif, execute=False)
  97. grass.percent(count, len(maps), 1)
  98. count += 1
  99. # Execute the collected SQL statenents
  100. dbif.execute_transaction(statement)
  101. if map_update or update:
  102. sp.update_from_registered_maps(dbif=dbif)
  103. dbif.close()
  104. if __name__ == "__main__":
  105. options, flags = grass.parser()
  106. main()