get_window.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*!
  2. \file lib/gis/get_window.c
  3. \brief GIS Library - Get window (i.e. GRASS region)
  4. (C) 2001-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Original author CERL
  8. */
  9. #include <stdlib.h>
  10. #include <grass/gis.h>
  11. #include <grass/glocale.h>
  12. #include "G.h"
  13. #include "gis_local_proto.h"
  14. static struct state {
  15. int initialized;
  16. struct Cell_head dbwindow;
  17. } state;
  18. static struct state *st = &state;
  19. /*!
  20. \brief Get the current region
  21. Reads the region as stored in the WIND file in the user's current
  22. mapset into region.
  23. 3D values are set to defaults if not available in WIND file. An
  24. error message is printed and exit() is called if there is a problem
  25. reading the region.
  26. <b>Note:</b> GRASS applications that read or write raster maps
  27. should not use this routine since its use implies that the active
  28. module region will not be used. Programs that read or write raster
  29. map data (or vector data) can query the active module region using
  30. Rast_window_rows() and Rast_window_cols().
  31. \param[out] window pointer to Cell_head
  32. */
  33. void G_get_window(struct Cell_head *window)
  34. {
  35. const char *regvar;
  36. if (G_is_initialized(&st->initialized)) {
  37. *window = st->dbwindow;
  38. return;
  39. }
  40. /* Optionally read the region from environment variable */
  41. regvar = getenv("GRASS_REGION");
  42. if (regvar) {
  43. char **tokens = G_tokenize(regvar, ";");
  44. G__read_Cell_head_array(tokens, &st->dbwindow, 0);
  45. G_free_tokens(tokens);
  46. }
  47. else {
  48. char *wind = getenv("WIND_OVERRIDE");
  49. if (wind)
  50. G_get_element_window(&st->dbwindow, "windows", wind, G_mapset());
  51. else
  52. G_get_element_window(&st->dbwindow, "", "WIND", G_mapset());
  53. }
  54. *window = st->dbwindow;
  55. if (!G__.window_set) {
  56. G__.window_set = 1;
  57. G__.window = st->dbwindow;
  58. }
  59. G_initialize_done(&st->initialized);
  60. }
  61. /*!
  62. \brief Get the default region
  63. Reads the default region for the location into <i>region.</i> 3D
  64. values are set to defaults if not available in WIND file.
  65. An error message is printed and exit() is called if there is a
  66. problem reading the default region.
  67. \param[out] window pointer to Cell_head
  68. */
  69. void G_get_default_window(struct Cell_head *window)
  70. {
  71. G_get_element_window(window, "", "DEFAULT_WIND", "PERMANENT");
  72. }
  73. /*!
  74. \brief Get region for selected element (raster, vector, window, etc.)
  75. G_fatal_error() is called on error
  76. \param[out] window pointer to Cell_head
  77. \param element element type
  78. \param name element name
  79. \param mapset mapset name
  80. */
  81. void G_get_element_window(struct Cell_head *window,
  82. const char *element, const char *name, const char *mapset)
  83. {
  84. FILE *fp;
  85. G_zero(window, sizeof(struct Cell_head));
  86. /* Read from file */
  87. fp = G_fopen_old(element, name, mapset);
  88. if (!fp)
  89. G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
  90. element, name, mapset);
  91. G_fseek(fp, 0, SEEK_END);
  92. if (!G_ftell(fp))
  93. G_fatal_error(_("Region file %s/%s/%s is empty"), mapset, element, name);
  94. G_fseek(fp, 0, SEEK_SET);
  95. G__read_Cell_head(fp, window, 0);
  96. fclose(fp);
  97. }
  98. /*!
  99. \brief Unset current region
  100. */
  101. void G_unset_window()
  102. {
  103. st->initialized = 0;
  104. G__.window_set = 0;
  105. }