Skip to content

Commit

Permalink
Merge branch 'main' into wxgui_get_list_of_recent_workspace_files
Browse files Browse the repository at this point in the history
  • Loading branch information
echoix committed Dec 12, 2024
2 parents 23804b5 + 3022d9f commit 3144bfb
Show file tree
Hide file tree
Showing 15 changed files with 351 additions and 10 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ dist.*
!.git/refs/heads
!.git/objects
.git/objects/*
!.git/objects/pack
4 changes: 2 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
if: ${{ matrix.language == 'c-cpp' }}

- name: Initialize CodeQL
uses: github/codeql-action/init@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6
uses: github/codeql-action/init@babb554ede22fd5605947329c4d04d8e7a0b8155 # v3.27.7
with:
languages: ${{ matrix.language }}
config-file: ./.github/codeql/codeql-config.yml
Expand All @@ -81,6 +81,6 @@ jobs:
run: .github/workflows/build_ubuntu-22.04.sh "${HOME}/install"

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6
uses: github/codeql-action/analyze@babb554ede22fd5605947329c4d04d8e7a0b8155 # v3.27.7
with:
category: "/language:${{matrix.language}}"
2 changes: 1 addition & 1 deletion .github/workflows/create_release_draft.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ jobs:
sha256sum ${{ env.GRASS }}.tar.xz > ${{ env.GRASS }}.tar.xz.sha256
- name: Publish draft distribution to GitHub (for tags only)
if: startsWith(github.ref, 'refs/tags/')
uses: softprops/action-gh-release@01570a1f39cb168c169c802c3bceb9e93fb10974 # v2.1.0
uses: softprops/action-gh-release@7b4da11513bf3f43f9999e90eabced41ab8bb048 # v2.2.0
with:
name: GRASS GIS ${{ github.ref_name }}
body: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
# Year and week of year so cache key changes weekly
run: echo "date=$(date +%Y-%U)" >> "${GITHUB_OUTPUT}"
- name: Setup Mamba
uses: mamba-org/setup-micromamba@06375d89d211a1232ef63355742e9e2e564bc7f7 # v2.0.2
uses: mamba-org/setup-micromamba@068f1ab4b37ed9b3d9f73da7db90a0cda0a48d29 # v2.0.3
with:
init-shell: bash
environment-file: .github/workflows/macos_dependencies.txt
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-code-quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ jobs:
path: bandit.sarif

- name: Upload SARIF File into Security Tab
uses: github/codeql-action/upload-sarif@aa578102511db1f4524ed59b8cc2bae4f6e88195 # v3.27.6
uses: github/codeql-action/upload-sarif@babb554ede22fd5605947329c4d04d8e7a0b8155 # v3.27.7
with:
sarif_file: bandit.sarif

Expand Down
2 changes: 1 addition & 1 deletion docker/alpine/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM alpine:3.20@sha256:1e42bbe2508154c9126d48c2b8a75420c3544343bf86fd041fb7527e017a4b4a as common
FROM alpine:3.21@sha256:21dc6063fd678b478f57c0e13f47560d0ea4eeba26dfc947b2a4f81f686b9f45 as common

# Based on:
# https://github.com/mundialis/docker-grass-gis/blob/master/Dockerfile
Expand Down
7 changes: 7 additions & 0 deletions lib/raster/put_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ int Rast_put_cell_title(const char *name, const char *title)
if (!out) {
fclose(in);
G_warning(_("G_put_title - can't create a temp file"));
G_free(tempfile);
return -1;
}

Expand All @@ -52,12 +53,15 @@ int Rast_put_cell_title(const char *name, const char *title)
if (line < 3) {
G_warning(_("category information for [%s] in [%s] invalid"), name,
mapset);
remove(tempfile);
G_free(tempfile);
return -1;
}

in = fopen(tempfile, "r");
if (!in) {
G_warning(_("G_put_title - can't reopen temp file"));
G_free(tempfile);
return -1;
}

Expand All @@ -66,6 +70,8 @@ int Rast_put_cell_title(const char *name, const char *title)
fclose(in);
G_warning(_("can't write category information for [%s] in [%s]"), name,
mapset);
remove(tempfile);
G_free(tempfile);
return -1;
}

Expand All @@ -75,6 +81,7 @@ int Rast_put_cell_title(const char *name, const char *title)
fclose(in);
fclose(out);
remove(tempfile);
G_free(tempfile);

return 1;
}
3 changes: 3 additions & 0 deletions ps/ps.map/ps_vlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,5 +206,8 @@ int PS_vlines_plot(struct Map_info *P_map, int vec, int type)
}
fprintf(PS.fp, "\n");
fprintf(PS.fp, "0 setlinejoin\n"); /* reset line join to miter */
Vect_destroy_line_struct(Points);
Vect_destroy_line_struct(nPoints);
Vect_destroy_cats_struct(Cats);
return 0;
}
2 changes: 1 addition & 1 deletion python/grass/script/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ include $(MODULE_TOPDIR)/include/Make/Python.make

DSTDIR = $(ETC)/python/grass/script

MODULES = core db raster raster3d vector array setup task utils
MODULES = core db imagery raster raster3d vector array setup task utils

PYFILES := $(patsubst %,$(DSTDIR)/%.py,$(MODULES) __init__)
PYCFILES := $(patsubst %,$(DSTDIR)/%.pyc,$(MODULES) __init__)
Expand Down
4 changes: 4 additions & 0 deletions python/grass/script/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
make_command,
mapsets,
message,
named_colors,
overwrite,
parse_color,
parse_command,
Expand Down Expand Up @@ -67,6 +68,7 @@
db_table_exist,
db_table_in_vector,
)
from .imagery import group_to_dict
from .raster import mapcalc, mapcalc_start, raster_history, raster_info, raster_what
from .raster3d import mapcalc3d, raster3d_info
from .utils import (
Expand Down Expand Up @@ -145,6 +147,7 @@
"get_raise_on_error",
"get_real_command",
"gisenv",
"group_to_dict",
"handle_errors",
"info",
"legal_name",
Expand All @@ -159,6 +162,7 @@
"mapcalc_start",
"mapsets",
"message",
"named_colors",
"natural_sort",
"naturally_sorted",
"overwrite",
Expand Down
150 changes: 150 additions & 0 deletions python/grass/script/imagery.py
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
Loading

0 comments on commit 3144bfb

Please sign in to comment.