windowio.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. static int
  11. Rast3d_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 = Rast3d_key_get_double;
  21. windowInt = Rast3d_key_get_int;
  22. }
  23. else {
  24. windowDouble = Rast3d_key_set_double;
  25. windowInt = Rast3d_key_set_int;
  26. }
  27. returnVal = 1;
  28. returnVal &= windowInt(windowKeys, RASTER3D_REGION_PROJ, proj);
  29. returnVal &= windowInt(windowKeys, RASTER3D_REGION_ZONE, zone);
  30. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_NORTH, north);
  31. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_SOUTH, south);
  32. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_EAST, east);
  33. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_WEST, west);
  34. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_TOP, top);
  35. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_BOTTOM, bottom);
  36. returnVal &= windowInt(windowKeys, RASTER3D_REGION_ROWS, rows);
  37. returnVal &= windowInt(windowKeys, RASTER3D_REGION_COLS, cols);
  38. returnVal &= windowInt(windowKeys, RASTER3D_REGION_DEPTHS, depths);
  39. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_EWRES, ew_res);
  40. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_NSRES, ns_res);
  41. returnVal &= windowDouble(windowKeys, RASTER3D_REGION_TBRES, tb_res);
  42. if (returnVal)
  43. return 1;
  44. Rast3d_error("Rast3d_readWriteWindow: error writing window");
  45. return 0;
  46. }
  47. /*
  48. * If windowName == NULL -> RASTER3D_WINDOW_ELEMENT ("$MAPSET/WIND3")
  49. * otherwise RASTER3D_WINDOW_DATABASE ("$MAPSET/windows3d/$NAME")
  50. */
  51. static void Rast3d_getFullWindowPath(char *path, const char *windowName)
  52. {
  53. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  54. if (windowName == NULL) {
  55. G_file_name(path, "", RASTER3D_WINDOW_ELEMENT, G_mapset());
  56. return;
  57. }
  58. while (*windowName == ' ')
  59. windowName++;
  60. if (strchr(windowName, GRASS_DIRSEP) || strchr(windowName, HOST_DIRSEP)) {
  61. sprintf(path, "%s", windowName);
  62. return;
  63. }
  64. if (G_name_is_fully_qualified(windowName, xname, xmapset)) {
  65. G_file_name(path, RASTER3D_WINDOW_DATABASE, xname, xmapset);
  66. return;
  67. }
  68. G_file_name(path, RASTER3D_WINDOW_DATABASE, windowName, G_mapset());
  69. }
  70. /*---------------------------------------------------------------------------*/
  71. /*
  72. static void
  73. Rast3d_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, RASTER3D_WINDOW_DATABASE, xname, xmapset);
  87. else
  88. G_file_name (path, RASTER3D_WINDOW_DATABASE, windowName, G_mapset ());
  89. } else
  90. sprintf (path, "%s", 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 Rast3d_read_window(RASTER3D_Region * window, const char *windowName)
  115. {
  116. struct Cell_head win;
  117. struct Key_Value *windowKeys;
  118. char path[GPATH_MAX];
  119. if (windowName == NULL) {
  120. G_get_window(&win);
  121. window->proj = win.proj;
  122. window->zone = win.zone;
  123. window->north = win.north;
  124. window->south = win.south;
  125. window->east = win.east;
  126. window->west = win.west;
  127. window->top = win.top;
  128. window->bottom = win.bottom;
  129. window->rows = win.rows3;
  130. window->cols = win.cols3;
  131. window->depths = win.depths;
  132. window->ns_res = win.ns_res3;
  133. window->ew_res = win.ew_res3;
  134. window->tb_res = win.tb_res;
  135. }
  136. else {
  137. Rast3d_getFullWindowPath(path, windowName);
  138. if (access(path, R_OK) != 0) {
  139. G_warning("Rast3d_read_window: unable to find [%s].", path);
  140. return 0;
  141. }
  142. windowKeys = G_read_key_value_file(path);
  143. if (!Rast3d_readWriteWindow(windowKeys, 1,
  144. &(window->proj), &(window->zone),
  145. &(window->north), &(window->south),
  146. &(window->east), &(window->west),
  147. &(window->top), &(window->bottom),
  148. &(window->rows), &(window->cols),
  149. &(window->depths), &(window->ew_res),
  150. &(window->ns_res), &(window->tb_res))) {
  151. Rast3d_error
  152. ("Rast3d_read_window: error extracting window key(s) of file %s",
  153. path);
  154. return 0;
  155. }
  156. G_free_key_value(windowKeys);
  157. }
  158. return 1;
  159. }
  160. /*---------------------------------------------------------------------------*/
  161. /* modified version of G__make_mapset_element */
  162. /*
  163. static int
  164. Rast3d_createPath (thePath)
  165. char *thePath;
  166. {
  167. char command[1024];
  168. char *path, *p, *pOld;
  169. if (*thePath == 0) return 0;
  170. strcpy (path = command, "mkdir ");
  171. while (*path) path++;
  172. p = path;
  173. */
  174. /* now append element, one directory at a time, to path */
  175. /*
  176. while (1) {
  177. if (*thePath == '/') *p++ = *thePath++;
  178. pOld = p;
  179. while ((*thePath) && (*thePath != '/')) *p++ = *thePath++;
  180. *p = 0;
  181. if (p == pOld) return 1;
  182. if (access (path, 0) != 0) mkdir (path,0777);
  183. if (access (path, 0) != 0) system (command);
  184. if (access (path, 0) != 0) {
  185. char err[1024];
  186. sprintf (err, "can't make mapset element %s (%s)", thePath, path);
  187. G_fatal_error (err);
  188. exit(1);
  189. }
  190. }
  191. }
  192. */
  193. /*---------------------------------------------------------------------------*/
  194. /*!
  195. * \brief
  196. *
  197. *
  198. * Writes <em>window</em> to the file specified by <em>windowName</em>. The name
  199. * is converted by the rules defined in window defaults. A NULL pointer
  200. * indicates the <em>WIND3</em> file in the current mapset.
  201. *
  202. * \param window
  203. * \param windowName
  204. * \return 1 ... if successful
  205. * 0 ... otherwise.
  206. */
  207. /*
  208. int
  209. Rast3d_writeWindow (window, windowName)
  210. RASTER3D_Region *window;
  211. char *windowName;
  212. {
  213. return 0;
  214. }
  215. */
  216. /*---------------------------------------------------------------------------*/
  217. /*!
  218. * \brief
  219. *
  220. * Allows the window to be set at run-time via the <em>region3</em>
  221. * command line argument. This function has to be called before
  222. * <em>G_parser ()</em>. See also window defaults.
  223. *
  224. * \return void
  225. */
  226. void Rast3d_use_window_params(void)
  227. {
  228. Rast3d_set_window_params();
  229. }