GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <grass/gis.h>
13#include <grass/raster.h>
14#include <grass/glocale.h>
15
16#include "R.h"
17
18/*!
19 * \brief Read the current window
20 *
21 * \param window pointer to Cell_head
22 */
23void Rast_get_window(struct Cell_head *window)
24{
26
27 if (R__.split_window)
29 _("Internal error: Rast_get_window() called with split window."
30 " Use Rast_get_input_window() or Rast_get_output_window() "
31 "instead."));
32
33 *window = R__.wr_window;
34}
35
36/*!
37 * \brief Read the current input window
38 *
39 * \param window pointer to Cell_head
40 */
41void Rast_get_input_window(struct Cell_head *window)
42{
44
45 *window = R__.rd_window;
46}
47
48/*!
49 * \brief Read the current output window
50 *
51 * \param window pointer to Cell_head
52 */
54{
56
57 *window = R__.wr_window;
58}
59
60/*!
61 * \brief Number of rows in active window.
62 *
63 * This routine returns the number of rows in the active module window.
64 * Before raster files can be read or written, it is necessary to
65 * known how many rows are in the active window. For example:
66 \code
67 int nrows, cols;
68 int row, col;
69
70 nrows = Rast_window_rows();
71 ncols = Rast_window_cols();
72 for (row = 0; row < nrows; row++) {
73 // read row ...
74 for (col = 0; col < ncols; col++) {
75 // process col ...
76 }
77 }
78 \endcode
79 *
80 * \return number of rows
81 */
83{
85
86 if (R__.split_window)
88 _("Internal error: Rast_window_rows() called with split window."
89 " Use Rast_input_window_rows() or Rast_output_window_rows() "
90 "instead."));
91
92 return R__.wr_window.rows;
93}
94
95/*!
96 * \brief Number of columns in active window.
97 *
98 * These routines return the number of rows and columns (respectively)
99 * in the active module region. Before raster maps can be read or
100 * written, it is necessary to known how many rows and columns are in
101 * the active region. For example:
102 *
103 \code
104 int nrows, cols;
105 int row, col;
106
107 nrows = Rast_window_rows();
108 ncols = Rast_window_cols();
109 for (row = 0; row < nrows; row++) {
110 // read row ...
111 for (col = 0; col < ncols; col++) {
112 // process col ...
113 }
114 }
115 \endcode
116 *
117 * \return number of columns
118 */
120{
122
123 if (R__.split_window)
125 _("Internal error: Rast_window_cols() called with split window."
126 " Use Rast_input_window_cols() or Rast_output_window_cols() "
127 "instead."));
128
129 return R__.wr_window.cols;
130}
131
132/*!
133 * \brief Number of rows in active input window.
134 *
135 * This routine returns the number of rows in the active input window.
136 *
137 * \return number of rows
138 */
140{
142
143 return R__.rd_window.rows;
144}
145
146/*!
147 * \brief Number of columns in active input window.
148 *
149 * This routine returns the number of columns in the active input window.
150 *
151 * \return number of columns
152 */
154{
156
157 return R__.rd_window.cols;
158}
159
160/*!
161 * \brief Number of rows in active output window.
162 *
163 * This routine returns the number of rows in the active output window.
164 *
165 * \return number of rows
166 */
168{
170
171 return R__.wr_window.rows;
172}
173
174/*!
175 * \brief Number of columns in active output window.
176 *
177 * This routine returns the number of columns in the active output window.
178 *
179 * \return number of columns
180 */
182{
184
185 return R__.wr_window.cols;
186}
187
188/*!
189 * \brief Northing to row.
190 *
191 * Converts a <i>north</i>ing relative to a <i>window</i> to a row.
192
193 * <b>Note:</b> The result is a double. Casting it to an integer will
194 * give the row number.
195 *
196 * \param north northing value
197 * \param window pointer to Cell_head
198 *
199 * \return row number
200 */
201double Rast_northing_to_row(double north, const struct Cell_head *window)
202{
203 return (window->north - north) / window->ns_res;
204}
205
206/*!
207 * \brief Easting to column.
208 *
209 * Converts <i>east</i> relative to a <i>window</i> to a column.
210
211 * <b>Note:</b> The result is a <i>double</i>. Casting it to an
212 * <i>int</i> will give the column number.
213 *
214 * \param east east coordinate
215 * \param window pointer to Cell_head
216 *
217 * \return column number
218 */
219double Rast_easting_to_col(double east, const struct Cell_head *window)
220{
221 east = G_adjust_easting(east, window);
222
223 return (east - window->west) / window->ew_res;
224}
225
226/*!
227 * \brief Row to northing.
228 *
229 * Converts a <i>row</i> relative to a <i>window</i> to a
230 * northing.
231
232 * <b>Note:</b> row is a double:
233 * - row+0.0 will return the northing for the northern edge of the row.
234 * - row+0.5 will return the northing for the center of the row.
235 * - row+1.0 will return the northing for the southern edge of the row.
236 *
237 * \param row row number
238 * \param[in] window pointer to Cell_head
239 *
240 * \return north coordinate
241 */
242double Rast_row_to_northing(double row, const struct Cell_head *window)
243{
244 return window->north - row * window->ns_res;
245}
246
247/*!
248 * \brief Column to easting.
249 *
250 * Converts a <i>col</i> relative to a <i>window</i> to an easting.
251 *
252 * <b>Note:</b> <i>col</i> is a <i>double</i>:
253 * - col+0.0 will return the easting for the western edge of the column.
254 * - col+0.5 will return the easting for the center of the column.
255 * - col+1.0 will return the easting for the eastern edge of the column.
256 *
257 * \param col column number
258 * \param[in] window pointer to Cell_head
259 *
260 * \return east coordinate
261 */
262double Rast_col_to_easting(double col, const struct Cell_head *window)
263{
264 return window->west + col * window->ew_res;
265}
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.
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.
void Rast_get_window(struct Cell_head *window)
Read the current window.
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.
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:443
double ew_res
Resolution - east to west cell size for 2D data.
Definition gis.h:479
double north
Extent coordinates (north)
Definition gis.h:489
double ns_res
Resolution - north to south cell size for 2D data.
Definition gis.h:483
double west
Extent coordinates (west)
Definition gis.h:495
Definition R.h:86
int split_window
Definition R.h:95
struct Cell_head wr_window
Definition R.h:97
struct Cell_head rd_window
Definition R.h:96