source: grass/trunk/lib/gis/get_window.c@ 69586

Last change on this file since 69586 was 65962, checked in by neteler, 9 years ago

libgis: catch empty region file (suggested in https://lists.osgeo.org/pipermail/grass-dev/2015-August/075933.html)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id
  • Property svn:mime-type set to text/x-csrc
File size: 3.3 KB
Line 
1/*!
2 \file lib/gis/get_window.c
3
4 \brief GIS Library - Get window (i.e. GRASS region)
5
6 (C) 2001-2009, 2011 by the GRASS Development Team
7
8 This program is free software under the GNU General Public License
9 (>=v2). Read the file COPYING that comes with GRASS for details.
10
11 \author Original author CERL
12*/
13
14#include <stdlib.h>
15
16#include <grass/gis.h>
17#include <grass/glocale.h>
18
19#include "G.h"
20#include "gis_local_proto.h"
21
22static struct state {
23 int initialized;
24 struct Cell_head dbwindow;
25} state;
26
27static struct state *st = &state;
28
29/*!
30 \brief Get the current region
31
32 Reads the region as stored in the WIND file in the user's current
33 mapset into region.
34
35 3D values are set to defaults if not available in WIND file. An
36 error message is printed and exit() is called if there is a problem
37 reading the region.
38
39 <b>Note:</b> GRASS applications that read or write raster maps
40 should not use this routine since its use implies that the active
41 module region will not be used. Programs that read or write raster
42 map data (or vector data) can query the active module region using
43 Rast_window_rows() and Rast_window_cols().
44
45 \param[out] window pointer to Cell_head
46*/
47void G_get_window(struct Cell_head *window)
48{
49 const char *regvar;
50
51 if (G_is_initialized(&st->initialized)) {
52 *window = st->dbwindow;
53 return;
54 }
55
56 /* Optionally read the region from environment variable */
57 regvar = getenv("GRASS_REGION");
58
59 if (regvar) {
60 char **tokens = G_tokenize(regvar, ";");
61 G__read_Cell_head_array(tokens, &st->dbwindow, 0);
62 G_free_tokens(tokens);
63 }
64 else {
65 char *wind = getenv("WIND_OVERRIDE");
66 if (wind)
67 G_get_element_window(&st->dbwindow, "windows", wind, G_mapset());
68 else
69 G_get_element_window(&st->dbwindow, "", "WIND", G_mapset());
70 }
71
72 *window = st->dbwindow;
73
74 if (!G__.window_set) {
75 G__.window_set = 1;
76 G__.window = st->dbwindow;
77 }
78
79 G_initialize_done(&st->initialized);
80}
81
82/*!
83 \brief Get the default region
84
85 Reads the default region for the location into <i>region.</i> 3D
86 values are set to defaults if not available in WIND file.
87
88 An error message is printed and exit() is called if there is a
89 problem reading the default region.
90
91 \param[out] window pointer to Cell_head
92*/
93void G_get_default_window(struct Cell_head *window)
94{
95 G_get_element_window(window, "", "DEFAULT_WIND", "PERMANENT");
96}
97
98/*!
99 \brief Get region for selected element (raster, vector, window, etc.)
100
101 G_fatal_error() is called on error
102
103 \param[out] window pointer to Cell_head
104 \param element element type
105 \param name element name
106 \param mapset mapset name
107*/
108void G_get_element_window(struct Cell_head *window,
109 const char *element, const char *name, const char *mapset)
110{
111 FILE *fp;
112
113 G_zero(window, sizeof(struct Cell_head));
114
115 /* Read from file */
116 fp = G_fopen_old(element, name, mapset);
117 if (!fp)
118 G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
119 element, name, mapset);
120
121 G_fseek(fp, 0, SEEK_END);
122 if (!G_ftell(fp))
123 G_fatal_error(_("Region file %s/%s/%s is empty"), mapset, element, name);
124 G_fseek(fp, 0, SEEK_SET);
125 G__read_Cell_head(fp, window, 0);
126 fclose(fp);
127}
128
129/*!
130 \brief Unset current region
131*/
132void G_unset_window()
133{
134 st->initialized = 0;
135 G__.window_set = 0;
136}
Note: See TracBrowser for help on using the repository browser.