GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
raster/set_window.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/set_window.c
3  *
4  * \brief Raster Library - Set window (map region)
5  *
6  * (C) 2001-2009 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 Original author CERL
12  */
13 
14 #include <grass/gis.h>
15 #include <grass/raster.h>
16 #include <grass/glocale.h>
17 
18 #include "../gis/G.h"
19 #include "R.h"
20 
21 static void update_window_mappings(void);
22 static void check_write_window(void);
23 
25 {
27  return;
28 
30 
33  R__.split_window = 0;
34 
36 }
37 
38 /*!
39  * \brief Establishes 'window' as the current working window.
40  *
41  * \param window window to become operative window
42  */
43 void Rast_set_window(struct Cell_head *window)
44 {
45  Rast__init();
46 
47  if (R__.split_window)
48  G_warning(_("Rast_set_window() called while window split"));
49 
50  check_write_window();
51 
52  G_adjust_Cell_head(window, 0, 0);
53 
54  R__.wr_window = *window;
55  R__.rd_window = *window;
56  R__.split_window = 0;
57 
58  update_window_mappings();
59 }
60 
61 /*!
62  \brief Unset current window
63  */
65 {
66  G_debug(4, "Rast_unset_window()");
67 
68  R__.window_set = 0;
69 }
70 
71 /*!
72  * \brief Establishes 'window' as the current working window for output.
73  *
74  * \param window window to become operative window
75  */
76 void Rast_set_output_window(struct Cell_head *window)
77 {
78  Rast__init();
79 
80  check_write_window();
81 
82  G_adjust_Cell_head(window, 0, 0);
83 
84  R__.wr_window = *window;
85  R__.split_window = 1;
86 
87  G_set_window(window);
88 }
89 
90 /*!
91  * \brief Establishes 'window' as the current working window for input.
92  *
93  * Any opened cell files has its file-to-window mapping reworked.
94  *
95  * \param window window to become operative window
96  */
97 void Rast_set_input_window(struct Cell_head *window)
98 {
99  Rast__init();
100 
101  G_adjust_Cell_head(window, 0, 0);
102 
103  R__.rd_window = *window;
104  R__.split_window = 1;
105 
106  update_window_mappings();
107 }
108 
109 static void update_window_mappings(void)
110 {
111  int i;
112  int maskfd;
113 
114  /* adjust window, check for valid window */
115  /* adjust the real one, not a copy
116  G_copy (&twindow, window, sizeof(struct Cell_head));
117  window = &twindow;
118  */
119 
120  /* except for mask raster, cell files open for read must have same
121  * projection and zone as new window
122  */
123  maskfd = R__.auto_mask > 0 ? R__.mask_fd : -1;
124  for (i = 0; i < R__.fileinfo_count; i++) {
125  struct fileinfo *fcb = &R__.fileinfo[i];
126 
127  if (fcb->open_mode == OPEN_OLD) {
128  if (fcb->cellhd.zone == R__.rd_window.zone &&
129  fcb->cellhd.proj == R__.rd_window.proj)
130  continue;
131  if (i != maskfd)
132  G_fatal_error(_("Rast_set_read_window(): projection/zone "
133  "differs from that of "
134  "currently open raster maps"));
135  }
136  }
137 
138  /* close the mask */
139  if (R__.auto_mask > 0) {
140  Rast_close(maskfd);
141  /* G_free (R__.mask_buf); */
142  R__.mask_fd = -1;
143  R__.auto_mask = -1; /* turn off masking */
144  }
145 
146  /* now for each possible open cell file, recreate the window mapping */
147  /*
148  * also the memory for reading and writing must be reallocated for all
149  * opened cell files
150  */
151  for (i = 0; i < R__.fileinfo_count; i++) {
152  struct fileinfo *fcb = &R__.fileinfo[i];
153 
154  if (fcb->open_mode != OPEN_OLD &&
157  continue;
158 
159  if (fcb->open_mode == OPEN_OLD)
160  G_fatal_error(_("Input window changed while maps are open for "
161  "read. Map name <%s>"),
162  fcb->name);
163  }
164 
165  /* turn masking (back) on if necessary */
167 }
168 
169 static void check_write_window(void)
170 {
171  int i;
172 
173  for (i = 0; i < R__.fileinfo_count; i++) {
174  struct fileinfo *fcb = &R__.fileinfo[i];
175 
176  if (fcb->open_mode == OPEN_NEW_UNCOMPRESSED ||
178  G_fatal_error(_("Output window changed while maps are open for "
179  "write. Map name <%s>"),
180  fcb->name);
181  }
182 }
#define OPEN_NEW_COMPRESSED
Definition: R.h:107
#define OPEN_NEW_UNCOMPRESSED
Definition: R.h:108
#define OPEN_OLD
Definition: R.h:106
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_warning(const char *,...) __attribute__((format(printf
void G_set_window(struct Cell_head *)
Establishes window as the current working window (region).
void G__init_window(void)
Initialize window (region).
int G_is_initialized(int *)
Definition: counter.c:60
void G_initialize_done(int *)
Definition: counter.c:77
void G_adjust_Cell_head(struct Cell_head *, int, int)
Adjust cell header.
Definition: adj_cellhd.c:51
int G_debug(int, const char *,...) __attribute__((format(printf
int Rast__check_for_auto_masking(void)
Checks for auto masking.
Definition: auto_mask.c:33
void Rast_close(int)
Close a raster map.
Definition: raster/close.c:99
void Rast__init(void)
Definition: raster/init.c:61
#define _(str)
Definition: glocale.h:10
void Rast__init_window(void)
void Rast_unset_window(void)
Unset current window.
void Rast_set_window(struct Cell_head *window)
Establishes 'window' as the current working window.
void Rast_set_output_window(struct Cell_head *window)
Establishes 'window' as the current working window for output.
void Rast_set_input_window(struct Cell_head *window)
Establishes 'window' as the current working window for input.
2D/3D raster map header (used also for region)
Definition: gis.h:446
int zone
Projection zone (UTM)
Definition: gis.h:480
int proj
Projection code.
Definition: gis.h:478
Definition: G.h:11
struct Cell_head window
Definition: G.h:12
Definition: R.h:87
struct fileinfo * fileinfo
Definition: R.h:101
int auto_mask
Definition: R.h:90
int window_set
Definition: R.h:95
int fileinfo_count
Definition: R.h:100
int split_window
Definition: R.h:96
int mask_fd
Definition: R.h:89
struct Cell_head wr_window
Definition: R.h:98
struct Cell_head rd_window
Definition: R.h:97
Definition: R.h:53
struct Cell_head cellhd
Definition: R.h:55
int open_mode
Definition: R.h:54
char * name
Definition: R.h:76