gui_support.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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, 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
  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. dbif, connected = 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 core.ScriptError as e:
  37. warning(e)
  38. continue
  39. for line in tlist_result:
  40. try:
  41. name, mapset = line.split('@')
  42. except ValueError:
  43. 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] = [name, ]
  55. else:
  56. result[mapset].append(name)
  57. if connected is True:
  58. dbif.close()
  59. return result
  60. ###############################################################################
  61. def tlist(type, dbif=None):
  62. """!Return a list of space time datasets of absolute and relative time
  63. @param type element type (strds, str3ds, stvds)
  64. @return a list of space time dataset ids
  65. """
  66. id = None
  67. sp = dataset_factory(type, id)
  68. dbif, connected = init_dbif(dbif)
  69. output = []
  70. temporal_type = ["absolute", 'relative']
  71. for type in temporal_type:
  72. # Table name
  73. if type == "absolute":
  74. table = sp.get_type() + "_view_abs_time"
  75. else:
  76. table = sp.get_type() + "_view_rel_time"
  77. # Create the sql selection statement
  78. sql = "SELECT id FROM " + table
  79. sql += " ORDER BY id"
  80. dbif.cursor.execute(sql)
  81. rows = dbif.cursor.fetchall()
  82. # Append the ids of the space time datasets
  83. for row in rows:
  84. for col in row:
  85. output.append(str(col))
  86. if connected is True:
  87. dbif.close()
  88. return output