GRASS GIS 8 Programmer's Manual  8.4.0dev(2024)-535c39c9fc
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-2008 by 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  *
13  * \date 1999-2008
14  */
15 
16 #include <stdlib.h>
17 
18 #include <grass/gis.h>
19 #include <grass/raster.h>
20 #include <grass/glocale.h>
21 
22 #include "R.h"
23 
24 /**
25  * \brief Checks for auto masking.
26  *
27  * On first call, opens the mask file if declared and available and
28  * allocates buffer for reading mask rows.
29  * On second call, returns 0 or 1.
30  *
31  * \return 0 if mask unset or unavailable
32  * \return 1 if mask set and available and ready to use
33  */
34 
36 {
37  struct Cell_head cellhd;
38 
39  Rast__init();
40 
41  /* if mask is switched off (-2) return -2
42  if R__.auto_mask is not set (-1) or set (>=0) recheck the MASK */
43 
44  if (R__.auto_mask < -1)
45  return R__.auto_mask;
46 
47  /* if(R__.mask_fd > 0) G_free (R__.mask_buf); */
48 
49  /* look for the existence of the MASK file */
50  R__.auto_mask = (G_find_raster("MASK", G_mapset()) != 0);
51 
52  if (R__.auto_mask <= 0)
53  return 0;
54 
55  /* check MASK projection/zone against current region */
56  Rast_get_cellhd("MASK", G_mapset(), &cellhd);
57  if (cellhd.zone != G_zone() || cellhd.proj != G_projection()) {
58  R__.auto_mask = 0;
59  return 0;
60  }
61 
62  if (R__.mask_fd >= 0)
64  R__.mask_fd = Rast__open_old("MASK", G_mapset());
65  if (R__.mask_fd < 0) {
66  R__.auto_mask = 0;
67  G_warning(_("Unable to open automatic MASK file"));
68  return 0;
69  }
70 
71  /* R__.mask_buf = Rast_allocate_c_buf(); */
72 
73  R__.auto_mask = 1;
74 
75  return 1;
76 }
77 
78 /**
79  * \brief Suppresses masking.
80  *
81  * \return
82  */
83 
85 {
86  Rast__init();
87 
88  if (R__.auto_mask > 0) {
90  /* G_free (R__.mask_buf); */
91  R__.mask_fd = -1;
92  }
93  R__.auto_mask = -2;
94 }
95 
96 /**
97  * \brief Unsuppresses masking.
98  *
99  * \return
100  */
101 
103 {
104  Rast__init();
105 
106  if (R__.auto_mask < -1) {
107  R__.mask_fd = -1;
109  }
110 }
int Rast__check_for_auto_masking(void)
Checks for auto masking.
Definition: auto_mask.c:35
void Rast_suppress_masking(void)
Suppresses masking.
Definition: auto_mask.c:84
void Rast_unsuppress_masking(void)
Unsuppresses masking.
Definition: auto_mask.c:102
void G_warning(const char *,...) __attribute__((format(printf
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
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
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:437
int zone
Projection zone (UTM)
Definition: gis.h:471
int proj
Projection code.
Definition: gis.h:469
Definition: R.h:87
int auto_mask
Definition: R.h:90
int mask_fd
Definition: R.h:89