window.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #include <grass/gis.h>
  14. #include <grass/raster.h>
  15. #include <grass/glocale.h>
  16. #include "R.h"
  17. /*!
  18. * \brief Read the current window
  19. *
  20. * \param window pointer to Cell_head
  21. */
  22. void Rast_get_window(struct Cell_head *window)
  23. {
  24. Rast__init_window();
  25. if (R__.split_window)
  26. G_fatal_error(_("Internal error: Rast_get_window() called with split window."
  27. " Use Rast_get_input_window() or Rast_get_output_window() instead."));
  28. *window = R__.wr_window;
  29. }
  30. /*!
  31. * \brief Read the current input window
  32. *
  33. * \param window pointer to Cell_head
  34. */
  35. void Rast_get_input_window(struct Cell_head *window)
  36. {
  37. Rast__init_window();
  38. *window = R__.rd_window;
  39. }
  40. /*!
  41. * \brief Read the current output window
  42. *
  43. * \param window pointer to Cell_head
  44. */
  45. void Rast_get_output_window(struct Cell_head *window)
  46. {
  47. Rast__init_window();
  48. *window = R__.wr_window;
  49. }
  50. /*!
  51. * \brief Number of rows in active window.
  52. *
  53. * This routine returns the number of rows in the active module window.
  54. * Before raster files can be read or written, it is necessary to
  55. * known how many rows are in the active window. For example:
  56. \code
  57. int nrows, cols;
  58. int row, col;
  59. nrows = Rast_window_rows();
  60. ncols = Rast_window_cols();
  61. for (row = 0; row < nrows; row++) {
  62. // read row ...
  63. for (col = 0; col < ncols; col++) {
  64. // process col ...
  65. }
  66. }
  67. \endcode
  68. *
  69. * \return number of rows
  70. */
  71. int Rast_window_rows(void)
  72. {
  73. Rast__init_window();
  74. if (R__.split_window)
  75. G_fatal_error(_("Internal error: Rast_window_rows() called with split window."
  76. " Use Rast_input_window_rows() or Rast_output_window_rows() instead."));
  77. return R__.wr_window.rows;
  78. }
  79. /*!
  80. * \brief Number of columns in active window.
  81. *
  82. * These routines return the number of rows and columns (respectively)
  83. * in the active module region. Before raster maps can be read or
  84. * written, it is necessary to known how many rows and columns are in
  85. * the active region. For example:
  86. *
  87. \code
  88. int nrows, cols;
  89. int row, col;
  90. nrows = Rast_window_rows();
  91. ncols = Rast_window_cols();
  92. for (row = 0; row < nrows; row++) {
  93. // read row ...
  94. for (col = 0; col < ncols; col++) {
  95. // process col ...
  96. }
  97. }
  98. \endcode
  99. *
  100. * \return number of columns
  101. */
  102. int Rast_window_cols(void)
  103. {
  104. Rast__init_window();
  105. if (R__.split_window)
  106. G_fatal_error(_("Internal error: Rast_window_cols() called with split window."
  107. " Use Rast_input_window_cols() or Rast_output_window_cols() instead."));
  108. return R__.wr_window.cols;
  109. }
  110. /*!
  111. * \brief Number of rows in active input window.
  112. *
  113. * This routine returns the number of rows in the active input window.
  114. *
  115. * \return number of rows
  116. */
  117. int Rast_input_window_rows(void)
  118. {
  119. Rast__init_window();
  120. return R__.rd_window.rows;
  121. }
  122. /*!
  123. * \brief Number of columns in active input window.
  124. *
  125. * This routine returns the number of columns in the active input window.
  126. *
  127. * \return number of columns
  128. */
  129. int Rast_input_window_cols(void)
  130. {
  131. Rast__init_window();
  132. return R__.rd_window.cols;
  133. }
  134. /*!
  135. * \brief Number of rows in active output window.
  136. *
  137. * This routine returns the number of rows in the active output window.
  138. *
  139. * \return number of rows
  140. */
  141. int Rast_output_window_rows(void)
  142. {
  143. Rast__init_window();
  144. return R__.wr_window.rows;
  145. }
  146. /*!
  147. * \brief Number of columns in active output window.
  148. *
  149. * This routine returns the number of columns in the active output window.
  150. *
  151. * \return number of columns
  152. */
  153. int Rast_output_window_cols(void)
  154. {
  155. Rast__init_window();
  156. return R__.wr_window.cols;
  157. }
  158. /*!
  159. * \brief Northing to row.
  160. *
  161. * Converts a <i>north</i>ing relative to a <i>window</i> to a row.
  162. * <b>Note:</b> The result is a double. Casting it to an integer will
  163. * give the row number.
  164. *
  165. * \param north northing value
  166. * \param window pointer to Cell_head
  167. *
  168. * \return row number
  169. */
  170. double Rast_northing_to_row(double north, const struct Cell_head *window)
  171. {
  172. return (window->north - north) / window->ns_res;
  173. }
  174. /*!
  175. * \brief Easting to column.
  176. *
  177. * Converts <i>east</i> relative to a <i>window</i> to a column.
  178. * <b>Note:</b> The result is a <i>double</i>. Casting it to an
  179. * <i>int</i> will give the column number.
  180. *
  181. * \param east east coordinate
  182. * \param window pointer to Cell_head
  183. *
  184. * \return column number
  185. */
  186. double Rast_easting_to_col(double east, const struct Cell_head *window)
  187. {
  188. east = G_adjust_easting(east, window);
  189. return (east - window->west) / window->ew_res;
  190. }
  191. /*!
  192. * \brief Row to northing.
  193. *
  194. * Converts a <i>row</i> relative to a <i>window</i> to a
  195. * northing.
  196. * <b>Note:</b> row is a double:
  197. * - row+0.0 will return the northing for the northern edge of the row.
  198. * - row+0.5 will return the northing for the center of the row.
  199. * - row+1.0 will return the northing for the southern edge of the row.
  200. *
  201. * \param row row number
  202. * \param[in] window pointer to Cell_head
  203. *
  204. * \return north coordinate
  205. */
  206. double Rast_row_to_northing(double row, const struct Cell_head *window)
  207. {
  208. return window->north - row * window->ns_res;
  209. }
  210. /*!
  211. * \brief Column to easting.
  212. *
  213. * Converts a <i>col</i> relative to a <i>window</i> to an easting.
  214. *
  215. * <b>Note:</b> <i>col</i> is a <i>double</i>:
  216. * - col+0.0 will return the easting for the western edge of the column.
  217. * - col+0.5 will return the easting for the center of the column.
  218. * - col+1.0 will return the easting for the eastern edge of the column.
  219. *
  220. * \param col column number
  221. * \param[in] window pointer to Cell_head
  222. *
  223. * \return east coordinate
  224. */
  225. double Rast_col_to_easting(double col, const struct Cell_head *window)
  226. {
  227. return window->west + col * window->ew_res;
  228. }