get_window.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. static struct state {
  14. int initialized;
  15. struct Cell_head dbwindow;
  16. } state;
  17. static struct state *st = &state;
  18. /*!
  19. \brief Read the database region
  20. Reads the database region as stored in the WIND file in the user's
  21. current mapset into region.
  22. 3D values are set to defaults if not available in WIND file. An
  23. error message is printed and exit() is called if there is a problem
  24. reading the region.
  25. <b>Note:</b> GRASS applications that read or write raster maps
  26. should not use this routine since its use implies that the active
  27. module region will not be used. Programs that read or write raster
  28. map data (or vector data) can query the active module region using
  29. Rast_window_rows() and Rast_window_cols().
  30. \param[out] window pointer to Cell_head
  31. */
  32. void G_get_window(struct Cell_head *window)
  33. {
  34. const char *regvar;
  35. if (G_is_initialized(&st->initialized)) {
  36. *window = st->dbwindow;
  37. return;
  38. }
  39. /* Optionally read the region from environment variable */
  40. regvar = getenv("GRASS_REGION");
  41. if (regvar) {
  42. char **tokens = G_tokenize(regvar, ";");
  43. G__read_Cell_head_array(tokens, &st->dbwindow, 0);
  44. G_free_tokens(tokens);
  45. }
  46. else {
  47. char *wind = getenv("WIND_OVERRIDE");
  48. if (wind)
  49. G__get_window(&st->dbwindow, "windows", wind, G_mapset());
  50. else
  51. G__get_window(&st->dbwindow, "", "WIND", G_mapset());
  52. }
  53. *window = st->dbwindow;
  54. if (!G__.window_set) {
  55. G__.window_set = 1;
  56. G__.window = st->dbwindow;
  57. }
  58. G_initialize_done(&st->initialized);
  59. }
  60. /*!
  61. \brief Read the default region
  62. Reads the default region for the location into <i>region.</i> 3D
  63. values are set to defaults if not available in WIND file.
  64. An error message is printed and exit() is called if there is a
  65. problem reading the default region.
  66. \param[out] window pointer to Cell_head
  67. */
  68. void G_get_default_window(struct Cell_head *window)
  69. {
  70. G__get_window(window, "", "DEFAULT_WIND", "PERMANENT");
  71. }
  72. /*!
  73. \brief Get window (region) of selected map
  74. G_fatal_error() is called on error
  75. \param window pointer to Cell_head
  76. \param element element type
  77. \param name map name
  78. \param mapset mapset name
  79. */
  80. void G__get_window(struct Cell_head *window,
  81. const char *element, const char *name, const char *mapset)
  82. {
  83. FILE *fp;
  84. G_zero(window, sizeof(struct Cell_head));
  85. /* Read from file */
  86. fp = G_fopen_old(element, name, mapset);
  87. if (!fp)
  88. G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
  89. element, name, mapset);
  90. G__read_Cell_head(fp, window, 0);
  91. fclose(fp);
  92. }
  93. /*!
  94. \brief Unset current window
  95. */
  96. void G_unset_window()
  97. {
  98. st->initialized = 0;
  99. G__.window_set = 0;
  100. }