Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

t.rast.univar: Add region_relation option for spatial filtering STDS by computational region #2793

Merged
merged 29 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1cba081
add spatial extent filter
ninsbl Jan 28, 2023
99a4760
pass only spatial relation
ninsbl Jan 28, 2023
2e30546
add spatial extent filter
ninsbl Jan 28, 2023
fafa82c
fix 3D where
ninsbl Jan 28, 2023
c081811
add tests for spatial filter
ninsbl Jan 28, 2023
9159eac
add more tests for spatial filter
ninsbl Jan 28, 2023
59f2fbe
fix failing test
ninsbl Jan 29, 2023
cd4ce6c
add spatial relation doc
ninsbl Feb 12, 2023
7945b9a
lower message severity
ninsbl Feb 12, 2023
be9f222
spatial_relation intro
ninsbl Feb 12, 2023
643bc4d
Update temporal/t.rast.univar/t.rast.univar.html
ninsbl Feb 13, 2023
550947c
Update temporal/temporalintro.html
ninsbl Feb 13, 2023
cf49c4e
rewrite user message
ninsbl Feb 13, 2023
37fa7ff
fix tests for empty strds
ninsbl Feb 13, 2023
ecfd1df
rename spatial filter option
ninsbl Feb 13, 2023
0b3e364
rename spatial filter option
ninsbl Feb 13, 2023
e39268e
update r-flag description
ninsbl Feb 13, 2023
2e12174
rename spatial filter option
ninsbl Feb 13, 2023
1fc915f
rename spatial filter option
ninsbl Feb 13, 2023
0896d00
improve spatial filter docs
ninsbl Feb 13, 2023
0d7afa6
fix failing test
ninsbl Feb 14, 2023
9f7735a
Update python/grass/temporal/abstract_space_time_dataset.py
ninsbl Feb 21, 2023
f8496ec
Update python/grass/temporal/univar_statistics.py
ninsbl Feb 21, 2023
593da28
address code review
ninsbl Feb 21, 2023
403072a
address code review
ninsbl Feb 21, 2023
52eab35
address code review, some linting
ninsbl Feb 21, 2023
37a1418
Merge branch 'main' into t_rast_univar_sr
ninsbl Sep 28, 2023
493697d
add back percentiles
ninsbl Sep 29, 2023
3f52fca
black
ninsbl Sep 29, 2023
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
78 changes: 53 additions & 25 deletions python/grass/temporal/abstract_space_time_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,15 +1369,22 @@ def get_registered_maps_as_objects_with_gaps(
In case more map information are needed, use the select()
method for each listed object.

The combination of the spatial_extent and spatial_relation parameters
can be used to return only map objects with the given spatial relation
to the provided spatial extent
ninsbl marked this conversation as resolved.
Show resolved Hide resolved

:param where: The SQL where statement to select a
subset of the registered maps without "WHERE"
:param dbif: The database interface to be used
:param spatial_extent: Return only maps with the provided spatial
relation to the given spatial extent (requires
spatial_relation parameter)
:param spatial_relation: Return only maps with the given spatial
relation to the provided spatial extent (requires
spatial_extent parameter)
:param spatial_extent: Spatial extent dict and projection information
e.g. from g.region -ug3 with GRASS GIS region keys
"n", "s", "e", "w", "b", "t", and "projection".
:param spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent

:return: ordered object list, in case nothing found None is returned
"""
Expand Down Expand Up @@ -1438,17 +1445,24 @@ def get_registered_maps_as_objects_with_temporal_topology(
In case more map information are needed, use the select()
method for each listed object.

The combination of the spatial_extent and spatial_relation parameters
can be used to return only maps with the given spatial relation to
the provided spatial extent

:param where: The SQL where statement to select a subset of
the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:param spatial_extent: Return only maps with the provided spatial
relation to the given spatial extent (requires
spatial_relation parameter)
:param spatial_relation: Return only maps with the given spatial
relation to the provided spatial extent (requires
spatial_extent parameter)
:param spatial_extent: Spatial extent dict and projection information
e.g. from g.region -ug3 with GRASS GIS region keys
"n", "s", "e", "w", "b", "t", and "projection".
:param spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent

:return: The ordered map object list,
In case nothing found None is returned
Expand Down Expand Up @@ -1485,17 +1499,24 @@ def get_registered_maps_as_objects(
In case more map information are needed, use the select()
method for each listed object.

The combination of the spatial_extent and spatial_relation parameters
can be used to return only maps with the given spatial relation to
the provided spatial extent

:param where: The SQL where statement to select a subset of
the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:param spatial_extent: Return only maps with the provided spatial
relation to the given spatial extent (requires
spatial_relation parameter)
:param spatial_relation: Return only maps with the given spatial
relation to the provided spatial extent (requires
spatial_extent parameter)
:param spatial_extent: Spatial extent dict and projection information
e.g. from g.region -ug3 with GRASS GIS region keys
"n", "s", "e", "w", "b", "t", and "projection".
:param spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent

:return: The ordered map object list,
In case nothing found None is returned
Expand Down Expand Up @@ -1659,7 +1680,7 @@ def _update_where_statement_by_spatial_extent(
:param str where: SQL WHERE statement to be updated
:param dict spatial_extent: Spatial extent dict and projection information
e.g. from g.region -ug3
:param dict spatial_relation: Spatial relation to the provided
:param str spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
Expand Down Expand Up @@ -1759,18 +1780,25 @@ def get_registered_maps(
In case columns are not specified, each row includes all columns
specified in the datatype specific view.

The combination of the spatial_extent and spatial_relation parameters
can be used to return only SQL rows of maps with the given spatial
relation to the provided spatial extent

:param columns: Columns to be selected as SQL compliant string
:param where: The SQL where statement to select a subset
of the registered maps without "WHERE"
:param order: The SQL order statement to be used to order the
objects in the list without "ORDER BY"
:param dbif: The database interface to be used
:param spatial_extent: Return only maps with the provided spatial
relation to the given spatial extent (requires
spatial_relation parameter)
:param spatial_relation: Return only maps with the given spatial
relation to the provided spatial extent (requires
spatial_extent parameter)
:param spatial_extent: Spatial extent dict and projection information
e.g. from g.region -ug3 with GRASS GIS region keys
"n", "s", "e", "w", "b", "t", and "projection".
:param spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent

:return: SQL rows of all registered maps,
In case nothing found None is returned
Expand Down
45 changes: 29 additions & 16 deletions python/grass/temporal/univar_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ def print_gridded_dataset_univar_statistics(
no_header=False,
fs="|",
rast_region=False,
spatial_relation=None,
region_relation=None,
zones=None,
nprocs=1,
):
"""Print univariate statistics for a space time raster or raster3d dataset
Returns None if the space time raster dataset is empty or if applied
filters (where, region_relation) do not return and maps to process
ninsbl marked this conversation as resolved.
Show resolved Hide resolved

:param type: Type of Space-Time-Dataset, must be either strds or str3ds
:param input: The name of the space time dataset
Expand All @@ -127,12 +129,12 @@ def print_gridded_dataset_univar_statistics(
:param rast_region: If set True ignore the current region settings
and use the raster map regions for univar statistical calculation.
Only available for strds.
:param dict spatial_relation: Spatial relation to the provided
spatial extent as a string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent
:param region_relation: Process only maps with the given spatial relation
to the computational region. A string with one of the following values:
"overlaps": maps that spatially overlap ("intersect")
within the provided spatial extent
"is_contained": maps that are fully within the provided spatial extent
"contains": maps that contain (fully cover) the provided spatial extent
:param zones: raster map with zones to calculate statistics for
"""
# We need a database interface
Expand All @@ -145,7 +147,7 @@ def print_gridded_dataset_univar_statistics(
out_file = open(output, "w")

spatial_extent = None
if spatial_relation:
if region_relation:
spatial_extent = gs.parse_command("g.region", flags="3gu")

strds_cols = (
Expand All @@ -159,19 +161,30 @@ def print_gridded_dataset_univar_statistics(
"start_time",
dbif,
spatial_extent=spatial_extent,
spatial_relation=spatial_relation,
spatial_relation=region_relation,
)

if not rows and rows != [""]:
dbif.close()
warn = "Space time %(sp)s dataset <%(i)s> is empty"
if spatial_relation:
warn += " or no maps with the requested spatial_relation to the computational region exist"
if where:
warn += " or where condition does not return any maps"
gs.warning(
_(warn) % {"sp": sp.get_new_map_instance(None).get_type(), "i": sp.get_id()}
gs.verbose(
_(
"Space time {type} dataset <{id}> is empty".format(
type=sp.get_new_map_instance(None).get_type(), id=sp.get_id()
)
)
)
if region_relation:
gs.verbose(
_(
"or no maps with the requested spatial relation"
"to the computational region exist"
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved
)
)
if where:
gs.verbose(_("or where condition does not return any maps"))

if output is not None:
out_file.close()
return
wenzeslaus marked this conversation as resolved.
Show resolved Hide resolved

if no_header is False:
Expand Down
12 changes: 4 additions & 8 deletions temporal/t.rast.univar/t.rast.univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
# %end

# %option
# % key: spatial_relation
# % key: region_relation
# % description: Process only maps with this spatial relation to the current computational region
# % guisection: Selection
# % options: overlaps,contains,is_contained
Expand All @@ -71,7 +71,7 @@

# %flag
# % key: r
# % description: Ignore the current region settings and use the raster map regions for univar statistical calculation
# % description: Use the raster map regions for univar statistical calculation instead of the current region
# %end

# %flag
Expand All @@ -80,10 +80,6 @@
# % guisection: Formatting
# %end

# %rules
# % exclusive: -r,spatial_relation
# %end

import grass.script as gs

############################################################################
Expand All @@ -102,7 +98,7 @@ def main():
output = options["output"]
nprocs = int(options["nprocs"])
where = options["where"]
spatial_relation = options["spatial_relation"]
region_relation = options["region_relation"]
extended = flags["e"]
no_header = flags["u"]
rast_region = bool(flags["r"])
Expand Down Expand Up @@ -131,7 +127,7 @@ def main():
fs=separator,
rast_region=rast_region,
zones=zones,
spatial_relation=spatial_relation,
region_relation=region_relation,
nprocs=nprocs,
)

Expand Down
21 changes: 4 additions & 17 deletions temporal/t.rast.univar/testsuite/test_t_rast_univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ def test_subset_with_output_coarse_resolution(self):

def test_error_handling_empty_strds(self):
# Empty strds
self.assertModuleFail(
self.assertModule(
"t.rast.univar",
input="A",
output="univar_output.txt",
Expand Down Expand Up @@ -395,7 +395,7 @@ def test_with_spatial_filter_intersects(self):
input="C",
where="start_time >= '2001-01-01'",
nprocs=2,
spatial_relation="overlaps",
region_relation="overlaps",
overwrite=True,
verbose=True,
)
Expand Down Expand Up @@ -423,7 +423,7 @@ def test_with_spatial_filter_contains(self):
input="C",
where="start_time >= '2001-01-01'",
nprocs=2,
spatial_relation="contains",
region_relation="contains",
overwrite=True,
verbose=True,
)
Expand All @@ -449,7 +449,7 @@ def test_with_spatial_filter_is_contained(self):
input="C",
where="start_time >= '2001-01-01'",
nprocs=2,
spatial_relation="is_contained",
region_relation="is_contained",
overwrite=True,
verbose=True,
)
Expand All @@ -467,19 +467,6 @@ def test_with_spatial_filter_is_contained(self):
res_line = res.split("|", 1)[1]
self.assertLooksLike(ref_line, res_line)

def test_with_with_spatial_filter_fails(self):
"""Test semantic labels"""
t_rast_univar = SimpleModule(
"t.rast.univar",
flags="r",
input="C",
where="start_time >= '2001-01-01'",
spatial_relation="overlaps",
overwrite=True,
verbose=True,
)
self.assertModuleFail(t_rast_univar)


if __name__ == "__main__":
from grass.gunittest.main import test
Expand Down
4 changes: 2 additions & 2 deletions temporal/t.rast3d.univar/testsuite/test_t_rast3d_univar.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ def test_subset_with_output_no_header(self):
res_line = res.split("|", 1)[1]
self.assertLooksLike(ref_line, res_line)

def test_error_handling_empty_strds(self):
def test_handling_empty_strds(self):
# Empty str3ds
self.assertModuleFail(
self.assertModule(
"t.rast3d.univar",
input="A",
output="univar_output.txt",
Expand Down
4 changes: 2 additions & 2 deletions temporal/temporalintro.html
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ <h4>Querying and map calculation</h4>
<p>
Similar to the <em>where</em> option, selected modules like
<a href="t.rast.univar.html">t.rast.univar</a> support a
<em>spatial_relation</em> option to limit computations to only maps of
the space time raster dataset, that have a given spatial relation to the
<em>spatial_relation</em> option to limit computations to maps of
the space time raster dataset that have a given spatial relation to the
current computational region. Supported spatial relations are:
<ul>
<li>"overlaps": process only maps that spatially overlap ("intersect")
Expand Down