window.c 5.6 KB

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