GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-76958057df
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  sprintf(path, "%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 
133 int Rast3d_read_window(RASTER3D_Region *window, const char *windowName)
134 {
135  struct Cell_head win;
136  struct Key_Value *windowKeys;
137  char path[GPATH_MAX];
138 
139  if (windowName == NULL) {
140  G_get_window(&win);
141 
142  window->proj = win.proj;
143  window->zone = win.zone;
144  window->north = win.north;
145  window->south = win.south;
146  window->east = win.east;
147  window->west = win.west;
148  window->top = win.top;
149  window->bottom = win.bottom;
150  window->rows = win.rows3;
151  window->cols = win.cols3;
152  window->depths = win.depths;
153  window->ns_res = win.ns_res3;
154  window->ew_res = win.ew_res3;
155  window->tb_res = win.tb_res;
156  }
157  else {
158  Rast3d_getFullWindowPath(path, windowName);
159 
160  if (access(path, R_OK) != 0) {
161  G_warning("Rast3d_read_window: unable to find [%s].", path);
162  return 0;
163  }
164 
165  windowKeys = G_read_key_value_file(path);
166 
167  if (!Rast3d__readWindow(
168  windowKeys, &(window->proj), &(window->zone), &(window->north),
169  &(window->south), &(window->east), &(window->west),
170  &(window->top), &(window->bottom), &(window->rows),
171  &(window->cols), &(window->depths), &(window->ew_res),
172  &(window->ns_res), &(window->tb_res))) {
173  Rast3d_error(
174  "Rast3d_read_window: error extracting window key(s) of file %s",
175  path);
176  return 0;
177  }
178 
179  G_free_key_value(windowKeys);
180  }
181 
182  return 1;
183 }
184 
185 /*---------------------------------------------------------------------------*/
186 /* modified version of G__make_mapset_element */
187 /*
188  static int
189  Rast3d_createPath (thePath)
190 
191  char *thePath;
192 
193  {
194  char command[1024];
195  char *path, *p, *pOld;
196 
197  if (*thePath == 0) return 0;
198 
199  strcpy (path = command, "mkdir ");
200  while (*path) path++;
201  p = path;
202  */
203 /* now append element, one directory at a time, to path */
204 /*
205  while (1) {
206  if (*thePath == '/') *p++ = *thePath++;
207  pOld = p;
208  while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
209  *p = 0;
210 
211  if (p == pOld) return 1;
212 
213  if (access (path, 0) != 0) mkdir (path,0777);
214  if (access (path, 0) != 0) system (command);
215  if (access (path, 0) != 0) {
216  char err[1024];
217  sprintf (err, "can't make mapset element %s (%s)", thePath, path);
218  G_fatal_error (err);
219  exit(1);
220  }
221  }
222  }
223  */
224 
225 /*---------------------------------------------------------------------------*/
226 
227 /*!
228  * \brief
229  *
230  *
231  * Writes <em>window</em> to the file specified by <em>windowName</em>. The name
232  * is converted by the rules defined in window defaults. A NULL pointer
233  * indicates the <em>WIND3</em> file in the current mapset.
234  *
235  * \param window
236  * \param windowName
237  * \return 1 ... if successful
238  * 0 ... otherwise.
239  */
240 
241 /*
242  int
243  Rast3d_writeWindow (window, windowName)
244 
245  RASTER3D_Region *window;
246  char *windowName;
247 
248  {
249  return 0;
250  }
251  */
252 
253 /*---------------------------------------------------------------------------*/
254 
255 /*!
256  * \brief
257  *
258  * Allows the window to be set at run-time via the <em>region3</em>
259  * command line argument. This function has to be called before
260  * <em>G_parser ()</em>. See also window defaults.
261  *
262  * \return void
263  */
264 
266 {
268 }
#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:121
int Rast3d_key_get_double(struct Key_Value *, const char *, double *)
Definition: keys.c:27
void Rast3d_error(const char *,...) __attribute__((format(printf
#define R_OK
Definition: dirent.c:25
#define GMAPSET_MAX
Definition: gis.h:192
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
#define GRASS_DIRSEP
Definition: gis.h:230
#define HOST_DIRSEP
Definition: gis.h:235
#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:439
int cols3
Number of columns for 3D data.
Definition: gis.h:460
double north
Extent coordinates (north)
Definition: gis.h:485
double bottom
Extent coordinates (bottom) - 3D data.
Definition: gis.h:495
int zone
Projection zone (UTM)
Definition: gis.h:473
int depths
number of depths for 3D data
Definition: gis.h:462
double east
Extent coordinates (east)
Definition: gis.h:489
double ew_res3
Resolution - east to west cell size for 3D data.
Definition: gis.h:477
double ns_res3
Resolution - north to south cell size for 3D data.
Definition: gis.h:481
double top
Extent coordinates (top) - 3D data.
Definition: gis.h:493
int rows3
Number of rows for 3D data.
Definition: gis.h:456
int proj
Projection code.
Definition: gis.h:471
double south
Extent coordinates (south)
Definition: gis.h:487
double tb_res
Resolution - top to bottom cell size for 3D data.
Definition: gis.h:483
double west
Extent coordinates (west)
Definition: gis.h:491
Definition: gis.h:527
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
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:133
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:265