get_window.c 3.4 KB

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