GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
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 SPDX-FileCopyrightText: 2001-2009, 2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <stdlib.h>
13
14#include <grass/gis.h>
15#include <grass/glocale.h>
16
17#include "G.h"
18#include "gis_local_proto.h"
19
20static struct state {
21 int initialized;
22 struct Cell_head dbwindow;
23} state;
24
25static struct state *st = &state;
26
27/*!
28 \brief Get the current region
29
30 Reads the region as stored in the WIND file in the user's current
31 mapset into region.
32
33 3D values are set to defaults if not available in WIND file. An
34 error message is printed and exit() is called if there is a problem
35 reading the region.
36
37 <b>Note:</b> GRASS applications that read or write raster maps
38 should not use this routine since its use implies that the active
39 module region will not be used. Programs that read or write raster
40 map data (or vector data) can query the active module region using
41 Rast_window_rows() and Rast_window_cols().
42
43 \param[out] window pointer to Cell_head
44 */
45void G_get_window(struct Cell_head *window)
46{
47 const char *regvar;
48
49 if (G_is_initialized(&st->initialized)) {
50 *window = st->dbwindow;
51 return;
52 }
53
54 /* Optionally read the region from environment variable */
55 regvar = getenv("GRASS_REGION");
56
57 if (regvar) {
58 char **tokens = G_tokenize(regvar, ";");
59
60 G__read_Cell_head_array(tokens, &st->dbwindow);
62 }
63 else {
64 char *wind = getenv("WIND_OVERRIDE");
65
66 if (wind) {
67 char wind_env[GNAME_MAX] = {0};
68 snprintf(wind_env, GNAME_MAX, "%s", wind);
69 G_get_element_window(&st->dbwindow, "windows", wind_env,
70 G_mapset());
71 }
72 else
73 G_get_element_window(&st->dbwindow, "", "WIND", G_mapset());
74 }
75
76 *window = st->dbwindow;
77
78 if (!G__.window_set) {
79 G__.window_set = 1;
80 G__.window = st->dbwindow;
81 }
82
83 G_initialize_done(&st->initialized);
84}
85
86/*!
87 \brief Get the default region
88
89 Reads the default region for the location into <i>region.</i> 3D
90 values are set to defaults if not available in WIND file.
91
92 An error message is printed and exit() is called if there is a
93 problem reading the default region.
94
95 \param[out] window pointer to Cell_head
96 */
97void G_get_default_window(struct Cell_head *window)
98{
99 G_get_element_window(window, "", "DEFAULT_WIND", "PERMANENT");
100}
101
102/*!
103 \brief Get region for selected element (raster, vector, window, etc.)
104
105 G_fatal_error() is called on error
106
107 \param[out] window pointer to Cell_head
108 \param element element type
109 \param name element name
110 \param mapset mapset name
111 */
112void G_get_element_window(struct Cell_head *window, const char *element,
113 const char *name, const char *mapset)
114{
115 FILE *fp;
116
117 G_zero(window, sizeof(struct Cell_head));
118
119 /* Read from file */
120 fp = G_fopen_old(element, name, mapset);
121 if (!fp)
122 G_fatal_error(_("Unable to open element file <%s> for <%s@%s>"),
123 element, name, mapset);
124
125 G_fseek(fp, 0, SEEK_END);
126 if (!G_ftell(fp))
127 G_fatal_error(_("Region file %s/%s/%s is empty"), mapset, element,
128 name);
129 G_fseek(fp, 0, SEEK_SET);
130 G__read_Cell_head(fp, window);
131 fclose(fp);
132}
133
134/*!
135 \brief Unset current region
136 */
138{
139 st->initialized = 0;
140 G__.window_set = 0;
141}
void G__read_Cell_head_array(char **, struct Cell_head *)
Read window from NULL terminated array of strings (for internal use only)
Definition rd_cellhd.c:94
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition gis/zero.c:21
void G__read_Cell_head(FILE *, struct Cell_head *)
Read cell header (for internal use only)
Definition rd_cellhd.c:54
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
void G_fseek(FILE *, off_t, int)
Change the file position of the stream.
Definition gis/seek.c:48
FILE * G_fopen_old(const char *, const char *, const char *)
Open a database file for reading.
Definition gis/open.c:250
char ** G_tokenize(const char *, const char *)
Tokenize string.
Definition gis/token.c:45
off_t G_ftell(FILE *)
Get the current file position of the stream.
Definition gis/seek.c:27
void G_free_tokens(char **)
Free memory allocated to tokens.
Definition gis/token.c:195
int G_is_initialized(int *)
Definition counter.c:60
void G_initialize_done(int *)
Definition counter.c:77
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
void G_get_window(struct Cell_head *window)
Get the current region.
Definition get_window.c:45
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:112
void G_unset_window(void)
Unset current region.
Definition get_window.c:137
void G_get_default_window(struct Cell_head *window)
Get the default region.
Definition get_window.c:97
#define GNAME_MAX
Definition gis.h:193
#define _(str)
Definition glocale.h:10
const char * name
Definition named_colr.c:6
2D/3D raster map header (used also for region)
Definition gis.h:443
Definition G.h:11
int window_set
Definition G.h:13
struct Cell_head window
Definition G.h:12