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