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