GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
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 
98 void Rast_set_input_window(struct Cell_head *window)
99 {
100  Rast__init();
101 
102  G_adjust_Cell_head(window, 0, 0);
103 
104  R__.rd_window = *window;
105  R__.split_window = 1;
106 
107  update_window_mappings();
108 }
109 
110 static void update_window_mappings(void)
111 {
112  int i;
113  int maskfd;
114 
115  /* adjust window, check for valid window */
116  /* adjust the real one, not a copy
117  G_copy (&twindow, window, sizeof(struct Cell_head));
118  window = &twindow;
119  */
120 
121  /* except for MASK, cell files open for read must have same projection
122  * and zone as new window
123  */
124  maskfd = R__.auto_mask > 0 ? R__.mask_fd : -1;
125  for (i = 0; i < R__.fileinfo_count; i++) {
126  struct fileinfo *fcb = &R__.fileinfo[i];
127 
128  if (fcb->open_mode == OPEN_OLD) {
129  if (fcb->cellhd.zone == R__.rd_window.zone &&
130  fcb->cellhd.proj == R__.rd_window.proj)
131  continue;
132  if (i != maskfd)
133  G_fatal_error(_("Rast_set_read_window(): projection/zone "
134  "differs from that of "
135  "currently open raster maps"));
136  }
137  }
138 
139  /* close the mask */
140  if (R__.auto_mask > 0) {
141  Rast_close(maskfd);
142  /* G_free (R__.mask_buf); */
143  R__.mask_fd = -1;
144  R__.auto_mask = -1; /* turn off masking */
145  }
146 
147  /* now for each possible open cell file, recreate the window mapping */
148  /*
149  * also the memory for reading and writing must be reallocated for all
150  * opened cell files
151  */
152  for (i = 0; i < R__.fileinfo_count; i++) {
153  struct fileinfo *fcb = &R__.fileinfo[i];
154 
155  if (fcb->open_mode != OPEN_OLD &&
158  continue;
159 
160  if (fcb->open_mode == OPEN_OLD)
161  G_fatal_error(_("Input window changed while maps are open for "
162  "read. Map name <%s>"),
163  fcb->name);
164  }
165 
166  /* turn masking (back) on if necessary */
168 }
169 
170 static void check_write_window(void)
171 {
172  int i;
173 
174  for (i = 0; i < R__.fileinfo_count; i++) {
175  struct fileinfo *fcb = &R__.fileinfo[i];
176 
177  if (fcb->open_mode == OPEN_NEW_UNCOMPRESSED ||
179  G_fatal_error(_("Output window changed while maps are open for "
180  "write. Map name <%s>"),
181  fcb->name);
182  }
183 }
#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:35
void Rast_close(int)
Close a raster map.
Definition: raster/close.c:99
void Rast__init(void)
Definition: raster/init.c:63
#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:440
int zone
Projection zone (UTM)
Definition: gis.h:474
int proj
Projection code.
Definition: gis.h:472
Definition: G.h:5
struct Cell_head window
Definition: G.h:6
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