closecell.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /***********************************************************************
  2. *
  3. * G_close_cell(fd)
  4. * Closes and does housekeeping on an opened cell file
  5. *
  6. * G_unopen_cell(fd)
  7. * Closes and does housekeeping on an opened cell file
  8. * without creating the cell file
  9. *
  10. * parms:
  11. * int fd open cell file
  12. *
  13. * returns:
  14. * -1 on fail
  15. * 0 on success
  16. *
  17. * note:
  18. * On closing of a cell file that was open for writing, dummy cats
  19. * and history files are created. Histogram and range info are written.
  20. *
  21. **********************************************************************/
  22. #ifdef __MINGW32__
  23. # include <windows.h>
  24. #endif
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <unistd.h>
  29. #include <fcntl.h>
  30. #include <signal.h>
  31. #include <grass/gis.h>
  32. #include <grass/glocale.h>
  33. #include "G.h"
  34. #define FORMAT_FILE "f_format"
  35. #define QUANT_FILE "f_quant"
  36. #define NULL_FILE "null"
  37. static int close_old(int);
  38. static int close_new(int, int);
  39. static int write_fp_format(int fd);
  40. /*!
  41. * \brief close a raster map
  42. *
  43. * The raster map
  44. * opened on file descriptor <b>fd</b> is closed. Memory allocated for raster
  45. * processing is freed. If open for writing, skeletal support files for the new
  46. * raster map are created as well.
  47. * <b>Note.</b> If a module wants to explicitly write support files (e.g., a
  48. * specific color table) for a raster map it creates, it must do so after the
  49. * raster map is closed. Otherwise the close will overwrite the support files.
  50. * See Raster_Map_Layer_Support_Routines for routines which write
  51. * raster support files.
  52. *
  53. * \param fd
  54. * \return int
  55. */
  56. /*!
  57. * \brief
  58. *
  59. * If the map is a new floating point, move the
  60. * <tt>.tmp</tt> file into the <tt>fcell</tt> element, create an empty file in the
  61. * <tt>cell</tt> directory; write the floating-point range file; write a default
  62. * quantization file quantization file is set here to round fp numbers (this is
  63. * a default for now). create an empty category file, with max cat = max value
  64. * (for backwards compatibility). Move the <tt>.tmp</tt> NULL-value bitmap file to
  65. * the <tt>cell_misc</tt> directory.
  66. *
  67. * \return int
  68. */
  69. int G_close_cell(int fd)
  70. {
  71. struct fileinfo *fcb = &G__.fileinfo[fd];
  72. if (fd < 0 || fd >= G__.fileinfo_count || fcb->open_mode <= 0)
  73. return -1;
  74. if (fcb->open_mode == OPEN_OLD)
  75. return close_old(fd);
  76. return close_new(fd, 1);
  77. }
  78. /*!
  79. * \brief unopen a raster map
  80. *
  81. * The raster map
  82. * opened on file descriptor <b>fd</b> is closed. Memory allocated for raster
  83. * processing is freed. If open for writing, the raster map is not created and
  84. * the temporary file created when the raster map was opened is removed (see
  85. * Creating_and_Opening_New_Raster_Files).
  86. * This routine is useful when errors are detected and it is desired to not
  87. * create the new raster map. While it is true that the raster map will not be
  88. * created if the module exits without closing the file, the temporary file will
  89. * not be removed at module exit. GRASS database management will eventually
  90. * remove the temporary file, but the file can be quite large and will take up
  91. * disk space until GRASS does remove it. Use this routine as a courtesy to the
  92. * user.
  93. *
  94. * \param fd
  95. * \return int
  96. */
  97. int G_unopen_cell(int fd)
  98. {
  99. struct fileinfo *fcb = &G__.fileinfo[fd];
  100. if (fd < 0 || fd >= G__.fileinfo_count || fcb->open_mode <= 0)
  101. return -1;
  102. if (fcb->open_mode == OPEN_OLD)
  103. return close_old(fd);
  104. else
  105. return close_new(fd, 0);
  106. }
  107. static int close_old(int fd)
  108. {
  109. struct fileinfo *fcb = &G__.fileinfo[fd];
  110. int i;
  111. /* if G__.auto_mask was only allocated for reading map rows to create
  112. non-existant null rows, and not for actuall mask, free G__.mask_row
  113. if(G__.auto_mask <=0)
  114. G_free (G__.mask_buf);
  115. This is obsolete since now the mask_bus is always allocated
  116. */
  117. if (fcb->gdal)
  118. G_close_gdal_link(fcb->gdal);
  119. for (i = 0; i < NULL_ROWS_INMEM; i++)
  120. G_free(fcb->NULL_ROWS[i]);
  121. if (fcb->cellhd.compressed)
  122. G_free(fcb->row_ptr);
  123. G_free(fcb->col_map);
  124. G_free(fcb->mapset);
  125. G_free(fcb->data);
  126. G_free(fcb->name);
  127. if (fcb->reclass_flag)
  128. G_free_reclass(&fcb->reclass);
  129. fcb->open_mode = -1;
  130. if (fcb->map_type != CELL_TYPE) {
  131. G_quant_free(&fcb->quant);
  132. xdr_destroy(&fcb->xdrstream);
  133. }
  134. close(fd);
  135. return 1;
  136. }
  137. static int close_new(int fd, int ok)
  138. {
  139. struct fileinfo *fcb = &G__.fileinfo[fd];
  140. int stat;
  141. struct Categories cats;
  142. struct History hist;
  143. char path[GPATH_MAX];
  144. CELL cell_min, cell_max;
  145. int row, i, open_mode;
  146. const char *CELL_DIR;
  147. if (ok) {
  148. switch (fcb->open_mode) {
  149. case OPEN_NEW_COMPRESSED:
  150. G_debug(1, "close %s compressed", fcb->name);
  151. break;
  152. case OPEN_NEW_UNCOMPRESSED:
  153. G_debug(1, "close %s uncompressed", fcb->name);
  154. break;
  155. }
  156. if (fcb->cur_row < fcb->cellhd.rows) {
  157. G_zero_raster_buf(fcb->data, fcb->map_type);
  158. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  159. G_put_raster_row(fd, fcb->data, fcb->map_type);
  160. G_free(fcb->data);
  161. fcb->data = NULL;
  162. }
  163. /* create path : full null file name */
  164. G__make_mapset_element_misc("cell_misc", fcb->name);
  165. G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  166. G_mapset());
  167. remove(path);
  168. if (fcb->null_cur_row > 0) {
  169. /* if temporary NULL file exists, write it into cell_misc/name/null */
  170. int null_fd;
  171. null_fd = G__open_null_write(fd);
  172. if (null_fd <= 0)
  173. return -1;
  174. if (null_fd < 1)
  175. return -1;
  176. /* first finish writing null file */
  177. /* write out the rows stored in memory */
  178. for (row = fcb->min_null_row; row < fcb->null_cur_row; row++)
  179. G__write_null_bits(null_fd,
  180. fcb->NULL_ROWS[row - fcb->min_null_row],
  181. row, fcb->cellhd.cols, fd);
  182. /* write missing rows */
  183. if (fcb->null_cur_row < fcb->cellhd.rows) {
  184. unsigned char *null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
  185. G__init_null_bits(null_work_buf, fcb->cellhd.cols);
  186. for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++)
  187. G__write_null_bits(null_fd, null_work_buf, row,
  188. fcb->cellhd.cols, fd);
  189. G_free(null_work_buf);
  190. }
  191. close(null_fd);
  192. if (rename(fcb->null_temp_name, path)) {
  193. G_warning(_("closecell: can't move %s\nto null file %s"),
  194. fcb->null_temp_name, path);
  195. stat = -1;
  196. }
  197. else {
  198. remove(fcb->null_temp_name);
  199. }
  200. }
  201. else {
  202. remove(fcb->null_temp_name);
  203. remove(path);
  204. } /* null_cur_row > 0 */
  205. if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */
  206. fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR);
  207. G__write_row_ptrs(fd);
  208. }
  209. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  210. int cell_fd;
  211. if (write_fp_format(fd) != 0) {
  212. G_warning(_("Error writing floating point format file for map %s"),
  213. fcb->name);
  214. stat = -1;
  215. }
  216. /* now write 0-length cell file */
  217. G__make_mapset_element("cell");
  218. cell_fd =
  219. creat(G__file_name(path, "cell", fcb->name, fcb->mapset),
  220. 0666);
  221. close(cell_fd);
  222. CELL_DIR = "fcell";
  223. }
  224. else {
  225. /* remove fcell/name file */
  226. G__file_name(path, "fcell", fcb->name, fcb->mapset);
  227. remove(path);
  228. /* remove cell_misc/name/f_format */
  229. G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  230. fcb->mapset);
  231. remove(path);
  232. CELL_DIR = "cell";
  233. close(fd);
  234. }
  235. } /* ok */
  236. /* NOW CLOSE THE FILE DESCRIPTOR */
  237. close(fd);
  238. /* remember open_mode */
  239. open_mode = fcb->open_mode;
  240. fcb->open_mode = -1;
  241. if (fcb->data != NULL)
  242. G_free(fcb->data);
  243. if (fcb->null_temp_name != NULL) {
  244. G_free(fcb->null_temp_name);
  245. fcb->null_temp_name = NULL;
  246. }
  247. /* if the cell file was written to a temporary file
  248. * move this temporary file into the cell file
  249. * if the move fails, tell the user, but go ahead and create
  250. * the support files
  251. */
  252. stat = 1;
  253. if (ok && (fcb->temp_name != NULL)) {
  254. G__file_name(path, CELL_DIR, fcb->name, fcb->mapset);
  255. remove(path);
  256. if (rename(fcb->temp_name, path)) {
  257. G_warning(_("closecell: can't move %s\nto cell file %s"),
  258. fcb->temp_name, path);
  259. stat = -1;
  260. }
  261. else {
  262. remove(fcb->temp_name);
  263. }
  264. }
  265. if (fcb->temp_name != NULL) {
  266. G_free(fcb->temp_name);
  267. }
  268. if (ok) {
  269. /* remove color table */
  270. G_remove_colors(fcb->name, "");
  271. /* create a history file */
  272. G_short_history(fcb->name, "raster", &hist);
  273. G_write_history(fcb->name, &hist);
  274. /* write the range */
  275. if (fcb->map_type == CELL_TYPE) {
  276. G_write_range(fcb->name, &fcb->range);
  277. G__remove_fp_range(fcb->name);
  278. }
  279. /*NOTE: int range for floating point maps is not written out */
  280. else { /* if(fcb->map_type != CELL_TYPE) */
  281. G_write_fp_range(fcb->name, &fcb->fp_range);
  282. G_construct_default_range(&fcb->range);
  283. /* this range will be used to add default rule to quant structure */
  284. }
  285. if (fcb->map_type != CELL_TYPE)
  286. fcb->cellhd.format = -1;
  287. else /* CELL map */
  288. fcb->cellhd.format = fcb->nbytes - 1;
  289. /* write header file */
  290. G_put_cellhd(fcb->name, &fcb->cellhd);
  291. /* if map is floating point write the quant rules, otherwise remove f_quant */
  292. if (fcb->map_type != CELL_TYPE) {
  293. /* DEFAULT RANGE QUANT
  294. G_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max);
  295. if(!G_is_d_null_value(&dcell_min) && !G_is_d_null_value(&dcell_max))
  296. {
  297. G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  298. G_quant_add_rule(&fcb->quant, dcell_min, dcell_max,
  299. cell_min, cell_max);
  300. }
  301. */
  302. G_quant_round(&fcb->quant);
  303. if (G_write_quant(fcb->name, fcb->mapset, &fcb->quant) < 0)
  304. G_warning(_("unable to write quant file!"));
  305. }
  306. else {
  307. /* remove cell_misc/name/f_quant */
  308. G__file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name,
  309. fcb->mapset);
  310. remove(path);
  311. }
  312. /* create empty cats file */
  313. G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  314. if (G_is_c_null_value(&cell_max))
  315. cell_max = 0;
  316. G_init_cats(cell_max, (char *)NULL, &cats);
  317. G_write_cats(fcb->name, &cats);
  318. G_free_cats(&cats);
  319. /* write the histogram */
  320. /* only works for integer maps */
  321. if ((fcb->map_type == CELL_TYPE)
  322. && (fcb->want_histogram)) {
  323. G_write_histogram_cs(fcb->name, &fcb->statf);
  324. G_free_cell_stats(&fcb->statf);
  325. }
  326. else {
  327. G_remove_histogram(fcb->name);
  328. }
  329. } /* OK */
  330. G_free(fcb->name);
  331. G_free(fcb->mapset);
  332. for (i = 0; i < NULL_ROWS_INMEM; i++)
  333. G_free(fcb->NULL_ROWS[i]);
  334. if (fcb->map_type != CELL_TYPE)
  335. G_quant_free(&fcb->quant);
  336. return stat;
  337. }
  338. /* returns 0 on success, 1 on failure */
  339. static int write_fp_format(int fd)
  340. {
  341. struct fileinfo *fcb = &G__.fileinfo[fd];
  342. struct Key_Value *format_kv;
  343. char path[GPATH_MAX];
  344. int stat;
  345. if (fcb->map_type == CELL_TYPE) {
  346. G_warning(_("unable to write f_format file for CELL maps"));
  347. return 0;
  348. }
  349. format_kv = G_create_key_value();
  350. if (fcb->map_type == FCELL_TYPE)
  351. G_set_key_value("type", "float", format_kv);
  352. else
  353. G_set_key_value("type", "double", format_kv);
  354. G_set_key_value("byte_order", "xdr", format_kv);
  355. if (fcb->open_mode == OPEN_NEW_COMPRESSED)
  356. G_set_key_value("lzw_compression_bits", "-1", format_kv);
  357. G__make_mapset_element_misc("cell_misc", fcb->name);
  358. G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset);
  359. G_write_key_value_file(path, format_kv, &stat);
  360. G_free_key_value(format_kv);
  361. return stat;
  362. }