GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-77aab223bc
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  * (C) 1999-2024 by Vaclav Petras and the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public
9  * License (>=v2). Read the file COPYING that comes with GRASS
10  * for details.
11  *
12  * \author CERL
13  * \author Vaclav Petras, NC State University, Center for Geospatial Analytics
14  */
15 
16 #include <string.h>
17 
18 #include <grass/gis.h>
19 #include <grass/raster.h>
20 #include <grass/glocale.h>
21 
22 /**
23  * @brief Get a printable text with information about raster mask
24  *
25  * Determines if 2D raster mask is present and returns textual information about
26  * the mask suitable for end-user display. The resulting text is translated.
27  * Caller is responsible for freeing the memory of the returned string.
28  *
29  * @return New string with textual information
30  */
31 char *Rast_mask_info(void)
32 {
33  char text[GNAME_MAX + GMAPSET_MAX + 16];
34  char name[GNAME_MAX];
35  char mapset[GMAPSET_MAX];
36 
37  switch (Rast__mask_info(name, mapset)) {
38  case 1:
39  sprintf(text, _("<%s> in mapset <%s>"), name, mapset);
40  break;
41  case -1:
42  strcpy(text, _("none"));
43  break;
44  default:
45  strcpy(text, _("not known"));
46  break;
47  }
48 
49  return G_store(text);
50 }
51 
52 /**
53  * @brief Retrieves the name of the raster mask to use.
54  *
55  * The returned raster map name is fully qualified, i.e., in the form
56  % "name@mapset".
57  *
58  * The mask name is "MASK@<mapset>", where <mapset> is the current
59  * mapset.
60  *
61  * The memory for the returned mask name is dynamically allocated using
62  * G_store(). It is the caller's responsibility to free the memory with
63  * G_free() when it is no longer needed.
64  *
65  * @returns A dynamically allocated string containing the mask name.
66  */
67 char *Rast_mask_name(void)
68 {
69  // Mask name is always "MASK@<current mapset>".
70  return G_fully_qualified_name("MASK", G_mapset());
71 }
72 
73 /**
74  * @brief Get raster mask status information
75  *
76  * _is_mask_reclass_ is a pointer to a bool variable which
77  * will be set to true if mask raster is a reclass and false otherwise.
78  *
79  * If you are not interested in the underlying reclassified raster map,
80  * pass NULL pointers for the three reclass parameters:
81  *
82  * ```
83  * Rast_mask_status(name, mapset, NULL, NULL, NULL);
84  * ```
85  *
86  * @param[out] name Name of the raster map used as mask
87  * @param[out] mapset Name of the mapset the raster is in
88  * @param[out] is_mask_reclass Will be set to true if mask raster is a reclass
89  * @param[out] reclass_name Name of the underlying reclassified raster map
90  * @param[out] reclass_mapset Name of the mapset the reclassified raster is in
91  *
92  * @return true if mask is present, false otherwise
93  */
94 bool Rast_mask_status(char *name, char *mapset, bool *is_mask_reclass,
95  char *reclass_name, char *reclass_mapset)
96 {
97  int present = Rast__mask_info(name, mapset);
98 
99  if (is_mask_reclass && reclass_name && reclass_mapset) {
100  if (present) {
101  *is_mask_reclass = Rast_is_reclass("MASK", G_mapset(), reclass_name,
102  reclass_mapset) > 0;
103  if (*is_mask_reclass) {
104  // The original mask values were overwritten in the initial
105  // info call. Put back the original values, so that we can
106  // report them to the caller.
107  strcpy(name, "MASK");
108  strcpy(mapset, G_mapset());
109  }
110  }
111  else {
112  *is_mask_reclass = false;
113  }
114  }
115 
116  if (present == 1)
117  return true;
118  else
119  return false;
120 }
121 
122 /**
123  * @brief Get information about the current mask
124  *
125  * Determines the status of the automatic masking and the name of the 2D
126  * raster which forms the mask. Typically, mask is raster called MASK in the
127  * current mapset, but when used with r.mask, it is usually a reclassed
128  * raster, and so when a MASK raster is present and it is a reclass raster,
129  * the name and mapset of the underlying reclassed raster are returned.
130  *
131  * The name and mapset is written to the parameter which need to be defined
132  * with a sufficient size, least as `char name[GNAME_MAX], mapset[GMAPSET_MAX]`.
133  *
134  * When the masking is not active, -1 is returned and name and mapset are
135  * undefined. When the masking is active, 1 is returned and name and mapset
136  * will hold the name and mapset of the underlying raster.
137  *
138  * @param[out] name Name of the raster map used as mask
139  * @param[out] mapset Name of the map's mapset
140  *
141  * @return 1 if mask is present, -1 otherwise
142  */
143 int Rast__mask_info(char *name, char *mapset)
144 {
145  char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
146 
147  strcpy(rname, "MASK");
148  (void)G_strlcpy(rmapset, G_mapset(), GMAPSET_MAX);
149 
150  if (!G_find_raster(rname, rmapset))
151  return -1;
152 
153  strcpy(name, rname);
154  strcpy(mapset, rmapset);
155  if (Rast_is_reclass(name, mapset, rname, rmapset) > 0) {
156  strcpy(name, rname);
157  strcpy(mapset, rmapset);
158  }
159 
160  return 1;
161 }
162 
163 /**
164  * @brief Check presence of 2D raster mask
165  *
166  * @return true if mask is present, false otherwise
167  */
169 {
170  return G_find_raster("MASK", G_mapset()) != NULL;
171 }
#define NULL
Definition: ccmath.h:32
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
const char * G_find_raster(char *, const char *)
Find a raster map.
Definition: find_rast.c:55
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition: nme_in_mps.c:101
char * G_store(const char *)
Copy string to allocated memory.
Definition: strings.c:87
size_t G_strlcpy(char *, const char *, size_t)
Safe string copy function.
Definition: strlcpy.c:52
int Rast_is_reclass(const char *, const char *, char *, char *)
Check if raster map is reclassified.
Definition: reclass.c:43
#define GMAPSET_MAX
Definition: gis.h:192
#define GNAME_MAX
Definition: gis.h:191
#define _(str)
Definition: glocale.h:10
char * Rast_mask_name(void)
Retrieves the name of the raster mask to use.
Definition: mask_info.c:67
bool Rast_mask_status(char *name, char *mapset, bool *is_mask_reclass, char *reclass_name, char *reclass_mapset)
Get raster mask status information.
Definition: mask_info.c:94
char * Rast_mask_info(void)
Get a printable text with information about raster mask.
Definition: mask_info.c:31
int Rast__mask_info(char *name, char *mapset)
Get information about the current mask.
Definition: mask_info.c:143
bool Rast_mask_is_present(void)
Check presence of 2D raster mask.
Definition: mask_info.c:168
const char * name
Definition: named_colr.c:6
#define strcpy
Definition: parson.c:62