get_window.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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, *err;
  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. err = 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. err = G__get_window(&st->dbwindow, "windows", wind, G_mapset());
  54. else
  55. err = G__get_window(&st->dbwindow, "", "WIND", G_mapset());
  56. }
  57. if (err)
  58. G_fatal_error(_("Region for current mapset %s. "
  59. "Run \"g.region\" to fix the current region."), err);
  60. *window = st->dbwindow;
  61. if (!G__.window_set) {
  62. G__.window_set = 1;
  63. G__.window = st->dbwindow;
  64. }
  65. G_initialize_done(&st->initialized);
  66. }
  67. /*!
  68. * \brief Read the default region
  69. *
  70. * Reads the default region for the location into <i>region.</i> 3D
  71. * values are set to defaults if not available in WIND file.
  72. *
  73. * An error message is printed and exit() is called if there is a
  74. * problem reading the default region.
  75. *
  76. * \param[out] window pointer to Cell_head
  77. */
  78. void G_get_default_window(struct Cell_head *window)
  79. {
  80. const char *err = G__get_window(window, "", "DEFAULT_WIND", "PERMANENT");
  81. if (err)
  82. G_fatal_error(_("Default region %s"), err);
  83. }
  84. /*!
  85. \brief Get cwindow (region) of selected map layer
  86. \param window pointer to Cell_head
  87. \param element element name
  88. \param name map name
  89. \param mapset mapset name
  90. \return string on error
  91. \return NULL on success
  92. */
  93. char *G__get_window(struct Cell_head *window,
  94. const char *element, const char *name, const char *mapset)
  95. {
  96. FILE *fp;
  97. char *err;
  98. G_zero(window, sizeof(struct Cell_head));
  99. /* Read from file */
  100. fp = G_fopen_old(element, name, mapset);
  101. if (!fp)
  102. return G_store(_("is not set"));
  103. err = G__read_Cell_head(fp, window, 0);
  104. fclose(fp);
  105. if (err) {
  106. char msg[1024];
  107. sprintf(msg, _("is invalid\n%s"), err);
  108. G_free(err);
  109. return G_store(msg);
  110. }
  111. return NULL;
  112. }