GRASS 8 Programmer's Manual  8.5.0dev(2025)-d272f2f102
windowio.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <grass/gis.h>
8 #include "raster3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 static int Rast3d__readWindow(struct Key_Value *windowKeys, int *proj,
13  int *zone, double *north, double *south,
14  double *east, double *west, double *top,
15  double *bottom, int *rows, int *cols, int *depths,
16  double *ew_res, double *ns_res, double *tb_res)
17 {
18  int returnVal;
19 
20  returnVal = 1;
21  returnVal &= Rast3d_key_get_int(windowKeys, RASTER3D_REGION_PROJ, proj);
22  returnVal &= Rast3d_key_get_int(windowKeys, RASTER3D_REGION_ZONE, zone);
23 
24  returnVal &=
25  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_NORTH, north);
26  returnVal &=
27  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_SOUTH, south);
28  returnVal &= Rast3d_key_get_double(windowKeys, RASTER3D_REGION_EAST, east);
29  returnVal &= Rast3d_key_get_double(windowKeys, RASTER3D_REGION_WEST, west);
30  returnVal &= Rast3d_key_get_double(windowKeys, RASTER3D_REGION_TOP, top);
31  returnVal &=
32  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_BOTTOM, bottom);
33 
34  returnVal &= Rast3d_key_get_int(windowKeys, RASTER3D_REGION_ROWS, rows);
35  returnVal &= Rast3d_key_get_int(windowKeys, RASTER3D_REGION_COLS, cols);
36  returnVal &= Rast3d_key_get_int(windowKeys, RASTER3D_REGION_DEPTHS, depths);
37 
38  returnVal &=
39  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_EWRES, ew_res);
40  returnVal &=
41  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_NSRES, ns_res);
42  returnVal &=
43  Rast3d_key_get_double(windowKeys, RASTER3D_REGION_TBRES, tb_res);
44 
45  if (returnVal)
46  return 1;
47 
48  Rast3d_error("Rast3d_readWriteWindow: error writing window");
49  return 0;
50 }
51 
52 /*
53  * If windowName == NULL -> RASTER3D_WINDOW_ELEMENT ("$MAPSET/WIND3")
54  * otherwise RASTER3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME")
55  */
56 static void Rast3d_getFullWindowPath(char *path, const char *windowName)
57 {
58  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
59 
60  if (windowName == NULL) {
62  return;
63  }
64 
65  while (*windowName == ' ')
66  windowName++;
67 
68  if (strchr(windowName, GRASS_DIRSEP) || strchr(windowName, HOST_DIRSEP)) {
69  snprintf(path, GPATH_MAX, "%s", windowName);
70  return;
71  }
72 
73  if (G_name_is_fully_qualified(windowName, xname, xmapset)) {
74  G_file_name(path, RASTER3D_WINDOW_DATABASE, xname, xmapset);
75  return;
76  }
77 
79 }
80 
81 /*---------------------------------------------------------------------------*/
82 /*
83  static void
84  Rast3d_getWindowLocation (path, windowName)
85 
86  char path[1024];
87  char *windowName;
88 
89  {
90  char xname[512], xmapset[512];
91  char *p, *slash;
92 
93  if (windowName == NULL) {
94  G_file_name (path, "", "", G_mapset ());
95  return;
96  }
97 
98  while (*windowName == ' ') windowName++;
99 
100  if ((*windowName != '/') && (*windowName != '.')) {
101  if (G_name_is_fully_qualified (windowName, xname, xmapset))
102  G_file_name (path, RASTER3D_WINDOW_DATABASE, xname, xmapset);
103  else
104  G_file_name (path, RASTER3D_WINDOW_DATABASE, windowName, G_mapset ());
105  } else
106  sprintf (path, "%s", windowName);
107  p = path;
108  slash = NULL;
109  while (*p != 0) {
110  if (*p == '/') slash = p;
111  p++;
112  }
113  if (slash != NULL) *slash = 0;
114  }
115  */
116 
117 /*---------------------------------------------------------------------------*/
118 
119 /*!
120  * \brief
121  *
122  * Reads
123  * <em>window</em> from the file specified by <em>windowName</em>. The name is
124  * converted by the rules defined in window defaults. A NULL pointer indicates
125  * the <em>WIND3</em> file in the current mapset.
126  *
127  * \param window
128  * \param windowName
129  * \return 1 ... if successful
130  * 0 ... otherwise.
131  */
132 int Rast3d_read_window(RASTER3D_Region *window, const char *windowName)
133 {
134  struct Cell_head win;
135  struct Key_Value *windowKeys;
136  char path[GPATH_MAX];
137 
138  if (windowName == NULL) {
139  G_get_window(&win);
140 
141  window->proj = win.proj;
142  window->zone = win.zone;
143  window->north = win.north;
144  window->south = win.south;
145  window->east = win.east;
146  window->west = win.west;
147  window->top = win.top;
148  window->bottom = win.bottom;
149  window->rows = win.rows3;
150  window->cols = win.cols3;
151  window->depths = win.depths;
152  window->ns_res = win.ns_res3;
153  window->ew_res = win.ew_res3;
154  window->tb_res = win.tb_res;
155  }
156  else {
157  Rast3d_getFullWindowPath(path, windowName);
158 
159  if (access(path, R_OK) != 0) {
160  G_warning("Rast3d_read_window: unable to find [%s].", path);
161  return 0;
162  }
163 
164  windowKeys = G_read_key_value_file(path);
165 
166  if (!Rast3d__readWindow(
167  windowKeys, &(window->proj), &(window->zone), &(window->north),
168  &(window->south), &(window->east), &(window->west),
169  &(window->top), &(window->bottom), &(window->rows),
170  &(window->cols), &(window->depths), &(window->ew_res),
171  &(window->ns_res), &(window->tb_res))) {
172  Rast3d_error(
173  "Rast3d_read_window: error extracting window key(s) of file %s",
174  path);
175  return 0;
176  }
177 
178  G_free_key_value(windowKeys);
179  }
180 
181  return 1;
182 }
183 
184 /*---------------------------------------------------------------------------*/
185 /* modified version of G__make_mapset_element */
186 /*
187  static int
188  Rast3d_createPath (thePath)
189 
190  char *thePath;
191 
192  {
193  char command[1024];
194  char *path, *p, *pOld;
195 
196  if (*thePath == 0) return 0;
197 
198  strcpy (path = command, "mkdir ");
199  while (*path) path++;
200  p = path;
201  */
202 /* now append element, one directory at a time, to path */
203 /*
204  while (1) {
205  if (*thePath == '/') *p++ = *thePath++;
206  pOld = p;
207  while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
208  *p = 0;
209 
210  if (p == pOld) return 1;
211 
212  if (access (path, 0) != 0) mkdir (path,0777);
213  if (access (path, 0) != 0) system (command);
214  if (access (path, 0) != 0) {
215  char err[1024];
216  sprintf (err, "can't make mapset element %s (%s)", thePath, path);
217  G_fatal_error (err);
218  exit(1);
219  }
220  }
221  }
222  */
223 
224 /*---------------------------------------------------------------------------*/
225 
226 /*!
227  * \brief
228  *
229  *
230  * Writes <em>window</em> to the file specified by <em>windowName</em>. The name
231  * is converted by the rules defined in window defaults. A NULL pointer
232  * indicates the <em>WIND3</em> file in the current mapset.
233  *
234  * \param window
235  * \param windowName
236  * \return 1 ... if successful
237  * 0 ... otherwise.
238  */
239 
240 /*
241  int
242  Rast3d_writeWindow (window, windowName)
243 
244  RASTER3D_Region *window;
245  char *windowName;
246 
247  {
248  return 0;
249  }
250  */
251 
252 /*---------------------------------------------------------------------------*/
253 
254 /*!
255  * \brief
256  *
257  * Allows the window to be set at run-time via the <em>region3</em>
258  * command line argument. This function has to be called before
259  * <em>G_parser ()</em>. See also window defaults.
260  *
261  * \return void
262  */
264 {
266 }
#define NULL
Definition: ccmath.h:32
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
void G_warning(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
void G_free_key_value(struct Key_Value *)
Free allocated Key_Value structure.
Definition: key_value1.c:104
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
struct Key_Value * G_read_key_value_file(const char *)
Read key/values pairs from file.
Definition: key_value3.c:55
void G_get_window(struct Cell_head *)
Get the current region.
Definition: get_window.c:47
int Rast3d_key_get_int(struct Key_Value *, const char *, int *)
Definition: keys.c:7
void Rast3d_set_window_params(void)
Definition: param.c:120
int Rast3d_key_get_double(struct Key_Value *, const char *, double *)
Definition: keys.c:27
void Rast3d_error(const char *,...) __attribute__((format(printf
#define GMAPSET_MAX
Definition: gis.h:197
#define GPATH_MAX
Definition: gis.h:199
#define GNAME_MAX
Definition: gis.h:196
#define GRASS_DIRSEP
Definition: gis.h:235
#define HOST_DIRSEP
Definition: gis.h:240
#define RASTER3D_WINDOW_DATABASE
Definition: raster3d.h:42
#define RASTER3D_WINDOW_ELEMENT
Definition: raster3d.h:40
#define RASTER3D_REGION_COLS
#define RASTER3D_REGION_EAST
#define RASTER3D_REGION_PROJ
#define RASTER3D_REGION_NORTH
#define RASTER3D_REGION_DEPTHS
#define RASTER3D_REGION_WEST
#define RASTER3D_REGION_NSRES
#define RASTER3D_REGION_ZONE
#define RASTER3D_REGION_ROWS
#define RASTER3D_REGION_BOTTOM
#define RASTER3D_REGION_TBRES
#define RASTER3D_REGION_SOUTH
#define RASTER3D_REGION_EWRES
#define RASTER3D_REGION_TOP
2D/3D raster map header (used also for region)
Definition: gis.h:446
int cols3
Number of columns for 3D data.
Definition: gis.h:467
double north
Extent coordinates (north)
Definition: gis.h:492
double bottom
Extent coordinates (bottom) - 3D data.
Definition: gis.h:502
int zone
Projection zone (UTM)
Definition: gis.h:480
int depths
number of depths for 3D data
Definition: gis.h:469
double east
Extent coordinates (east)
Definition: gis.h:496
double ew_res3
Resolution - east to west cell size for 3D data.
Definition: gis.h:484
double ns_res3
Resolution - north to south cell size for 3D data.
Definition: gis.h:488
double top
Extent coordinates (top) - 3D data.
Definition: gis.h:500
int rows3
Number of rows for 3D data.
Definition: gis.h:463
int proj
Projection code.
Definition: gis.h:478
double south
Extent coordinates (south)
Definition: gis.h:494
double tb_res
Resolution - top to bottom cell size for 3D data.
Definition: gis.h:490
double west
Extent coordinates (west)
Definition: gis.h:498
Definition: gis.h:534
double tb_res
Definition: raster3d.h:56
double north
Definition: raster3d.h:49
double south
Definition: raster3d.h:49
double east
Definition: raster3d.h:50
double ns_res
Definition: raster3d.h:56
double ew_res
Definition: raster3d.h:56
double bottom
Definition: raster3d.h:51
double top
Definition: raster3d.h:51
double west
Definition: raster3d.h:50
Definition: path.h:15
#define access
Definition: unistd.h:7
#define R_OK
Definition: unistd.h:25
int Rast3d_read_window(RASTER3D_Region *window, const char *windowName)
Reads window from the file specified by windowName. The name is converted by the rules defined in win...
Definition: windowio.c:132
void Rast3d_use_window_params(void)
Writes window to the file specified by windowName. The name is converted by the rules defined in wind...
Definition: windowio.c:263