gui_support.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. ###############################################################################
  12. def tlist_grouped(type, group_type = False):
  13. """!List of temporal elements grouped by mapsets.
  14. Returns a dictionary where the keys are mapset
  15. names and the values are lists of space time datasets in that
  16. mapset. Example:
  17. @code
  18. >>> tgis.tlist_grouped('strds')['PERMANENT']
  19. ['precipitation', 'temperature']
  20. @endcode
  21. @param type element type (strds, str3ds, stvds)
  22. @param group_type TBD
  23. @return directory of mapsets/elements
  24. """
  25. result = {}
  26. mapset = None
  27. if type == 'stds':
  28. types = ['strds', 'str3ds', 'stvds']
  29. else:
  30. types = [type]
  31. for type in types:
  32. try:
  33. tlist_result = tlist(type)
  34. except core.ScriptError, e:
  35. warning(e)
  36. continue
  37. for line in tlist_result:
  38. try:
  39. name, mapset = line.split('@')
  40. except ValueError:
  41. warning(_("Invalid element '%s'") % line)
  42. continue
  43. if mapset not in result:
  44. if group_type:
  45. result[mapset] = {}
  46. else:
  47. result[mapset] = []
  48. if group_type:
  49. if type in result[mapset]:
  50. result[mapset][type].append(name)
  51. else:
  52. result[mapset][type] = [name, ]
  53. else:
  54. result[mapset].append(name)
  55. return result
  56. ###############################################################################
  57. def tlist(type):
  58. """!Return a list of space time datasets of absolute and relative time
  59. @param type element type (strds, str3ds, stvds)
  60. @return a list of space time dataset ids
  61. """
  62. id = None
  63. sp = dataset_factory(type, id)
  64. dbif = SQLDatabaseInterfaceConnection()
  65. dbif.connect()
  66. output = []
  67. temporal_type = ["absolute", 'relative']
  68. for type in temporal_type:
  69. # Table name
  70. if type == "absolute":
  71. table = sp.get_type() + "_view_abs_time"
  72. else:
  73. table = sp.get_type() + "_view_rel_time"
  74. # Create the sql selection statement
  75. sql = "SELECT id FROM " + table
  76. sql += " ORDER BY id"
  77. dbif.cursor.execute(sql)
  78. rows = dbif.cursor.fetchall()
  79. # Append the ids of the space time datasets
  80. for row in rows:
  81. for col in row:
  82. output.append(str(col))
  83. dbif.close()
  84. return output