close.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*!
  2. * \file raster/close.c
  3. *
  4. * \brief Raster Library - Close raster file
  5. *
  6. * (C) 1999-2009 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author USACERL and many others
  13. */
  14. #ifdef __MINGW32__
  15. # include <windows.h>
  16. #endif
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <fcntl.h>
  22. #include <signal.h>
  23. #include <grass/gis.h>
  24. #include <grass/raster.h>
  25. #include <grass/glocale.h>
  26. #include "R.h"
  27. #define FORMAT_FILE "f_format"
  28. #define QUANT_FILE "f_quant"
  29. #define NULL_FILE "null"
  30. static int close_old(int);
  31. static int close_new(int, int);
  32. static void write_fp_format(int fd);
  33. /*!
  34. * \brief Close a raster map
  35. *
  36. * The raster map opened on file descriptor <i>fd</i> is
  37. * closed. Memory allocated for raster processing is freed. If open
  38. * for writing, skeletal support files for the new raster map are
  39. * created as well.
  40. *
  41. * <b>Note:</b> If a module wants to explicitly write support files
  42. * (e.g., a specific color table) for a raster map it creates, it must
  43. * do so after the raster map is closed. Otherwise the close will
  44. * overwrite the support files. See \ref
  45. * Raster_Map_Layer_Support_Routines for routines which write raster
  46. * support files.
  47. *
  48. * If the map is a new floating point, move the <tt>.tmp</tt> file
  49. * into the <tt>fcell</tt> element, create an empty file in the
  50. * <tt>cell</tt> directory; write the floating-point range file; write
  51. * a default quantization file quantization file is set here to round
  52. * fp numbers (this is a default for now). create an empty category
  53. * file, with max cat = max value (for backwards compatibility). Move
  54. * the <tt>.tmp</tt> NULL-value bitmap file to the <tt>cell_misc</tt>
  55. * directory.
  56. *
  57. * \param fd file descriptor
  58. *
  59. * \return void
  60. */
  61. void Rast_close(int fd)
  62. {
  63. struct fileinfo *fcb = &R__.fileinfo[fd];
  64. if (fd < 0 || fd >= R__.fileinfo_count || fcb->open_mode <= 0)
  65. G_fatal_error(_("Invalid descriptor: %d"), fd);
  66. if (fcb->open_mode == OPEN_OLD)
  67. close_old(fd);
  68. else
  69. close_new(fd, 1);
  70. }
  71. /*!
  72. * \brief Unopen a raster map
  73. *
  74. * The raster map opened on file descriptor <i>fd</i> is
  75. * closed. Memory allocated for raster processing is freed. If open
  76. * for writing, the raster map is not created and the temporary file
  77. * created when the raster map was opened is removed (see \ref
  78. * Creating_and_Opening_New_Raster_Files). This routine is useful when
  79. * errors are detected and it is desired to not create the new raster
  80. * map. While it is true that the raster map will not be created if
  81. * the module exits without closing the file, the temporary file will
  82. * not be removed at module exit. GRASS database management will
  83. * eventually remove the temporary file, but the file can be quite
  84. * large and will take up disk space until GRASS does remove it. Use
  85. * this routine as a courtesy to the user.
  86. *
  87. * \param fd file descriptor
  88. *
  89. * \return void
  90. */
  91. void Rast_unopen(int fd)
  92. {
  93. struct fileinfo *fcb = &R__.fileinfo[fd];
  94. if (fd < 0 || fd >= R__.fileinfo_count || fcb->open_mode <= 0)
  95. G_fatal_error(_("Invalid descriptor: %d"), fd);
  96. if (fcb->open_mode == OPEN_OLD)
  97. close_old(fd);
  98. else
  99. close_new(fd, 0);
  100. }
  101. /*!
  102. * \brief Unopen all raster maps
  103. *
  104. * Unopen all raster maps opened for write. Memory allocated for
  105. * raster processing is freed, and the temporary file created when the
  106. * raster map was opened is removed (see \ref
  107. * Creating_and_Opening_New_Raster_Files). This routine is useful when
  108. * errors are detected and it is desired to remove temporary files.
  109. *
  110. * \return void
  111. */
  112. void Rast__unopen_all(void)
  113. {
  114. int i;
  115. for (i = 0; i < R__.fileinfo_count; i++) {
  116. struct fileinfo *fcb = &R__.fileinfo[i];
  117. if (fcb->open_mode == OPEN_NEW_COMPRESSED ||
  118. fcb->open_mode == OPEN_NEW_UNCOMPRESSED)
  119. close_new(i, 0);
  120. }
  121. }
  122. static int close_old(int fd)
  123. {
  124. struct fileinfo *fcb = &R__.fileinfo[fd];
  125. /* if R__.auto_mask was only allocated for reading map rows to create
  126. non-existant null rows, and not for actuall mask, free R__.mask_row
  127. if(R__.auto_mask <=0)
  128. G_free (R__.mask_buf);
  129. This is obsolete since now the mask_bus is always allocated
  130. */
  131. if (fcb->gdal)
  132. Rast_close_gdal_link(fcb->gdal);
  133. if (fcb->null_bits)
  134. G_free(fcb->null_bits);
  135. if (fcb->null_fd >= 0)
  136. close(fcb->null_fd);
  137. fcb->null_fd = -1;
  138. if (fcb->cellhd.compressed)
  139. G_free(fcb->row_ptr);
  140. G_free(fcb->col_map);
  141. G_free(fcb->mapset);
  142. G_free(fcb->data);
  143. G_free(fcb->name);
  144. if (fcb->reclass_flag)
  145. Rast_free_reclass(&fcb->reclass);
  146. fcb->open_mode = -1;
  147. if (fcb->map_type != CELL_TYPE) {
  148. Rast_quant_free(&fcb->quant);
  149. }
  150. close(fcb->data_fd);
  151. return 1;
  152. }
  153. static void write_support_files(int fd)
  154. {
  155. struct fileinfo *fcb = &R__.fileinfo[fd];
  156. struct Categories cats;
  157. struct History hist;
  158. CELL cell_min, cell_max;
  159. char path[GPATH_MAX];
  160. /* remove color table */
  161. Rast_remove_colors(fcb->name, "");
  162. /* create a history file */
  163. Rast_short_history(fcb->name, "raster", &hist);
  164. Rast_write_history(fcb->name, &hist);
  165. /* write the range */
  166. if (fcb->map_type == CELL_TYPE) {
  167. Rast_write_range(fcb->name, &fcb->range);
  168. Rast__remove_fp_range(fcb->name);
  169. }
  170. /*NOTE: int range for floating point maps is not written out */
  171. else { /* if(fcb->map_type != CELL_TYPE) */
  172. Rast_write_fp_range(fcb->name, &fcb->fp_range);
  173. Rast_construct_default_range(&fcb->range);
  174. /* this range will be used to add default rule to quant structure */
  175. }
  176. if (fcb->map_type != CELL_TYPE)
  177. fcb->cellhd.format = -1;
  178. else /* CELL map */
  179. fcb->cellhd.format = fcb->nbytes - 1;
  180. /* write header file */
  181. Rast_put_cellhd(fcb->name, &fcb->cellhd);
  182. /* if map is floating point write the quant rules, otherwise remove f_quant */
  183. if (fcb->map_type != CELL_TYPE) {
  184. /* DEFAULT RANGE QUANT
  185. Rast_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max);
  186. if(!Rast_is_d_null_value(&dcell_min) && !Rast_is_d_null_value(&dcell_max))
  187. {
  188. Rast_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  189. Rast_quant_add_rule(&fcb->quant, dcell_min, dcell_max,
  190. cell_min, cell_max);
  191. }
  192. */
  193. Rast_quant_round(&fcb->quant);
  194. Rast_write_quant(fcb->name, fcb->mapset, &fcb->quant);
  195. }
  196. else {
  197. /* remove cell_misc/name/f_quant */
  198. G_file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name,
  199. fcb->mapset);
  200. remove(path);
  201. }
  202. /* create empty cats file */
  203. Rast_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  204. if (Rast_is_c_null_value(&cell_max))
  205. cell_max = 0;
  206. Rast_init_cats((char *)NULL, &cats);
  207. Rast_write_cats(fcb->name, &cats);
  208. Rast_free_cats(&cats);
  209. /* write the histogram */
  210. /* only works for integer maps */
  211. if ((fcb->map_type == CELL_TYPE)
  212. && (fcb->want_histogram)) {
  213. Rast_write_histogram_cs(fcb->name, &fcb->statf);
  214. Rast_free_cell_stats(&fcb->statf);
  215. }
  216. else {
  217. Rast_remove_histogram(fcb->name);
  218. }
  219. }
  220. static int close_new_gdal(int fd, int ok)
  221. {
  222. struct fileinfo *fcb = &R__.fileinfo[fd];
  223. char path[GPATH_MAX];
  224. int stat = 1;
  225. if (ok) {
  226. int cell_fd;
  227. G_debug(1, "close %s GDAL", fcb->name);
  228. if (fcb->cur_row < fcb->cellhd.rows) {
  229. int row;
  230. Rast_zero_output_buf(fcb->data, fcb->map_type);
  231. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  232. Rast_put_row(fd, fcb->data, fcb->map_type);
  233. G_free(fcb->data);
  234. fcb->data = NULL;
  235. }
  236. /* create path : full null file name */
  237. G__make_mapset_element_misc("cell_misc", fcb->name);
  238. G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  239. G_mapset());
  240. remove(path);
  241. /* write 0-length cell file */
  242. G__make_mapset_element("cell");
  243. G_file_name(path, "cell", fcb->name, fcb->mapset);
  244. cell_fd = creat(path, 0666);
  245. close(cell_fd);
  246. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  247. write_fp_format(fd);
  248. /* write 0-length fcell file */
  249. G__make_mapset_element("fcell");
  250. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  251. cell_fd = creat(path, 0666);
  252. close(cell_fd);
  253. }
  254. else {
  255. /* remove fcell/name file */
  256. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  257. remove(path);
  258. /* remove cell_misc/name/f_format */
  259. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  260. fcb->mapset);
  261. remove(path);
  262. }
  263. if (Rast_close_gdal_write_link(fcb->gdal) < 0)
  264. stat = -1;
  265. }
  266. else {
  267. remove(fcb->gdal->filename);
  268. Rast_close_gdal_link(fcb->gdal);
  269. }
  270. fcb->open_mode = -1;
  271. if (fcb->data != NULL)
  272. G_free(fcb->data);
  273. if (ok)
  274. write_support_files(fd);
  275. G_free(fcb->name);
  276. G_free(fcb->mapset);
  277. if (fcb->map_type != CELL_TYPE)
  278. Rast_quant_free(&fcb->quant);
  279. return stat;
  280. }
  281. static int close_new(int fd, int ok)
  282. {
  283. struct fileinfo *fcb = &R__.fileinfo[fd];
  284. int stat;
  285. char path[GPATH_MAX];
  286. int row;
  287. const char *CELL_DIR;
  288. if (fcb->gdal)
  289. return close_new_gdal(fd, ok);
  290. if (fcb->null_fd >= 0)
  291. close(fcb->null_fd);
  292. fcb->null_fd = -1;
  293. if (ok) {
  294. switch (fcb->open_mode) {
  295. case OPEN_NEW_COMPRESSED:
  296. G_debug(1, "close %s compressed", fcb->name);
  297. break;
  298. case OPEN_NEW_UNCOMPRESSED:
  299. G_debug(1, "close %s uncompressed", fcb->name);
  300. break;
  301. }
  302. if (fcb->cur_row < fcb->cellhd.rows) {
  303. Rast_zero_output_buf(fcb->data, fcb->map_type);
  304. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  305. Rast_put_row(fd, fcb->data, fcb->map_type);
  306. G_free(fcb->data);
  307. fcb->data = NULL;
  308. }
  309. /* create path : full null file name */
  310. G__make_mapset_element_misc("cell_misc", fcb->name);
  311. G_file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  312. G_mapset());
  313. remove(path);
  314. if (fcb->null_cur_row > 0) {
  315. /* if temporary NULL file exists, write it into cell_misc/name/null */
  316. if (rename(fcb->null_temp_name, path)) {
  317. G_warning(_("Unable to rename null file '%s' to '%s'"),
  318. fcb->null_temp_name, path);
  319. stat = -1;
  320. }
  321. else {
  322. remove(fcb->null_temp_name);
  323. }
  324. }
  325. else {
  326. remove(fcb->null_temp_name);
  327. remove(path);
  328. } /* null_cur_row > 0 */
  329. if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */
  330. fcb->row_ptr[fcb->cellhd.rows] = lseek(fcb->data_fd, 0L, SEEK_CUR);
  331. Rast__write_row_ptrs(fd);
  332. }
  333. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  334. int cell_fd;
  335. write_fp_format(fd);
  336. /* now write 0-length cell file */
  337. G__make_mapset_element("cell");
  338. cell_fd =
  339. creat(G_file_name(path, "cell", fcb->name, fcb->mapset),
  340. 0666);
  341. close(cell_fd);
  342. CELL_DIR = "fcell";
  343. }
  344. else {
  345. /* remove fcell/name file */
  346. G_file_name(path, "fcell", fcb->name, fcb->mapset);
  347. remove(path);
  348. /* remove cell_misc/name/f_format */
  349. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  350. fcb->mapset);
  351. remove(path);
  352. CELL_DIR = "cell";
  353. close(fcb->data_fd);
  354. }
  355. } /* ok */
  356. /* NOW CLOSE THE FILE DESCRIPTOR */
  357. close(fcb->data_fd);
  358. fcb->open_mode = -1;
  359. if (fcb->data != NULL)
  360. G_free(fcb->data);
  361. if (fcb->null_temp_name != NULL) {
  362. G_free(fcb->null_temp_name);
  363. fcb->null_temp_name = NULL;
  364. }
  365. /* if the cell file was written to a temporary file
  366. * move this temporary file into the cell file
  367. * if the move fails, tell the user, but go ahead and create
  368. * the support files
  369. */
  370. stat = 1;
  371. if (ok && (fcb->temp_name != NULL)) {
  372. G_file_name(path, CELL_DIR, fcb->name, fcb->mapset);
  373. remove(path);
  374. if (rename(fcb->temp_name, path)) {
  375. G_warning(_("Unable to rename cell file '%s' to '%s'"),
  376. fcb->temp_name, path);
  377. stat = -1;
  378. }
  379. else {
  380. remove(fcb->temp_name);
  381. }
  382. }
  383. if (fcb->temp_name != NULL) {
  384. G_free(fcb->temp_name);
  385. }
  386. if (ok)
  387. write_support_files(fd);
  388. G_free(fcb->name);
  389. G_free(fcb->mapset);
  390. G_free(fcb->null_bits);
  391. if (fcb->map_type != CELL_TYPE)
  392. Rast_quant_free(&fcb->quant);
  393. return stat;
  394. }
  395. /* returns 0 on success, 1 on failure */
  396. static void write_fp_format(int fd)
  397. {
  398. struct fileinfo *fcb = &R__.fileinfo[fd];
  399. struct Key_Value *format_kv;
  400. char path[GPATH_MAX];
  401. if (fcb->map_type == CELL_TYPE) {
  402. G_warning(_("unable to write f_format file for CELL maps"));
  403. return;
  404. }
  405. format_kv = G_create_key_value();
  406. if (fcb->map_type == FCELL_TYPE)
  407. G_set_key_value("type", "float", format_kv);
  408. else
  409. G_set_key_value("type", "double", format_kv);
  410. G_set_key_value("byte_order", "xdr", format_kv);
  411. if (fcb->open_mode == OPEN_NEW_COMPRESSED)
  412. G_set_key_value("lzw_compression_bits", "-1", format_kv);
  413. G__make_mapset_element_misc("cell_misc", fcb->name);
  414. G_file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset);
  415. G_write_key_value_file(path, format_kv);
  416. G_free_key_value(format_kv);
  417. }