set_window.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*!
  2. * \file 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 Establishes 'window' as the current working window for output.
  49. *
  50. * \param window window to become operative window
  51. */
  52. void Rast_set_output_window(struct Cell_head *window)
  53. {
  54. Rast__init();
  55. check_write_window();
  56. G_adjust_Cell_head(window, 0, 0);
  57. R__.wr_window = *window;
  58. R__.split_window = 1;
  59. G_set_window(window);
  60. }
  61. /*!
  62. * \brief Establishes 'window' as the current working window for input.
  63. *
  64. * Any opened cell files has its file-to-window mapping reworked.
  65. *
  66. * \param window window to become operative window
  67. */
  68. void Rast_set_input_window(struct Cell_head *window)
  69. {
  70. Rast__init();
  71. G_adjust_Cell_head(window, 0, 0);
  72. R__.rd_window = *window;
  73. R__.split_window = 1;
  74. update_window_mappings();
  75. }
  76. static void update_window_mappings(void)
  77. {
  78. int i;
  79. int maskfd;
  80. /* adjust window, check for valid window */
  81. /* adjust the real one, not a copy
  82. G_copy (&twindow, window, sizeof(struct Cell_head));
  83. window = &twindow;
  84. */
  85. /* except for MASK, cell files open for read must have same projection
  86. * and zone as new window
  87. */
  88. maskfd = R__.auto_mask > 0 ? R__.mask_fd : -1;
  89. for (i = 0; i < R__.fileinfo_count; i++) {
  90. struct fileinfo *fcb = &R__.fileinfo[i];
  91. if (fcb->open_mode == OPEN_OLD) {
  92. if (fcb->cellhd.zone == R__.rd_window.zone &&
  93. fcb->cellhd.proj == R__.rd_window.proj)
  94. continue;
  95. if (i != maskfd)
  96. G_fatal_error(_("Rast_set_read_window(): projection/zone differs from that of "
  97. "currently open raster maps"));
  98. }
  99. }
  100. /* close the mask */
  101. if (R__.auto_mask > 0) {
  102. Rast_close(maskfd);
  103. /* G_free (R__.mask_buf); */
  104. R__.mask_fd = -1;
  105. R__.auto_mask = -1; /* turn off masking */
  106. }
  107. /* now for each possible open cell file, recreate the window mapping */
  108. /*
  109. * also the memory for reading and writing must be reallocated for all opened
  110. * cell files
  111. */
  112. for (i = 0; i < R__.fileinfo_count; i++) {
  113. struct fileinfo *fcb = &R__.fileinfo[i];
  114. if (fcb->open_mode != OPEN_OLD &&
  115. fcb->open_mode != OPEN_NEW_UNCOMPRESSED &&
  116. fcb->open_mode != OPEN_NEW_COMPRESSED)
  117. continue;
  118. if (fcb->open_mode == OPEN_OLD) {
  119. G_fatal_error(_("Input window changed while maps are open for read"));
  120. Rast__create_window_mapping(i);
  121. }
  122. }
  123. /* turn masking (back) on if necessary */
  124. Rast__check_for_auto_masking();
  125. }
  126. static void check_write_window(void)
  127. {
  128. int i;
  129. for (i = 0; i < R__.fileinfo_count; i++) {
  130. struct fileinfo *fcb = &R__.fileinfo[i];
  131. if (fcb->open_mode == OPEN_NEW_UNCOMPRESSED ||
  132. fcb->open_mode == OPEN_NEW_COMPRESSED)
  133. G_fatal_error(_("Output window changed while maps are open for write"));
  134. }
  135. }