Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 102 additions & 27 deletions python/grass/temporal/list_stds.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,23 @@ def get_dataset_list(
This method returns a dictionary, the keys are the available mapsets,
the values are the rows from the SQL database query.

:param type: The type of the datasets (strds, str3ds, stvds, raster,
raster_3d, vector)
:param type: The dataset type(s) (strds, str3ds, stvds, raster,
raster_3d, vector) as a string with a single dataset type
(e.g. "strds") or a list of strings with dataset types (e.g.
["strds", "stvds"])
:param temporal_type: The temporal type of the datasets (absolute,
relative)
relative) as a string or a list of strings
:param columns: A comma separated list of columns that will be selected
:param where: A where statement for selected listing without "WHERE"
:param order: A comma separated list of columns to order the
datasets by category
:param dbif: The database interface to be used

:return: A dictionary with the rows of the SQL query for each
available mapset
:return: A dictionary with mapsets as keys. When *type* is a string,
values are raw database row objects (preserving backward
compatibility). When *type* is a list, values are plain
dicts with an additional ``"type"`` key identifying the
dataset type of each record.

.. code-block:: pycon

Expand Down Expand Up @@ -97,35 +102,105 @@ def get_dataset_list(
>>> check = sp.delete()

"""
msgr = get_tgis_message_interface()

dbif, connection_state_changed = init_dbif(dbif)

is_list_input = isinstance(type, list)
stds_type = [type] if isinstance(type, str) else type
if isinstance(temporal_type, str):
temporal_type = [temporal_type]

result = {}

for mapset in dbif.tgis_mapsets:
if temporal_type == "absolute":
table = type + "_view_abs_time"
else:
table = type + "_view_rel_time"
for ttype in temporal_type:
mapset_for_schema = (
list(dbif.tgis_mapsets.keys())[0] if dbif.tgis_mapsets else None
)
if not mapset_for_schema:
continue

type_schemas = []
for dtype in stds_type:
table = (
dtype + "_view_abs_time"
if ttype == "absolute"
else dtype + "_view_rel_time"
)
dbif.execute(f"SELECT * FROM {table} WHERE 0=1", mapset=mapset_for_schema)
type_schemas.append(
[d[0] for d in dbif.connections[mapset_for_schema].cursor.description]
)

if columns and columns.find("all") == -1:
sql = "SELECT " + str(columns) + " FROM " + table
else:
sql = "SELECT * FROM " + table
common_columns = [
col
for col in type_schemas[0]
if all(col in schema for schema in type_schemas[1:])
]
valid_columns_set = set(common_columns)

if where:
sql += " WHERE " + where
sql += " AND mapset = '%s'" % (mapset)
if columns and columns.find("all") == -1:
requested_columns = [
col.strip() for col in columns.split(",") if col != "type"
]
for col in requested_columns:
if col not in valid_columns_set:
if connection_state_changed:
dbif.close()
if len(stds_type) == 1:
msgr.fatal(
_(
"Column '%s' is not available for the requested dataset type"
)
% col
)
else:
msgr.fatal(
_(
"Column '%s' is not available for the requested combination of dataset types"
)
% col
)
final_columns = requested_columns
else:
sql += " WHERE mapset = '%s'" % (mapset)

if order:
sql += " ORDER BY " + order

dbif.execute(sql, mapset=mapset)
rows = dbif.fetchall(mapset=mapset)

if rows:
result[mapset] = rows
final_columns = common_columns

if not final_columns:
if connection_state_changed:
dbif.close()
msgr.fatal(_("No valid database columns were requested"))

columns_to_query = ",".join(final_columns)

for dtype in stds_type:
for mapset in dbif.tgis_mapsets:
if ttype == "absolute":
table = dtype + "_view_abs_time"
else:
table = dtype + "_view_rel_time"

sql = f"SELECT {columns_to_query} FROM {table}"

if where:
sql += " WHERE " + where
sql += " AND mapset = '%s'" % (mapset)
else:
sql += " WHERE mapset = '%s'" % (mapset)

if order:
sql += " ORDER BY " + order

dbif.execute(sql, mapset=mapset)
rows = dbif.fetchall(mapset=mapset)

if rows:
if mapset not in result:
result[mapset] = []
if is_list_input:
for row in rows:
result[mapset].append({**dict(row), "type": dtype})
else:
result[mapset].extend(rows)

if connection_state_changed:
dbif.close()
Expand Down
45 changes: 43 additions & 2 deletions temporal/t.list/t.list.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

*t.list* lists any dataset that is registered in the temporal database.
Datasets are raster, 3D raster and vector maps as well as their
corresponding space time datasets (STRDS, STR3DS and STVDS). The type of
the dataset can be specified using the *type* option, default is STRDS.
corresponding space time datasets (STRDS, STR3DS and STVDS). The type of the
dataset can be specified using the *type* option (default is STRDS).
Multiple comma-separated types can be provided, such as *type=strds,stvds*.
By default all datasets with relative and absolute time are listed.
However, the user has the ability to specify a single temporal type with
the *temporaltype* option. The user can define the columns that should
Expand Down Expand Up @@ -149,7 +150,47 @@ t.list type=raster format=json columns=id,start_time
"start_time": "2012-12-01 00:00:00"
}
]
```

To list multiple dataset types at once, specify a comma-separated list of
types (e.g., `type=strds,stvds`).

**Note:** The `type` column is automatically included in the **csv** and
**json** output formats when multiple types are requested to clearly identify
the dataset type of each record. It can also be explicitly requested for
single types using `columns=id,type`.

```sh
t.list type=strds,stvds columns=id format=json
```

```json
[
{
"id": "lst_daily@PERMANENT",
"type": "strds"
},
{
"id": "mini_set@PERMANENT",
"type": "strds"
},
{
"id": "nc_lst_daily@PERMANENT",
"type": "strds"
},
{
"id": "precip_abs1@PERMANENT",
"type": "strds"
},
{
"id": "prec_observer@PERMANENT",
"type": "stvds"
},
{
"id": "schools_stds@PERMANENT",
"type": "stvds"
}
]
```

## SEE ALSO
Expand Down
Loading
Loading