gui_support.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. (C) 2008-2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. @author Soeren Gebbert
  9. """
  10. from space_time_datasets import *
  11. from factory import *
  12. ###############################################################################
  13. def tlist_grouped(type, group_type = False):
  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
  19. >>> tgis.tlist_grouped('strds')['PERMANENT']
  20. ['precipitation', 'temperature']
  21. @endcode
  22. @param type element type (strds, str3ds, stvds)
  23. @param group_type TBD
  24. @return directory of mapsets/elements
  25. """
  26. result = {}
  27. mapset = None
  28. if type == 'stds':
  29. types = ['strds', 'str3ds', 'stvds']
  30. else:
  31. types = [type]
  32. for type in types:
  33. try:
  34. tlist_result = tlist(type)
  35. except core.ScriptError, e:
  36. warning(e)
  37. continue
  38. for line in tlist_result:
  39. try:
  40. name, mapset = line.split('@')
  41. except ValueError:
  42. warning(_("Invalid element '%s'") % line)
  43. continue
  44. if mapset not in result:
  45. if group_type:
  46. result[mapset] = {}
  47. else:
  48. result[mapset] = []
  49. if group_type:
  50. if type in result[mapset]:
  51. result[mapset][type].append(name)
  52. else:
  53. result[mapset][type] = [name, ]
  54. else:
  55. result[mapset].append(name)
  56. return result
  57. ###############################################################################
  58. def tlist(type):
  59. """!Return a list of space time datasets of absolute and relative time
  60. @param type element type (strds, str3ds, stvds)
  61. @return a list of space time dataset ids
  62. """
  63. id = None
  64. sp = dataset_factory(type, id)
  65. dbif = SQLDatabaseInterfaceConnection()
  66. dbif.connect()
  67. output = []
  68. temporal_type = ["absolute", 'relative']
  69. for type in temporal_type:
  70. # Table name
  71. if type == "absolute":
  72. table = sp.get_type() + "_view_abs_time"
  73. else:
  74. table = sp.get_type() + "_view_rel_time"
  75. # Create the sql selection statement
  76. sql = "SELECT id FROM " + table
  77. sql += " ORDER BY id"
  78. dbif.cursor.execute(sql)
  79. rows = dbif.cursor.fetchall()
  80. # Append the ids of the space time datasets
  81. for row in rows:
  82. for col in row:
  83. output.append(str(col))
  84. dbif.close()
  85. return output