gui_support.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. """
  2. GUI support functions
  3. (C) 2008-2011 by the GRASS Development Team
  4. This program is free software under the GNU General Public
  5. License (>=v2). Read the file COPYING that comes with GRASS
  6. for details.
  7. :authors: Soeren Gebbert
  8. """
  9. from .core import get_available_temporal_mapsets, init_dbif
  10. from .factory import dataset_factory
  11. import grass.script as gscript
  12. ###############################################################################
  13. def tlist_grouped(type, group_type=False, dbif=None):
  14. """List of temporal elements grouped by mapsets.
  15. Returns a dictionary where the keys are mapset
  16. names and the values are lists of space time datasets in that
  17. mapset. Example:
  18. .. code-block:: python
  19. >>> import grass.temporalas tgis
  20. >>> tgis.tlist_grouped('strds')['PERMANENT']
  21. ['precipitation', 'temperature']
  22. :param type: element type (strds, str3ds, stvds)
  23. :param group_type: TBD
  24. :return: directory of mapsets/elements
  25. """
  26. result = {}
  27. dbif, connection_state_changed = init_dbif(dbif)
  28. mapset = None
  29. if type == "stds":
  30. types = ["strds", "str3ds", "stvds"]
  31. else:
  32. types = [type]
  33. for type in types:
  34. try:
  35. tlist_result = tlist(type=type, dbif=dbif)
  36. except gscript.ScriptError as e:
  37. gscript.warning(e)
  38. continue
  39. for line in tlist_result:
  40. try:
  41. name, mapset = line.split("@")
  42. except ValueError:
  43. gscript.warning(_("Invalid element '%s'") % line)
  44. continue
  45. if mapset not in result:
  46. if group_type:
  47. result[mapset] = {}
  48. else:
  49. result[mapset] = []
  50. if group_type:
  51. if type in result[mapset]:
  52. result[mapset][type].append(name)
  53. else:
  54. result[mapset][type] = [
  55. name,
  56. ]
  57. else:
  58. result[mapset].append(name)
  59. if connection_state_changed is True:
  60. dbif.close()
  61. return result
  62. ###############################################################################
  63. def tlist(type, dbif=None):
  64. """Return a list of space time datasets of absolute and relative time
  65. :param type: element type (strds, str3ds, stvds)
  66. :return: a list of space time dataset ids
  67. """
  68. id = None
  69. sp = dataset_factory(type, id)
  70. dbif, connection_state_changed = init_dbif(dbif)
  71. mapsets = get_available_temporal_mapsets()
  72. output = []
  73. temporal_type = ["absolute", "relative"]
  74. for type in temporal_type:
  75. # For each available mapset
  76. for mapset in mapsets.keys():
  77. # Table name
  78. if type == "absolute":
  79. table = sp.get_type() + "_view_abs_time"
  80. else:
  81. table = sp.get_type() + "_view_rel_time"
  82. # Create the sql selection statement
  83. sql = "SELECT id FROM " + table
  84. sql += " WHERE mapset = '%s'" % (mapset)
  85. sql += " ORDER BY id"
  86. dbif.execute(sql, mapset=mapset)
  87. rows = dbif.fetchall(mapset=mapset)
  88. # Append the ids of the space time datasets
  89. for row in rows:
  90. for col in row:
  91. output.append(str(col))
  92. if connection_state_changed is True:
  93. dbif.close()
  94. return output