-
-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
388 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ dist.* | |
!.git/refs/heads | ||
!.git/objects | ||
.git/objects/* | ||
!.git/objects/pack |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
""" | ||
Imagery related functions to be used in Python scripts. | ||
Usage: | ||
:: | ||
import grass.script as gs | ||
gs.imagery.group_to_dict(imagery_group) | ||
... | ||
(C) 2024 by Stefan Blumentrath and the GRASS Development Team | ||
This program is free software under the GNU General Public | ||
License (>=v2). Read the file COPYING that comes with GRASS | ||
for details. | ||
.. sectionauthor:: Stefan Blumentrath | ||
""" | ||
|
||
from .core import read_command, warning, fatal | ||
from .raster import raster_info | ||
|
||
|
||
def group_to_dict( | ||
imagery_group_name, | ||
subgroup=None, | ||
dict_keys="semantic_labels", | ||
dict_values="map_names", | ||
fill_semantic_label=True, | ||
env=None, | ||
): | ||
"""Create a dictionary to represent an imagery group with metadata. | ||
Depending on the dict_keys option, the returned dictionary uses either | ||
the names of the raster maps ("map_names"), their row indices in the group | ||
("indices") or their associated semantic_labels ("semantic_labels") as keys. | ||
The default is to use semantic_labels. Note that map metadata | ||
of the maps in the group have to be read to get the semantic label, | ||
in addition to the group file. The same metadata is read when the | ||
"metadata" is requested as dict_values. Other supported dict_values | ||
are "map_names" (default), "semantic_labels", or "indices". | ||
The function can also operate on the level of subgroups. In case a | ||
non-existing (or empty sub-group) is requested a warning is printed | ||
and an empty dictionary is returned (following the behavior of i.group). | ||
Example:: | ||
>>> run_command("g.copy", raster="lsat7_2000_10,lsat7_2000_10") | ||
>>> run_command("r.support", raster="lsat7_2000_10", semantic_label="L8_1") | ||
>>> run_command("g.copy", raster="lsat7_2000_20,lsat7_2000_20") | ||
>>> run_command("r.support", raster="lsat7_2000_20", semantic_label="L8_2") | ||
>>> run_command("g.copy", raster="lsat7_2000_30,lsat7_2000_30") | ||
>>> run_command("r.support", raster="lsat7_2000_30", semantic_label="L8_3") | ||
>>> run_command("i.group", group="L8_group", | ||
>>> input="lsat7_2000_10,lsat7_2000_20,lsat7_2000_30") | ||
>>> group_to_dict("L8_group") # doctest: +ELLIPSIS | ||
{"L8_1": "lsat7_2000_10", ... "L8_3": "lsat7_2000_30"} | ||
>>> run_command("g.remove", flags="f", type="group", name="L8_group") | ||
>>> run_command("g.remove", flags="f", type="raster", | ||
>>> name="lsat7_2000_10,lsat7_2000_20,lsat7_2000_30") | ||
:param str imagery_group_name: Name of the imagery group to process (or None) | ||
:param str subgroup: Name of the imagery sub-group to process (or None) | ||
:param str dict_keys: What to use as key for dictionary. It can be either | ||
"semantic_labels" (default), "map_names" or "indices" | ||
:param str dict_values: What to use as values for dictionary. It can be either | ||
"map_names" (default), "semanic_labels", "indices" or | ||
"metadata" (to return dictionaries with full map metadata) | ||
:param bool fill_semantic_label: If maps in a group do not have a semantic | ||
label, their index in the group is used | ||
instead (default). Otherwise None / "none" | ||
is used. | ||
:param dict env: Environment to use when parsing the imagery group | ||
:return: dictionary representing an imagery group with it's maps and their | ||
semantic labels, row indices in the group, or metadata | ||
:rtype: dict | ||
""" | ||
group_dict = {} | ||
maps_in_group = ( | ||
read_command( | ||
"i.group", | ||
group=imagery_group_name, | ||
subgroup=subgroup, | ||
flags="g", | ||
quiet=True, | ||
env=env, | ||
) | ||
.strip() | ||
.split() | ||
) | ||
|
||
if dict_keys not in {"indices", "map_names", "semantic_labels"}: | ||
raise ValueError(f"Invalid dictionary keys <{dict_keys}> requested") | ||
|
||
if dict_values not in {"indices", "map_names", "semantic_labels", "metadata"}: | ||
raise ValueError(f"Invalid dictionary values <{dict_values}> requested") | ||
|
||
if subgroup and not maps_in_group: | ||
warning( | ||
_("Empty result returned for subgroup <{sg}> in group <{g}>").format( | ||
sg=subgroup, g=imagery_group_name | ||
) | ||
) | ||
|
||
for idx, raster_map in enumerate(maps_in_group): | ||
raster_map_info = None | ||
# Get raster metadata if needed | ||
if ( | ||
dict_values in {"semantic_labels", "metadata"} | ||
or dict_keys == "semantic_labels" | ||
): | ||
raster_map_info = raster_info(raster_map, env=env) | ||
|
||
# Get key for dictionary | ||
if dict_keys == "indices": | ||
key = str(idx + 1) | ||
elif dict_keys == "map_names": | ||
key = raster_map | ||
elif dict_keys == "semantic_labels": | ||
key = raster_map_info["semantic_label"] | ||
if not key or key == '"none"': | ||
if fill_semantic_label: | ||
key = str(idx + 1) | ||
else: | ||
fatal( | ||
_( | ||
"Semantic label missing for raster map {m} in group <{g}>." | ||
).format(m=raster_map, g=imagery_group_name) | ||
) | ||
|
||
if dict_values == "indices": | ||
val = str(idx + 1) | ||
elif dict_values == "map_names": | ||
val = raster_map | ||
elif dict_values == "semantic_labels": | ||
val = raster_map_info["semantic_label"] | ||
elif dict_values == "metadata": | ||
val = raster_map_info | ||
if key in group_dict: | ||
warning( | ||
_( | ||
"Key {k} from raster map {m} already present in group dictionary." | ||
"Overwriting existing entry..." | ||
).format(k=key, r=raster_map) | ||
) | ||
group_dict[key] = val | ||
return group_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
import os | ||
|
||
import pytest | ||
|
||
import grass.script as gs | ||
from grass.script.task import grassTask | ||
|
||
|
||
def test_mapcalc_simple_e_name(): | ||
@pytest.fixture | ||
def xy_session_patched_env(tmp_path, monkeypatch): | ||
"""Active session in an XY location (scope: function), patching env vars directly. | ||
This allows functions not accepting an env dictionary argument to work in tests""" | ||
location = "xy_test" | ||
gs.core._create_location_xy(tmp_path, location) # pylint: disable=protected-access | ||
with gs.setup.init(tmp_path / location, env=os.environ.copy()) as session: | ||
for key, value in session.env.items(): | ||
monkeypatch.setenv(key, value) | ||
yield session | ||
|
||
|
||
def test_mapcalc_simple_e_name(xy_session_patched_env): | ||
gt = grassTask("r.mapcalc.simple") | ||
assert gt.get_param("e")["name"] == "e" | ||
|
||
|
||
def test_mapcalc_simple_expession_name(): | ||
def test_mapcalc_simple_expression_name(xy_session_patched_env): | ||
gt = grassTask("r.mapcalc.simple") | ||
assert gt.get_param("expression")["name"] == "expression" | ||
|
||
|
||
def test_d_vect_from_bin(xy_session_patched_env): | ||
"""Tests that a module installed in "$GISBASE/bin can be used with grassTask""" | ||
task = grassTask("d.vect") | ||
task.get_param("map")["value"] = "map_name" | ||
task.get_flag("i")["value"] = True | ||
task.get_param("layer")["value"] = 1 | ||
task.get_param("label_bcolor")["value"] = "red" | ||
# the default parameter display is added automatically | ||
actual = " ".join(task.get_cmd()) | ||
expected = "d.vect -i map=map_name layer=1 display=shape label_bcolor=red" | ||
assert actual == expected | ||
|
||
|
||
def test_v_clip_from_scripts(xy_session_patched_env): | ||
"""Tests that a module installed in "$GISBASE/scripts can be used with grassTask""" | ||
task = grassTask("v.clip") | ||
task.get_param("input")["value"] = "map_name" | ||
task.get_flag("r")["value"] = True | ||
task.get_param("clip")["value"] = "clip_map_name" | ||
task.get_param("output")["value"] = "output_map_name" | ||
actual = " ".join(task.get_cmd()) | ||
expected = "v.clip -r input=map_name clip=clip_map_name output=output_map_name" | ||
assert actual == expected |
Oops, something went wrong.