GRASS 8 Programmer's Manual
8.5.0(2026)-8d6ceba290
Toggle main menu visibility
Loading...
Searching...
No Matches
get_window.c
Go to the documentation of this file.
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
22
static
struct
state {
23
int
initialized;
24
struct
Cell_head dbwindow;
25
} state;
26
27
static
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
*/
47
void
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
62
G__read_Cell_head_array
(tokens, &st->dbwindow);
63
G_free_tokens
(tokens);
64
}
65
else
{
66
char
*wind = getenv(
"WIND_OVERRIDE"
);
67
68
if
(wind) {
69
char
wind_env[GNAME_MAX] = {0};
70
snprintf(wind_env, GNAME_MAX,
"%s"
, wind);
71
G_get_element_window
(&st->dbwindow,
"windows"
, wind_env,
72
G_mapset
());
73
}
74
else
75
G_get_element_window
(&st->dbwindow,
""
,
"WIND"
,
G_mapset
());
76
}
77
78
*window = st->dbwindow;
79
80
if
(!
G__
.
window_set
) {
81
G__
.
window_set
= 1;
82
G__
.
window
= st->dbwindow;
83
}
84
85
G_initialize_done
(&st->initialized);
86
}
87
88
/*!
89
\brief Get the default region
90
91
Reads the default region for the location into <i>region.</i> 3D
92
values are set to defaults if not available in WIND file.
93
94
An error message is printed and exit() is called if there is a
95
problem reading the default region.
96
97
\param[out] window pointer to Cell_head
98
*/
99
void
G_get_default_window
(
struct
Cell_head *window)
100
{
101
G_get_element_window
(window,
""
,
"DEFAULT_WIND"
,
"PERMANENT"
);
102
}
103
104
/*!
105
\brief Get region for selected element (raster, vector, window, etc.)
106
107
G_fatal_error() is called on error
108
109
\param[out] window pointer to Cell_head
110
\param element element type
111
\param name element name
112
\param mapset mapset name
113
*/
114
void
G_get_element_window
(
struct
Cell_head *window,
const
char
*
element
,
115
const
char
*
name
,
const
char
*mapset)
116
{
117
FILE *fp;
118
119
G_zero
(window,
sizeof
(
struct
Cell_head));
120
121
/* Read from file */
122
fp =
G_fopen_old
(
element
,
name
, mapset);
123
if
(!fp)
124
G_fatal_error
(_(
"Unable to open element file <%s> for <%s@%s>"
),
125
element
,
name
, mapset);
126
127
G_fseek(fp, 0, SEEK_END);
128
if
(!
G_ftell
(fp))
129
G_fatal_error
(_(
"Region file %s/%s/%s is empty"
), mapset,
element
,
130
name
);
131
G_fseek(fp, 0, SEEK_SET);
132
G__read_Cell_head
(fp, window);
133
fclose(fp);
134
}
135
136
/*!
137
\brief Unset current region
138
*/
139
void
G_unset_window
(
void
)
140
{
141
st->initialized = 0;
142
G__
.
window_set
= 0;
143
}
G.h
G_initialize_done
void G_initialize_done(int *p)
Definition
counter.c:77
G_is_initialized
int G_is_initialized(int *p)
Definition
counter.c:60
G_get_window
void G_get_window(struct Cell_head *window)
Get the current region.
Definition
get_window.c:47
G_get_element_window
void G_get_element_window(struct Cell_head *window, const char *element, const char *name, const char *mapset)
Get region for selected element (raster, vector, window, etc.).
Definition
get_window.c:114
G_unset_window
void G_unset_window(void)
Unset current region.
Definition
get_window.c:139
G_get_default_window
void G_get_default_window(struct Cell_head *window)
Get the default region.
Definition
get_window.c:99
G_fatal_error
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition
gis/error.c:159
G_fopen_old
FILE * G_fopen_old(const char *element, const char *name, const char *mapset)
Open a database file for reading.
Definition
gis/open.c:253
G_ftell
off_t G_ftell(FILE *fp)
Get the current file position of the stream.
Definition
gis/seek.c:29
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition
mapset.c:33
name
const char * name
Definition
named_colr.c:6
G__read_Cell_head_array
void G__read_Cell_head_array(char **array, struct Cell_head *cellhd)
Read window from NULL terminated array of strings (for internal use only).
Definition
rd_cellhd.c:96
G__read_Cell_head
void G__read_Cell_head(FILE *fd, struct Cell_head *cellhd)
Read cell header (for internal use only).
Definition
rd_cellhd.c:56
G__
Definition
G.h:11
G__::window_set
int window_set
Definition
G.h:13
G__::window
struct Cell_head window
Definition
G.h:12
element
Definition
lidar.h:85
G_free_tokens
void G_free_tokens(char **tokens)
Free memory allocated to tokens.
Definition
token.c:197
G_tokenize
char ** G_tokenize(const char *buf, const char *delim)
Tokenize string.
Definition
token.c:47
G_zero
void G_zero(void *buf, int i)
Zero out a buffer, buf, of length i.
Definition
zero.c:23
gis
get_window.c
Generated on
for GRASS 8 Programmer's Manual by
1.17.0