set_window.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*!
  2. * \file lib/raster/set_window.c
  3. *
  4. * \brief Raster Library - Set window (map region)
  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 "../gis/G.h"
  17. #include "R.h"
  18. static void update_window_mappings(void);
  19. static void check_write_window(void);
  20. void Rast__init_window(void)
  21. {
  22. if (G_is_initialized(&R__.window_set))
  23. return;
  24. G__init_window();
  25. R__.rd_window = G__.window;
  26. R__.wr_window = G__.window;
  27. R__.split_window = 0;
  28. G_initialize_done(&R__.window_set);
  29. }
  30. /*!
  31. * \brief Establishes 'window' as the current working window.
  32. *
  33. * \param window window to become operative window
  34. */
  35. void Rast_set_window(struct Cell_head *window)
  36. {
  37. Rast__init();
  38. if (R__.split_window)
  39. G_warning(_("Rast_set_window() called while window split"));
  40. check_write_window();
  41. G_adjust_Cell_head(window, 0, 0);
  42. R__.wr_window = *window;
  43. R__.rd_window = *window;
  44. R__.split_window = 0;
  45. update_window_mappings();
  46. }
  47. /*!
  48. \brief Unset current window
  49. */
  50. void Rast_unset_window(void)
  51. {
  52. G_debug(4, "Rast_unset_window()");
  53. R__.window_set = 0;
  54. }
  55. /*!
  56. * \brief Establishes 'window' as the current working window for output.
  57. *
  58. * \param window window to become operative window
  59. */
  60. void Rast_set_output_window(struct Cell_head *window)
  61. {
  62. Rast__init();
  63. check_write_window();
  64. G_adjust_Cell_head(window, 0, 0);
  65. R__.wr_window = *window;
  66. R__.split_window = 1;
  67. G_set_window(window);
  68. }
  69. /*!
  70. * \brief Establishes 'window' as the current working window for input.
  71. *
  72. * Any opened cell files has its file-to-window mapping reworked.
  73. *
  74. * \param window window to become operative window
  75. */
  76. void Rast_set_input_window(struct Cell_head *window)
  77. {
  78. Rast__init();
  79. G_adjust_Cell_head(window, 0, 0);
  80. R__.rd_window = *window;
  81. R__.split_window = 1;
  82. update_window_mappings();
  83. }
  84. static void update_window_mappings(void)
  85. {
  86. int i;
  87. int maskfd;
  88. /* adjust window, check for valid window */
  89. /* adjust the real one, not a copy
  90. G_copy (&twindow, window, sizeof(struct Cell_head));
  91. window = &twindow;
  92. */
  93. /* except for MASK, cell files open for read must have same projection
  94. * and zone as new window
  95. */
  96. maskfd = R__.auto_mask > 0 ? R__.mask_fd : -1;
  97. for (i = 0; i < R__.fileinfo_count; i++) {
  98. struct fileinfo *fcb = &R__.fileinfo[i];
  99. if (fcb->open_mode == OPEN_OLD) {
  100. if (fcb->cellhd.zone == R__.rd_window.zone &&
  101. fcb->cellhd.proj == R__.rd_window.proj)
  102. continue;
  103. if (i != maskfd)
  104. G_fatal_error(_("Rast_set_read_window(): projection/zone differs from that of "
  105. "currently open raster maps"));
  106. }
  107. }
  108. /* close the mask */
  109. if (R__.auto_mask > 0) {
  110. Rast_close(maskfd);
  111. /* G_free (R__.mask_buf); */
  112. R__.mask_fd = -1;
  113. R__.auto_mask = -1; /* turn off masking */
  114. }
  115. /* now for each possible open cell file, recreate the window mapping */
  116. /*
  117. * also the memory for reading and writing must be reallocated for all opened
  118. * cell files
  119. */
  120. for (i = 0; i < R__.fileinfo_count; i++) {
  121. struct fileinfo *fcb = &R__.fileinfo[i];
  122. if (fcb->open_mode != OPEN_OLD &&
  123. fcb->open_mode != OPEN_NEW_UNCOMPRESSED &&
  124. fcb->open_mode != OPEN_NEW_COMPRESSED)
  125. continue;
  126. if (fcb->open_mode == OPEN_OLD)
  127. G_fatal_error(_("Input window changed while maps are open for read. Map name <%s>"), fcb->name);
  128. }
  129. /* turn masking (back) on if necessary */
  130. Rast__check_for_auto_masking();
  131. }
  132. static void check_write_window(void)
  133. {
  134. int i;
  135. for (i = 0; i < R__.fileinfo_count; i++) {
  136. struct fileinfo *fcb = &R__.fileinfo[i];
  137. if (fcb->open_mode == OPEN_NEW_UNCOMPRESSED ||
  138. fcb->open_mode == OPEN_NEW_COMPRESSED)
  139. G_fatal_error(_("Output window changed while maps are open for write. Map name <%s>"), fcb->name);
  140. }
  141. }