GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
g3dwindowio.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 "G3d_intern.h"
9 
10 /*---------------------------------------------------------------------------*/
11 
12 static int
13 G3d_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 = G3d_keyGetDouble;
24  windowInt = G3d_keyGetInt;
25  }
26  else {
27  windowDouble = G3d_keySetDouble;
28  windowInt = G3d_keySetInt;
29  }
30 
31  returnVal = 1;
32  returnVal &= windowInt(windowKeys, G3D_REGION_PROJ, proj);
33  returnVal &= windowInt(windowKeys, G3D_REGION_ZONE, zone);
34 
35  returnVal &= windowDouble(windowKeys, G3D_REGION_NORTH, north);
36  returnVal &= windowDouble(windowKeys, G3D_REGION_SOUTH, south);
37  returnVal &= windowDouble(windowKeys, G3D_REGION_EAST, east);
38  returnVal &= windowDouble(windowKeys, G3D_REGION_WEST, west);
39  returnVal &= windowDouble(windowKeys, G3D_REGION_TOP, top);
40  returnVal &= windowDouble(windowKeys, G3D_REGION_BOTTOM, bottom);
41 
42  returnVal &= windowInt(windowKeys, G3D_REGION_ROWS, rows);
43  returnVal &= windowInt(windowKeys, G3D_REGION_COLS, cols);
44  returnVal &= windowInt(windowKeys, G3D_REGION_DEPTHS, depths);
45 
46  returnVal &= windowDouble(windowKeys, G3D_REGION_EWRES, ew_res);
47  returnVal &= windowDouble(windowKeys, G3D_REGION_NSRES, ns_res);
48  returnVal &= windowDouble(windowKeys, G3D_REGION_TBRES, tb_res);
49 
50  if (returnVal)
51  return 1;
52 
53  G3d_error("G3d_readWriteWindow: error writing window");
54  return 0;
55 }
56 
57 /*
58  * If windowName == NULL -> G3D_WINDOW_ELEMENT ("$MAPSET/WIND3")
59  * otherwise G3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME")
60  */
61 static void G3d_getFullWindowPath(char *path, const char *windowName)
62 {
63  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
64 
65  if (windowName == NULL) {
66  G__file_name(path, "", G3D_WINDOW_ELEMENT, G_mapset());
67  return;
68  }
69 
70  while (*windowName == ' ')
71  windowName++;
72 
73  if (strchr(windowName, GRASS_DIRSEP) || strchr(windowName, HOST_DIRSEP)) {
74  sprintf(path, windowName);
75  return;
76  }
77 
78  if (G__name_is_fully_qualified(windowName, xname, xmapset)) {
79  G__file_name(path, G3D_WINDOW_DATABASE, xname, xmapset);
80  return;
81  }
82 
83  G__file_name(path, G3D_WINDOW_DATABASE, windowName, G_mapset());
84 }
85 
86 /*---------------------------------------------------------------------------*/
87 /*
88  static void
89  G3d_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, G3D_WINDOW_DATABASE, xname, xmapset);
108  else
109  G__file_name (path, G3D_WINDOW_DATABASE, windowName, G_mapset ());
110  } else
111  sprintf (path, 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 
139 int G3d_readWindow(G3D_Region * window, const char *windowName)
140 {
141  struct Cell_head win;
142  struct Key_Value *windowKeys;
143  char path[GPATH_MAX];
144  int status;
145 
146 
147  if (windowName == NULL) {
148  G_get_window(&win);
149 
150  window->proj = win.proj;
151  window->zone = win.zone;
152  window->north = win.north;
153  window->south = win.south;
154  window->east = win.east;
155  window->west = win.west;
156  window->top = win.top;
157  window->bottom = win.bottom;
158  window->rows = win.rows3;
159  window->cols = win.cols3;
160  window->depths = win.depths;
161  window->ns_res = win.ns_res3;
162  window->ew_res = win.ew_res3;
163  window->tb_res = win.tb_res;
164  }
165  else {
166  G3d_getFullWindowPath(path, windowName);
167 
168  if (access(path, R_OK) != 0) {
169  G_warning("G3d_readWindow: unable to find [%s].", path);
170  return 0;
171  }
172 
173  windowKeys = G_read_key_value_file(path, &status);
174  if (status != 0) {
175  G3d_error("G3d_readWindow: Unable to open %s", path);
176  return 0;
177  }
178 
179  if (!G3d_readWriteWindow(windowKeys, 1,
180  &(window->proj), &(window->zone),
181  &(window->north), &(window->south),
182  &(window->east), &(window->west),
183  &(window->top), &(window->bottom),
184  &(window->rows), &(window->cols),
185  &(window->depths), &(window->ew_res),
186  &(window->ns_res), &(window->tb_res))) {
187  G3d_error
188  ("G3d_readWindow: error extracting window key(s) of file %s",
189  path);
190  return 0;
191  }
192 
193  G_free_key_value(windowKeys);
194  }
195 
196  return 1;
197 }
198 
199 /*---------------------------------------------------------------------------*/
200 /* modified version of G__make_mapset_element */
201 /*
202  static int
203  G3d_createPath (thePath)
204 
205  char *thePath;
206 
207  {
208  char command[1024];
209  char *path, *p, *pOld;
210 
211  if (*thePath == 0) return 0;
212 
213  strcpy (path = command, "mkdir ");
214  while (*path) path++;
215  p = path;
216  */
217  /* now append element, one directory at a time, to path */
218 /*
219  while (1) {
220  if (*thePath == '/') *p++ = *thePath++;
221  pOld = p;
222  while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
223  *p = 0;
224 
225  if (p == pOld) return 1;
226 
227  if (access (path, 0) != 0) mkdir (path,0777);
228  if (access (path, 0) != 0) system (command);
229  if (access (path, 0) != 0) {
230  char err[1024];
231  sprintf (err, "can't make mapset element %s (%s)", thePath, path);
232  G_fatal_error (err);
233  exit(1);
234  }
235  }
236  }
237  */
238 
239 /*---------------------------------------------------------------------------*/
240 
241 
256 /*
257  int
258  G3d_writeWindow (window, windowName)
259 
260  G3D_Region *window;
261  char *windowName;
262 
263  {
264  return 0;
265  }
266  */
267 
268 /*---------------------------------------------------------------------------*/
269 
270 
282 {
284 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
#define G3D_REGION_EAST
Definition: G3d_intern.h:73
#define G3D_REGION_COLS
Definition: G3d_intern.h:78
#define G3D_REGION_NSRES
Definition: G3d_intern.h:83
#define G3D_REGION_SOUTH
Definition: G3d_intern.h:72
char xmapset[512]
Definition: g3dcats.c:89
void G3d_error(const char *msg,...)
Definition: g3derror.c:75
int G3d_keyGetDouble(struct Key_Value *keys, const char *key, double *d)
Definition: g3dkeys.c:26
int G3d_keySetInt(struct Key_Value *keys, const char *key, const int *i)
Definition: g3dkeys.c:93
int G_free_key_value(struct Key_Value *kv)
Free allocated Key_Value structure.
Definition: key_value1.c:145
#define R_OK
Definition: dirent.c:6
#define G3D_REGION_DEPTHS
Definition: G3d_intern.h:79
#define G3D_REGION_BOTTOM
Definition: G3d_intern.h:76
#define G3D_REGION_EWRES
Definition: G3d_intern.h:82
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:33
struct Key_Value * G_read_key_value_file(const char *file, int *stat)
Read key/values pairs from file.
Definition: key_value3.c:54
void G3d_useWindowParams(void)
Writes window to the file specified by windowName. The name is converted by the rules defined in wind...
Definition: g3dwindowio.c:281
tuple window
Definition: tools.py:543
int
Definition: g3dcolor.c:48
int G_get_window(struct Cell_head *window)
read the database region
Definition: get_window.c:47
#define G3D_REGION_TBRES
Definition: G3d_intern.h:84
int G3d_keySetDouble(struct Key_Value *keys, const char *key, const double *d)
Definition: g3dkeys.c:103
#define G3D_REGION_PROJ
Definition: G3d_intern.h:80
#define G3D_REGION_TOP
Definition: G3d_intern.h:75
return NULL
Definition: dbfopen.c:1394
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
tuple cols
int G3d_readWindow(G3D_Region *window, const char *windowName)
Reads window from the file specified by windowName. The name is converted by the rules defined in win...
Definition: g3dwindowio.c:139
void G3d_setWindowParams(void)
Definition: g3dparam.c:165
#define G3D_REGION_ZONE
Definition: G3d_intern.h:81
#define G3D_REGION_WEST
Definition: G3d_intern.h:74
#define G3D_REGION_NORTH
Definition: G3d_intern.h:71
int G3d_keyGetInt(struct Key_Value *keys, const char *key, int *i)
Definition: g3dkeys.c:7
char xname[512]
Definition: g3dcats.c:89
#define G3D_REGION_ROWS
Definition: G3d_intern.h:77
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:57