get_window.c 3.3 KB

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