GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
raster/window.c
Go to the documentation of this file.
1 /*!
2  * \file lib/raster/window.c
3  *
4  * \brief Raster Library - Window functions.
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 "R.h"
19 
20 /*!
21  * \brief Read the current window
22  *
23  * \param window pointer to Cell_head
24  */
25 void Rast_get_window(struct Cell_head *window)
26 {
28 
29  if (R__.split_window)
31  _("Internal error: Rast_get_window() called with split window."
32  " Use Rast_get_input_window() or Rast_get_output_window() "
33  "instead."));
34 
35  *window = R__.wr_window;
36 }
37 
38 /*!
39  * \brief Read the current input window
40  *
41  * \param window pointer to Cell_head
42  */
43 void Rast_get_input_window(struct Cell_head *window)
44 {
46 
47  *window = R__.rd_window;
48 }
49 
50 /*!
51  * \brief Read the current output window
52  *
53  * \param window pointer to Cell_head
54  */
55 void Rast_get_output_window(struct Cell_head *window)
56 {
58 
59  *window = R__.wr_window;
60 }
61 
62 /*!
63  * \brief Number of rows in active window.
64  *
65  * This routine returns the number of rows in the active module window.
66  * Before raster files can be read or written, it is necessary to
67  * known how many rows are in the active window. For example:
68  \code
69  int nrows, cols;
70  int row, col;
71 
72  nrows = Rast_window_rows();
73  ncols = Rast_window_cols();
74  for (row = 0; row < nrows; row++) {
75  // read row ...
76  for (col = 0; col < ncols; col++) {
77  // process col ...
78  }
79  }
80  \endcode
81  *
82  * \return number of rows
83  */
85 {
87 
88  if (R__.split_window)
90  _("Internal error: Rast_window_rows() called with split window."
91  " Use Rast_input_window_rows() or Rast_output_window_rows() "
92  "instead."));
93 
94  return R__.wr_window.rows;
95 }
96 
97 /*!
98  * \brief Number of columns in active window.
99  *
100  * These routines return the number of rows and columns (respectively)
101  * in the active module region. Before raster maps can be read or
102  * written, it is necessary to known how many rows and columns are in
103  * the active region. For example:
104  *
105  \code
106  int nrows, cols;
107  int row, col;
108 
109  nrows = Rast_window_rows();
110  ncols = Rast_window_cols();
111  for (row = 0; row < nrows; row++) {
112  // read row ...
113  for (col = 0; col < ncols; col++) {
114  // process col ...
115  }
116  }
117  \endcode
118  *
119  * \return number of columns
120  */
122 {
124 
125  if (R__.split_window)
127  _("Internal error: Rast_window_cols() called with split window."
128  " Use Rast_input_window_cols() or Rast_output_window_cols() "
129  "instead."));
130 
131  return R__.wr_window.cols;
132 }
133 
134 /*!
135  * \brief Number of rows in active input window.
136  *
137  * This routine returns the number of rows in the active input window.
138  *
139  * \return number of rows
140  */
142 {
144 
145  return R__.rd_window.rows;
146 }
147 
148 /*!
149  * \brief Number of columns in active input window.
150  *
151  * This routine returns the number of columns in the active input window.
152  *
153  * \return number of columns
154  */
156 {
158 
159  return R__.rd_window.cols;
160 }
161 
162 /*!
163  * \brief Number of rows in active output window.
164  *
165  * This routine returns the number of rows in the active output window.
166  *
167  * \return number of rows
168  */
170 {
172 
173  return R__.wr_window.rows;
174 }
175 
176 /*!
177  * \brief Number of columns in active output window.
178  *
179  * This routine returns the number of columns in the active output window.
180  *
181  * \return number of columns
182  */
184 {
186 
187  return R__.wr_window.cols;
188 }
189 
190 /*!
191  * \brief Northing to row.
192  *
193  * Converts a <i>north</i>ing relative to a <i>window</i> to a row.
194 
195  * <b>Note:</b> The result is a double. Casting it to an integer will
196  * give the row number.
197  *
198  * \param north northing value
199  * \param window pointer to Cell_head
200  *
201  * \return row number
202  */
203 double Rast_northing_to_row(double north, const struct Cell_head *window)
204 {
205  return (window->north - north) / window->ns_res;
206 }
207 
208 /*!
209  * \brief Easting to column.
210  *
211  * Converts <i>east</i> relative to a <i>window</i> to a column.
212 
213  * <b>Note:</b> The result is a <i>double</i>. Casting it to an
214  * <i>int</i> will give the column number.
215  *
216  * \param east east coordinate
217  * \param window pointer to Cell_head
218  *
219  * \return column number
220  */
221 double Rast_easting_to_col(double east, const struct Cell_head *window)
222 {
223  east = G_adjust_easting(east, window);
224 
225  return (east - window->west) / window->ew_res;
226 }
227 
228 /*!
229  * \brief Row to northing.
230  *
231  * Converts a <i>row</i> relative to a <i>window</i> to a
232  * northing.
233 
234  * <b>Note:</b> row is a double:
235  * - row+0.0 will return the northing for the northern edge of the row.
236  * - row+0.5 will return the northing for the center of the row.
237  * - row+1.0 will return the northing for the southern edge of the row.
238  *
239  * \param row row number
240  * \param[in] window pointer to Cell_head
241  *
242  * \return north coordinate
243  */
244 double Rast_row_to_northing(double row, const struct Cell_head *window)
245 {
246  return window->north - row * window->ns_res;
247 }
248 
249 /*!
250  * \brief Column to easting.
251  *
252  * Converts a <i>col</i> relative to a <i>window</i> to an easting.
253  *
254  * <b>Note:</b> <i>col</i> is a <i>double</i>:
255  * - col+0.0 will return the easting for the western edge of the column.
256  * - col+0.5 will return the easting for the center of the column.
257  * - col+1.0 will return the easting for the eastern edge of the column.
258  *
259  * \param col column number
260  * \param[in] window pointer to Cell_head
261  *
262  * \return east coordinate
263  */
264 double Rast_col_to_easting(double col, const struct Cell_head *window)
265 {
266  return window->west + col * window->ew_res;
267 }
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
double G_adjust_easting(double, const struct Cell_head *)
Returns east not smaller than west.
void Rast__init_window(void)
#define _(str)
Definition: glocale.h:10
int Rast_output_window_rows(void)
Number of rows in active output window.
double Rast_row_to_northing(double row, const struct Cell_head *window)
Row to northing.
double Rast_col_to_easting(double col, const struct Cell_head *window)
Column to easting.
void Rast_get_output_window(struct Cell_head *window)
Read the current output window.
Definition: raster/window.c:55
int Rast_input_window_rows(void)
Number of rows in active input window.
double Rast_easting_to_col(double east, const struct Cell_head *window)
Easting to column.
void Rast_get_input_window(struct Cell_head *window)
Read the current input window.
Definition: raster/window.c:43
void Rast_get_window(struct Cell_head *window)
Read the current window.
Definition: raster/window.c:25
int Rast_input_window_cols(void)
Number of columns in active input window.
int Rast_window_cols(void)
Number of columns in active window.
int Rast_window_rows(void)
Number of rows in active window.
Definition: raster/window.c:84
int Rast_output_window_cols(void)
Number of columns in active output window.
double Rast_northing_to_row(double north, const struct Cell_head *window)
Northing to row.
2D/3D raster map header (used also for region)
Definition: gis.h:446
double ew_res
Resolution - east to west cell size for 2D data.
Definition: gis.h:482
double north
Extent coordinates (north)
Definition: gis.h:492
double ns_res
Resolution - north to south cell size for 2D data.
Definition: gis.h:486
int rows
Number of rows for 2D data.
Definition: gis.h:461
int cols
Number of columns for 2D data.
Definition: gis.h:465
double west
Extent coordinates (west)
Definition: gis.h:498
Definition: R.h:87
int split_window
Definition: R.h:96
struct Cell_head wr_window
Definition: R.h:98
struct Cell_head rd_window
Definition: R.h:97