GRASS GIS 8 Programmer's Manual  8.5.0dev(2025)-582d100897
auto_mask.c
Go to the documentation of this file.
1 /**
2  * \file auto_mask.c
3  *
4  * \brief Raster Library - Auto masking routines.
5  *
6  * (C) 2001-2024 by Vaclav Petras and the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author GRASS GIS Development Team
12  * \author Vaclav Petras (environmental variable and refactoring)
13  */
14 
15 #include <stdlib.h>
16 
17 #include <grass/gis.h>
18 #include <grass/raster.h>
19 #include <grass/glocale.h>
20 
21 #include "R.h"
22 
23 /**
24  * \brief Checks for auto masking.
25  *
26  * On first call, opens the mask file if declared and available and
27  * allocates buffer for reading mask rows.
28  * On second call, returns 0 or 1.
29  *
30  * \return 0 if mask unset or unavailable
31  * \return 1 if mask set and available and ready to use
32  */
34 {
35  struct Cell_head cellhd;
36 
37  Rast__init();
38 
39  /* if mask is switched off (-2) return -2
40  if R__.auto_mask is not set (-1) or set (>=0) recheck the mask */
41 
42  // TODO: This needs to be documented or modified accordingly.
43  if (R__.auto_mask < -1)
44  return R__.auto_mask;
45 
46  /* if(R__.mask_fd > 0) G_free (R__.mask_buf); */
47 
48  /* Decide between default mask name and env var specified one. */
49  char *mask_name = Rast_mask_name();
50  char *mask_mapset = "";
51 
52  /* Check for the existence of the mask raster. */
53  R__.auto_mask = (G_find_raster2(mask_name, mask_mapset) != 0);
54 
55  if (R__.auto_mask <= 0)
56  return 0;
57 
58  /* Check mask raster projection/zone against current region */
59  Rast_get_cellhd(mask_name, mask_mapset, &cellhd);
60  if (cellhd.zone != G_zone() || cellhd.proj != G_projection()) {
61  R__.auto_mask = 0;
62  return 0;
63  }
64 
65  if (R__.mask_fd >= 0)
67  R__.mask_fd = Rast__open_old(mask_name, mask_mapset);
68  if (R__.mask_fd < 0) {
69  R__.auto_mask = 0;
70  G_warning(_("Unable to open automatic mask <%s>"), mask_name);
71  return 0;
72  }
73 
74  /* R__.mask_buf = Rast_allocate_c_buf(); */
75 
76  R__.auto_mask = 1;
77  G_free(mask_name);
78 
79  return 1;
80 }
81 
82 /**
83  * \brief Suppresses masking.
84  *
85  * \return
86  */
87 
89 {
90  Rast__init();
91 
92  if (R__.auto_mask > 0) {
94  /* G_free (R__.mask_buf); */
95  R__.mask_fd = -1;
96  }
97  R__.auto_mask = -2;
98 }
99 
100 /**
101  * \brief Unsuppresses masking.
102  *
103  * \return
104  */
105 
107 {
108  Rast__init();
109 
110  if (R__.auto_mask < -1) {
111  R__.mask_fd = -1;
113  }
114 }
int Rast__check_for_auto_masking(void)
Checks for auto masking.
Definition: auto_mask.c:33
void Rast_suppress_masking(void)
Suppresses masking.
Definition: auto_mask.c:88
void Rast_unsuppress_masking(void)
Unsuppresses masking.
Definition: auto_mask.c:106
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
void G_warning(const char *,...) __attribute__((format(printf
const char * G_find_raster2(const char *, const char *)
Find a raster map (look but don't touch)
Definition: find_rast.c:76
int G_zone(void)
Query cartographic zone.
Definition: zone.c:24
int G_projection(void)
Query cartographic projection.
Definition: proj1.c:32
void Rast_unopen(int)
Unopen a raster map.
Definition: raster/close.c:132
void Rast_close(int)
Close a raster map.
Definition: raster/close.c:99
char * Rast_mask_name(void)
Retrieves the name of the raster mask to use.
Definition: mask_info.c:72
int Rast__open_old(const char *, const char *)
Lower level function, open cell files, supercell files, and the mask file.
Definition: raster/open.c:152
void Rast__init(void)
Definition: raster/init.c:63
void Rast_get_cellhd(const char *, const char *, struct Cell_head *)
Read the raster header.
Definition: get_cellhd.c:41
#define _(str)
Definition: glocale.h:10
2D/3D raster map header (used also for region)
Definition: gis.h:441
int zone
Projection zone (UTM)
Definition: gis.h:475
int proj
Projection code.
Definition: gis.h:473
Definition: R.h:87
int auto_mask
Definition: R.h:90
int mask_fd
Definition: R.h:89