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