gui_support.py 3.3 KB

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