g3dwindowio.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. static int
  11. G3d_readWriteWindow(struct Key_Value *windowKeys, int doRead, int *proj,
  12. int *zone, double *north, double *south, double *east,
  13. double *west, double *top, double *bottom, int *rows,
  14. int *cols, int *depths, double *ew_res, double *ns_res,
  15. double *tb_res)
  16. {
  17. int returnVal;
  18. int (*windowInt) (), (*windowDouble) ();
  19. if (doRead) {
  20. windowDouble = G3d_keyGetDouble;
  21. windowInt = G3d_keyGetInt;
  22. }
  23. else {
  24. windowDouble = G3d_keySetDouble;
  25. windowInt = G3d_keySetInt;
  26. }
  27. returnVal = 1;
  28. returnVal &= windowInt(windowKeys, G3D_REGION_PROJ, proj);
  29. returnVal &= windowInt(windowKeys, G3D_REGION_ZONE, zone);
  30. returnVal &= windowDouble(windowKeys, G3D_REGION_NORTH, north);
  31. returnVal &= windowDouble(windowKeys, G3D_REGION_SOUTH, south);
  32. returnVal &= windowDouble(windowKeys, G3D_REGION_EAST, east);
  33. returnVal &= windowDouble(windowKeys, G3D_REGION_WEST, west);
  34. returnVal &= windowDouble(windowKeys, G3D_REGION_TOP, top);
  35. returnVal &= windowDouble(windowKeys, G3D_REGION_BOTTOM, bottom);
  36. returnVal &= windowInt(windowKeys, G3D_REGION_ROWS, rows);
  37. returnVal &= windowInt(windowKeys, G3D_REGION_COLS, cols);
  38. returnVal &= windowInt(windowKeys, G3D_REGION_DEPTHS, depths);
  39. returnVal &= windowDouble(windowKeys, G3D_REGION_EWRES, ew_res);
  40. returnVal &= windowDouble(windowKeys, G3D_REGION_NSRES, ns_res);
  41. returnVal &= windowDouble(windowKeys, G3D_REGION_TBRES, tb_res);
  42. if (returnVal)
  43. return 1;
  44. G3d_error("G3d_readWriteWindow: error writing window");
  45. return 0;
  46. }
  47. /*
  48. * If windowName == NULL -> G3D_WINDOW_ELEMENT ("$MAPSET/WIND3")
  49. * otherwise G3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME")
  50. */
  51. static void G3d_getFullWindowPath(char *path, const char *windowName)
  52. {
  53. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  54. if (windowName == NULL) {
  55. G__file_name(path, "", G3D_WINDOW_ELEMENT, G_mapset());
  56. return;
  57. }
  58. while (*windowName == ' ')
  59. windowName++;
  60. if ((*windowName == '/') || (*windowName == '.')) {
  61. sprintf(path, windowName);
  62. return;
  63. }
  64. if (G_name_is_fully_qualified(windowName, xname, xmapset)) {
  65. G__file_name(path, G3D_WINDOW_DATABASE, xname, xmapset);
  66. return;
  67. }
  68. G__file_name(path, G3D_WINDOW_DATABASE, windowName, G_mapset());
  69. }
  70. /*---------------------------------------------------------------------------*/
  71. /*
  72. static void
  73. G3d_getWindowLocation (path, windowName)
  74. char path[1024];
  75. char *windowName;
  76. {
  77. char xname[512], xmapset[512];
  78. char *p, *slash;
  79. if (windowName == NULL) {
  80. G__file_name (path, "", "", G_mapset ());
  81. return;
  82. }
  83. while (*windowName == ' ') windowName++;
  84. if ((*windowName != '/') && (*windowName != '.')) {
  85. if (G_name_is_fully_qualified (windowName, xname, xmapset))
  86. G__file_name (path, G3D_WINDOW_DATABASE, xname, xmapset);
  87. else
  88. G__file_name (path, G3D_WINDOW_DATABASE, windowName, G_mapset ());
  89. } else
  90. sprintf (path, windowName);
  91. p = path;
  92. slash = NULL;
  93. while (*p != 0) {
  94. if (*p == '/') slash = p;
  95. p++;
  96. }
  97. if (slash != NULL) *slash = 0;
  98. }
  99. */
  100. /*---------------------------------------------------------------------------*/
  101. /*!
  102. * \brief
  103. *
  104. * Reads
  105. * <em>window</em> from the file specified by <em>windowName</em>. The name is
  106. * converted by the rules defined in window defaults. A NULL pointer indicates
  107. * the <em>WIND3</em> file in the current mapset.
  108. *
  109. * \param window
  110. * \param windowName
  111. * \return 1 ... if successful
  112. * 0 ... otherwise.
  113. */
  114. int G3d_readWindow(G3D_Region * window, const char *windowName)
  115. {
  116. struct Cell_head win;
  117. struct Key_Value *windowKeys;
  118. char path[GPATH_MAX];
  119. int status;
  120. if (windowName == NULL) {
  121. G_get_window(&win);
  122. window->proj = win.proj;
  123. window->zone = win.zone;
  124. window->north = win.north;
  125. window->south = win.south;
  126. window->east = win.east;
  127. window->west = win.west;
  128. window->top = win.top;
  129. window->bottom = win.bottom;
  130. window->rows = win.rows3;
  131. window->cols = win.cols3;
  132. window->depths = win.depths;
  133. window->ns_res = win.ns_res3;
  134. window->ew_res = win.ew_res3;
  135. window->tb_res = win.tb_res;
  136. }
  137. else {
  138. G3d_getFullWindowPath(path, windowName);
  139. if (access(path, R_OK) != 0) {
  140. G_warning("G3d_readWindow: unable to find [%s].", path);
  141. return 0;
  142. }
  143. windowKeys = G_read_key_value_file(path, &status);
  144. if (status != 0) {
  145. G3d_error("G3d_readWindow: Unable to open %s", path);
  146. return 0;
  147. }
  148. if (!G3d_readWriteWindow(windowKeys, 1,
  149. &(window->proj), &(window->zone),
  150. &(window->north), &(window->south),
  151. &(window->east), &(window->west),
  152. &(window->top), &(window->bottom),
  153. &(window->rows), &(window->cols),
  154. &(window->depths), &(window->ew_res),
  155. &(window->ns_res), &(window->tb_res))) {
  156. G3d_error
  157. ("G3d_readWindow: error extracting window key(s) of file %s",
  158. path);
  159. return 0;
  160. }
  161. G_free_key_value(windowKeys);
  162. }
  163. return 1;
  164. }
  165. /*---------------------------------------------------------------------------*/
  166. /* modified version of G__make_mapset_element */
  167. /*
  168. static int
  169. G3d_createPath (thePath)
  170. char *thePath;
  171. {
  172. char command[1024];
  173. char *path, *p, *pOld;
  174. if (*thePath == 0) return 0;
  175. strcpy (path = command, "mkdir ");
  176. while (*path) path++;
  177. p = path;
  178. */
  179. /* now append element, one directory at a time, to path */
  180. /*
  181. while (1) {
  182. if (*thePath == '/') *p++ = *thePath++;
  183. pOld = p;
  184. while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
  185. *p = 0;
  186. if (p == pOld) return 1;
  187. if (access (path, 0) != 0) mkdir (path,0777);
  188. if (access (path, 0) != 0) system (command);
  189. if (access (path, 0) != 0) {
  190. char err[1024];
  191. sprintf (err, "can't make mapset element %s (%s)", thePath, path);
  192. G_fatal_error (err);
  193. exit(1);
  194. }
  195. }
  196. }
  197. */
  198. /*---------------------------------------------------------------------------*/
  199. /*!
  200. * \brief
  201. *
  202. *
  203. * Writes <em>window</em> to the file specified by <em>windowName</em>. The name
  204. * is converted by the rules defined in window defaults. A NULL pointer
  205. * indicates the <em>WIND3</em> file in the current mapset.
  206. *
  207. * \param window
  208. * \param windowName
  209. * \return 1 ... if successful
  210. * 0 ... otherwise.
  211. */
  212. /*
  213. int
  214. G3d_writeWindow (window, windowName)
  215. G3D_Region *window;
  216. char *windowName;
  217. {
  218. return 0;
  219. }
  220. */
  221. /*---------------------------------------------------------------------------*/
  222. /*!
  223. * \brief
  224. *
  225. * Allows the window to be set at run-time via the <em>region3</em>
  226. * command line argument. This function has to be called before
  227. * <em>G_parser ()</em>. See also window defaults.
  228. *
  229. * \return void
  230. */
  231. void G3d_useWindowParams(void)
  232. {
  233. G3d_setWindowParams();
  234. }