closecell.c 12 KB

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