get_window.c 3.0 KB

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