close.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*!
  2. \file lib/raster3d/close.c
  3. \brief 3D Raster Library - Close 3D raster file
  4. (C) 1999-2009, 2011 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. \author USACERL and many others
  9. */
  10. #ifdef __MINGW32__
  11. # include <windows.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <sys/types.h>
  16. #include <unistd.h>
  17. #include <grass/raster.h>
  18. #include <grass/glocale.h>
  19. #include "raster3d_intern.h"
  20. static int close_new(RASTER3D_Map * map)
  21. {
  22. char path[GPATH_MAX];
  23. struct Categories cats;
  24. struct History hist;
  25. Rast3d_remove_color(map->fileName);
  26. /* create empty cats file */
  27. Rast_init_cats(NULL, &cats);
  28. Rast3d_write_cats(map->fileName, &cats);
  29. Rast_free_cats(&cats);
  30. /*generate the history file, use the normal G_ functions */
  31. Rast_short_history(map->fileName, "raster3d", &hist);
  32. Rast_command_history(&hist);
  33. /*Use the G3d function to write the history file,
  34. * otherwise the path is wrong */
  35. if (Rast3d_write_history(map->fileName, &hist) < 0) {
  36. G_warning(_("Unable to write history for 3D raster map <%s>"), map->fileName);
  37. }
  38. Rast3d_range_write(map);
  39. close(map->data_fd);
  40. /* finally move tempfile to data file */
  41. Rast3d_filename(path, RASTER3D_CELL_ELEMENT, map->fileName, map->mapset);
  42. #ifdef __MINGW32__
  43. if (CopyFile(map->tempName, path, FALSE) == 0) {
  44. #else
  45. if (link(map->tempName, path) < 0) {
  46. #endif
  47. if (rename(map->tempName, path)) {
  48. G_warning(_("Unable to move temp raster map <%s> to 3D raster map <%s>"),
  49. map->tempName, path);
  50. return 0;
  51. }
  52. }
  53. else
  54. remove(map->tempName);
  55. return 1;
  56. }
  57. static int close_cell_new(RASTER3D_Map * map)
  58. {
  59. long ltmp;
  60. if (map->useCache)
  61. if (!Rast3d_flush_all_tiles(map)) {
  62. G_warning(_("Unable to flush all tiles"));
  63. return 0;
  64. }
  65. if (!Rast3d_flush_index(map)) {
  66. G_warning(_("Unable to flush index"));
  67. return 0;
  68. }
  69. /* write the header info which was filled with dummy values at the */
  70. /* opening time */
  71. if (lseek(map->data_fd,
  72. (long)(map->offset - sizeof(int) - sizeof(long)),
  73. SEEK_SET) == -1) {
  74. G_warning(_("Unable to position file"));
  75. return 0;
  76. }
  77. if (!Rast3d_write_ints(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1)) {
  78. G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
  79. return 0;
  80. }
  81. Rast3d_long_encode(&(map->indexOffset), (unsigned char *)&ltmp, 1);
  82. if (write(map->data_fd, &ltmp, sizeof(long)) != sizeof(long)) {
  83. G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
  84. return 0;
  85. }
  86. if (!close_new(map)) {
  87. G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
  88. return 0;
  89. }
  90. return 1;
  91. }
  92. static int close_old(RASTER3D_Map * map)
  93. {
  94. if (close(map->data_fd) != 0) {
  95. G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
  96. return 0;
  97. }
  98. return 1;
  99. }
  100. static int close_cell_old(RASTER3D_Map * map)
  101. {
  102. if (!close_old(map)) {
  103. G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
  104. return 0;
  105. }
  106. return 1;
  107. }
  108. /*!
  109. \brief Close 3D raster map files
  110. Closes g3d-file. If <em>map</em> is new and cache-mode is used for
  111. <em>map</em> then every tile which is not flushed before closing is
  112. flushed.
  113. \param map pointer to RASTER3D_Map to be closed
  114. \return 1 success
  115. \return 0 failure
  116. */
  117. int Rast3d_close(RASTER3D_Map * map)
  118. {
  119. if (map->operation == RASTER3D_WRITE_DATA) {
  120. if (!close_cell_new(map)) {
  121. G_warning(_("Unable to create 3D raster map <%s>"), map->fileName);
  122. return 0;
  123. }
  124. }
  125. else {
  126. if (!close_cell_old(map)) {
  127. G_warning(_("Unable to close 3D raster map <%s>"), map->fileName);
  128. return 0;
  129. }
  130. }
  131. Rast3d_free(map->index);
  132. Rast3d_free(map->tileLength);
  133. if (map->useCache) {
  134. if (!Rast3d_dispose_cache(map)) {
  135. G_warning(_("Error in cache"));
  136. return 0;
  137. }
  138. }
  139. else
  140. Rast3d_free(map->data);
  141. if (map->operation == RASTER3D_WRITE_DATA)
  142. if (!Rast3d_write_header(map,
  143. map->region.proj, map->region.zone,
  144. map->region.north, map->region.south,
  145. map->region.east, map->region.west,
  146. map->region.top, map->region.bottom,
  147. map->region.rows, map->region.cols,
  148. map->region.depths,
  149. map->region.ew_res, map->region.ns_res,
  150. map->region.tb_res,
  151. map->tileX, map->tileY, map->tileZ,
  152. map->type,
  153. map->compression, map->useRle, map->useLzw,
  154. map->precision, map->offset, map->useXdr,
  155. map->hasIndex, map->unit, map->vertical_unit, map->version)) {
  156. G_warning(_("Unable to write header for 3D raster map <%s>"), map->fileName);
  157. return 0;
  158. }
  159. Rast3d_free(map);
  160. return 1;
  161. }