GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
mask_info.c
Go to the documentation of this file.
1/**
2 * \file lib/raster/mask_info.c
3 *
4 * \brief Raster Library - Get mask information
5 *
6 * SPDX-FileCopyrightText: 1999-2024 Vaclav Petras
7 * SPDX-FileCopyrightText: GRASS Development Team
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 *
10 * \author CERL
11 * \author Vaclav Petras, NC State University, Center for Geospatial Analytics
12 */
13
14#include <string.h>
15#include <stdbool.h>
16#include <stdlib.h>
17
18#if defined(_OPENMP)
19#include <omp.h>
20#endif
21
22#include <grass/gis.h>
23#include <grass/raster.h>
24#include <grass/glocale.h>
25
26/**
27 * @brief Get a printable text with information about raster mask
28 *
29 * Determines if 2D raster mask is present and returns textual information about
30 * the mask suitable for end-user display. The resulting text is translated.
31 * Caller is responsible for freeing the memory of the returned string.
32 *
33 * @return New string with textual information
34 *
35 * @see Rast_mask_status()
36 */
37char *Rast_mask_info(void)
38{
39 char text[GNAME_MAX + GMAPSET_MAX + 16];
40 char name[GNAME_MAX];
41 char mapset[GMAPSET_MAX];
42
43 switch (Rast__mask_info(name, mapset)) {
44 case 1:
45 snprintf(text, sizeof(text), _("<%s> in mapset <%s>"), name, mapset);
46 break;
47 case -1:
48 strcpy(text, _("none"));
49 break;
50 default:
51 strcpy(text, _("not known"));
52 break;
53 }
54
55 return G_store(text);
56}
57
58/**
59 * @brief Retrieves the name of the raster mask to use.
60 *
61 * The returned raster map name is fully qualified, i.e., in the form
62 * "name@mapset". The mask name is returned whether the mask is present or not.
63 *
64 * This function checks if an environment variable "GRASS_MASK" is set.
65 * If it is set, the value of the environment variable is returned
66 * as the mask name. If it is not set, the function will default to the
67 * mask name "MASK@<mapset>", where <mapset> is the current mapset.
68 *
69 * The memory for the returned mask name is dynamically allocated using
70 * G_store(). It is the caller's responsibility to free the memory with
71 * G_free() when it is no longer needed.
72 *
73 * @returns A dynamically allocated string containing the mask name.
74 *
75 * @since version 8.5
76 */
77char *Rast_mask_name(void)
78{
79 // First, see if the environment variable is defined.
80 const char *env_variable = getenv("GRASS_MASK");
81 if (env_variable != NULL && strcmp(env_variable, "") != 0) {
82 // Variable exists and is not empty.
83 // While the function does not document that, the provided mapset
84 // is a fallback, so we don't have to parse the name to find out
85 // ourselves what to do.
87 }
88
89 // Mask name defaults to "MASK@<current mapset>".
90 return G_fully_qualified_name("MASK", G_mapset());
91}
92
93/**
94 * @brief Get name of a mask if it is present
95 *
96 * Unlike, Rast__mask_info() this always returns name of the mask
97 * if it is present regardless of the mask being a reclass or not.
98 *
99 * @param[out] name Name of the raster map used as mask
100 * @param[out] mapset Name of the map's mapset
101 *
102 * @return true if mask is present, false otherwise
103 */
104static bool Rast__get_present_mask(char *name, char *mapset)
105{
107 char *full_name = Rast_mask_name();
108 if (!G_find_raster2(full_name, ""))
109 return false;
112 strncpy(mapset, rmapset, GMAPSET_MAX);
114 return true;
115}
116
117/**
118 * @brief Get raster mask status information
119 *
120 * _is_mask_reclass_ is a pointer to a bool variable which
121 * will be set to true if mask raster is a reclass and false otherwise.
122 *
123 * If you are not interested in the underlying reclassified raster map,
124 * pass NULL pointers for the three reclass parameters:
125 *
126 * ```
127 * Rast_mask_status(name, mapset, NULL, NULL, NULL);
128 * ```
129 *
130 * @param[out] name Name of the raster map used as mask
131 * @param[out] mapset Name of the mapset the raster is in
132 * @param[out] is_mask_reclass Will be set to true if mask raster is a reclass
133 * @param[out] reclass_name Name of the underlying reclassified raster map
134 * @param[out] reclass_mapset Name of the mapset the reclassified raster is in
135 *
136 * @return true if mask is present, false otherwise
137 *
138 * @since version 8.5
139 */
140bool Rast_mask_status(char *name, char *mapset, bool *is_mask_reclass,
143{
144 bool present = Rast__get_present_mask(name, mapset);
145
147 if (present) {
150 }
151 else {
152 *is_mask_reclass = false;
153 }
154 }
155 return present;
156}
157
158/**
159 * @brief Get information about the current mask
160 *
161 * Determines the status of the automatic masking and the name of the 2D
162 * raster which forms the mask. Typically, mask is raster called MASK in the
163 * current mapset, but when used with r.mask, it is usually a reclassed
164 * raster, and so when a mask raster is present and it is a reclass raster,
165 * the name and mapset of the underlying reclassed raster are returned.
166 *
167 * The name and mapset is written to the parameter which need to be defined
168 * with a sufficient size, least as `char name[GNAME_MAX], mapset[GMAPSET_MAX]`.
169 *
170 * When the masking is not active, -1 is returned and name and mapset are
171 * undefined. When the masking is active, 1 is returned and name and mapset
172 * will hold the name and mapset of the underlying raster.
173 *
174 * @param[out] name Name of the raster map used as mask
175 * @param[out] mapset Name of the map's mapset
176 *
177 * @return 1 if mask is present, -1 otherwise
178 *
179 * @see Rast_mask_status(), Rast_mask_name()
180 */
181int Rast__mask_info(char *name, char *mapset)
182{
184 bool present = Rast__get_present_mask(name, mapset);
185 if (!present)
186 return -1;
187
188 if (Rast_is_reclass(name, mapset, rname, rmapset) > 0) {
189 strcpy(name, rname);
190 strcpy(mapset, rmapset);
191 }
192 return 1;
193}
194
195/**
196 * @brief Check presence of 2D raster mask
197 *
198 * @return true if mask is present, false otherwise
199 *
200 * @since version 8.5
201 */
203{
204 char *name = Rast_mask_name();
205 bool present = G_find_raster2(name, "") != NULL;
206 return present;
207}
208
209/**
210 * \brief Disable OpenMP if raster mask is present
211 *
212 * This helper function can be removed when raster reading is made
213 * thread safe.
214 *
215 * \param nprocs number of threads to use
216 * \return number of threads in use
217 *
218 * \since version 8.5
219 */
221{
222
223#if defined(_OPENMP)
224 if (nprocs > 1 && Rast_mask_is_present()) {
226 G_verbose_message(_("Single thread processing enforced due to "
227 "raster mask being present."));
228 nprocs = 1;
229 }
230#else
231 nprocs = 1;
232#endif
233
234 return nprocs;
235}
#define NULL
Definition ccmath.h:32
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
int G_unqualified_name(const char *, const char *, char *, char *)
Returns unqualified map name (without @ mapset)
Definition nme_in_mps.c:132
void void G_verbose_message(const char *,...) __attribute__((format(printf
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition nme_in_mps.c:99
char * G_store(const char *)
Copy string to allocated memory.
Definition strings.c:85
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition find_rast.c:73
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
int Rast_is_reclass(const char *, const char *, char[256], char[256])
Check if raster map is reclassified.
Definition reclass.c:42
#define GMAPSET_MAX
Definition gis.h:194
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
int Rast_disable_omp_on_mask(int nprocs)
Disable OpenMP if raster mask is present.
Definition mask_info.c:220
bool Rast_mask_status(char *name, char *mapset, bool *is_mask_reclass, char reclass_name[256], char reclass_mapset[256])
Get raster mask status information.
Definition mask_info.c:140
char * Rast_mask_name(void)
Retrieves the name of the raster mask to use.
Definition mask_info.c:77
int Rast__mask_info(char *name, char *mapset)
Get information about the current mask.
Definition mask_info.c:181
char * Rast_mask_info(void)
Get a printable text with information about raster mask.
Definition mask_info.c:37
bool Rast_mask_is_present(void)
Check presence of 2D raster mask.
Definition mask_info.c:202
const char * name
Definition named_colr.c:6
#define strcpy
Definition parson.c:66