closecell.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 void write_support_files(int fd)
  138. {
  139. struct fileinfo *fcb = &G__.fileinfo[fd];
  140. struct Categories cats;
  141. struct History hist;
  142. CELL cell_min, cell_max;
  143. char path[GPATH_MAX];
  144. /* remove color table */
  145. G_remove_colors(fcb->name, "");
  146. /* create a history file */
  147. G_short_history(fcb->name, "raster", &hist);
  148. G_write_history(fcb->name, &hist);
  149. /* write the range */
  150. if (fcb->map_type == CELL_TYPE) {
  151. G_write_range(fcb->name, &fcb->range);
  152. G__remove_fp_range(fcb->name);
  153. }
  154. /*NOTE: int range for floating point maps is not written out */
  155. else { /* if(fcb->map_type != CELL_TYPE) */
  156. G_write_fp_range(fcb->name, &fcb->fp_range);
  157. G_construct_default_range(&fcb->range);
  158. /* this range will be used to add default rule to quant structure */
  159. }
  160. if (fcb->map_type != CELL_TYPE)
  161. fcb->cellhd.format = -1;
  162. else /* CELL map */
  163. fcb->cellhd.format = fcb->nbytes - 1;
  164. /* write header file */
  165. G_put_cellhd(fcb->name, &fcb->cellhd);
  166. /* if map is floating point write the quant rules, otherwise remove f_quant */
  167. if (fcb->map_type != CELL_TYPE) {
  168. /* DEFAULT RANGE QUANT
  169. G_get_fp_range_min_max(&fcb->fp_range, &dcell_min, &dcell_max);
  170. if(!G_is_d_null_value(&dcell_min) && !G_is_d_null_value(&dcell_max))
  171. {
  172. G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  173. G_quant_add_rule(&fcb->quant, dcell_min, dcell_max,
  174. cell_min, cell_max);
  175. }
  176. */
  177. G_quant_round(&fcb->quant);
  178. if (G_write_quant(fcb->name, fcb->mapset, &fcb->quant) < 0)
  179. G_warning(_("unable to write quant file!"));
  180. }
  181. else {
  182. /* remove cell_misc/name/f_quant */
  183. G__file_name_misc(path, "cell_misc", QUANT_FILE, fcb->name,
  184. fcb->mapset);
  185. remove(path);
  186. }
  187. /* create empty cats file */
  188. G_get_range_min_max(&fcb->range, &cell_min, &cell_max);
  189. if (G_is_c_null_value(&cell_max))
  190. cell_max = 0;
  191. G_init_cats(cell_max, (char *)NULL, &cats);
  192. G_write_cats(fcb->name, &cats);
  193. G_free_cats(&cats);
  194. /* write the histogram */
  195. /* only works for integer maps */
  196. if ((fcb->map_type == CELL_TYPE)
  197. && (fcb->want_histogram)) {
  198. G_write_histogram_cs(fcb->name, &fcb->statf);
  199. G_free_cell_stats(&fcb->statf);
  200. }
  201. else {
  202. G_remove_histogram(fcb->name);
  203. }
  204. }
  205. static int close_new_gdal(int fd, int ok)
  206. {
  207. struct fileinfo *fcb = &G__.fileinfo[fd];
  208. char path[GPATH_MAX];
  209. int stat = 1;
  210. if (ok) {
  211. int cell_fd;
  212. G_debug(1, "close %s GDAL", fcb->name);
  213. if (fcb->cur_row < fcb->cellhd.rows) {
  214. int row;
  215. G_zero_raster_buf(fcb->data, fcb->map_type);
  216. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  217. G_put_raster_row(fd, fcb->data, fcb->map_type);
  218. G_free(fcb->data);
  219. fcb->data = NULL;
  220. }
  221. /* create path : full null file name */
  222. G__make_mapset_element_misc("cell_misc", fcb->name);
  223. G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  224. G_mapset());
  225. remove(path);
  226. /* write 0-length cell file */
  227. G__make_mapset_element("cell");
  228. G__file_name(path, "cell", fcb->name, fcb->mapset);
  229. cell_fd = creat(path, 0666);
  230. close(cell_fd);
  231. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  232. if (write_fp_format(fd) != 0) {
  233. G_warning(_("Error writing floating point format file for map %s"),
  234. fcb->name);
  235. stat = -1;
  236. }
  237. /* write 0-length fcell file */
  238. G__make_mapset_element("fcell");
  239. G__file_name(path, "fcell", fcb->name, fcb->mapset);
  240. cell_fd = creat(path, 0666);
  241. close(cell_fd);
  242. }
  243. else {
  244. /* remove fcell/name file */
  245. G__file_name(path, "fcell", fcb->name, fcb->mapset);
  246. remove(path);
  247. /* remove cell_misc/name/f_format */
  248. G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  249. fcb->mapset);
  250. remove(path);
  251. }
  252. if (G_close_gdal_write_link(fcb->gdal) < 0)
  253. stat = -1;
  254. }
  255. else {
  256. remove(fcb->gdal->filename);
  257. G_close_gdal_link(fcb->gdal);
  258. }
  259. /* NOW CLOSE THE FILE DESCRIPTOR */
  260. close(fd);
  261. fcb->open_mode = -1;
  262. if (fcb->data != NULL)
  263. G_free(fcb->data);
  264. if (ok)
  265. write_support_files(fd);
  266. G_free(fcb->name);
  267. G_free(fcb->mapset);
  268. if (fcb->map_type != CELL_TYPE)
  269. G_quant_free(&fcb->quant);
  270. return stat;
  271. }
  272. static int close_new(int fd, int ok)
  273. {
  274. struct fileinfo *fcb = &G__.fileinfo[fd];
  275. int stat;
  276. char path[GPATH_MAX];
  277. int row, i;
  278. const char *CELL_DIR;
  279. if (fcb->gdal)
  280. return close_new_gdal(fd, ok);
  281. if (ok) {
  282. switch (fcb->open_mode) {
  283. case OPEN_NEW_COMPRESSED:
  284. G_debug(1, "close %s compressed", fcb->name);
  285. break;
  286. case OPEN_NEW_UNCOMPRESSED:
  287. G_debug(1, "close %s uncompressed", fcb->name);
  288. break;
  289. }
  290. if (fcb->cur_row < fcb->cellhd.rows) {
  291. G_zero_raster_buf(fcb->data, fcb->map_type);
  292. for (row = fcb->cur_row; row < fcb->cellhd.rows; row++)
  293. G_put_raster_row(fd, fcb->data, fcb->map_type);
  294. G_free(fcb->data);
  295. fcb->data = NULL;
  296. }
  297. /* create path : full null file name */
  298. G__make_mapset_element_misc("cell_misc", fcb->name);
  299. G__file_name_misc(path, "cell_misc", NULL_FILE, fcb->name,
  300. G_mapset());
  301. remove(path);
  302. if (fcb->null_cur_row > 0) {
  303. /* if temporary NULL file exists, write it into cell_misc/name/null */
  304. int null_fd;
  305. null_fd = G__open_null_write(fd);
  306. if (null_fd <= 0)
  307. return -1;
  308. if (null_fd < 1)
  309. return -1;
  310. /* first finish writing null file */
  311. /* write out the rows stored in memory */
  312. for (row = fcb->min_null_row; row < fcb->null_cur_row; row++)
  313. G__write_null_bits(null_fd,
  314. fcb->NULL_ROWS[row - fcb->min_null_row],
  315. row, fcb->cellhd.cols, fd);
  316. /* write missing rows */
  317. if (fcb->null_cur_row < fcb->cellhd.rows) {
  318. unsigned char *null_work_buf = G__allocate_null_bits(fcb->cellhd.cols);
  319. G__init_null_bits(null_work_buf, fcb->cellhd.cols);
  320. for (row = fcb->null_cur_row; row < fcb->cellhd.rows; row++)
  321. G__write_null_bits(null_fd, null_work_buf, row,
  322. fcb->cellhd.cols, fd);
  323. G_free(null_work_buf);
  324. }
  325. close(null_fd);
  326. if (rename(fcb->null_temp_name, path)) {
  327. G_warning(_("closecell: can't move %s\nto null file %s"),
  328. fcb->null_temp_name, path);
  329. stat = -1;
  330. }
  331. else {
  332. remove(fcb->null_temp_name);
  333. }
  334. }
  335. else {
  336. remove(fcb->null_temp_name);
  337. remove(path);
  338. } /* null_cur_row > 0 */
  339. if (fcb->open_mode == OPEN_NEW_COMPRESSED) { /* auto compression */
  340. fcb->row_ptr[fcb->cellhd.rows] = lseek(fd, 0L, SEEK_CUR);
  341. G__write_row_ptrs(fd);
  342. }
  343. if (fcb->map_type != CELL_TYPE) { /* floating point map */
  344. int cell_fd;
  345. if (write_fp_format(fd) != 0) {
  346. G_warning(_("Error writing floating point format file for map %s"),
  347. fcb->name);
  348. stat = -1;
  349. }
  350. /* now write 0-length cell file */
  351. G__make_mapset_element("cell");
  352. cell_fd =
  353. creat(G__file_name(path, "cell", fcb->name, fcb->mapset),
  354. 0666);
  355. close(cell_fd);
  356. CELL_DIR = "fcell";
  357. }
  358. else {
  359. /* remove fcell/name file */
  360. G__file_name(path, "fcell", fcb->name, fcb->mapset);
  361. remove(path);
  362. /* remove cell_misc/name/f_format */
  363. G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name,
  364. fcb->mapset);
  365. remove(path);
  366. CELL_DIR = "cell";
  367. close(fd);
  368. }
  369. } /* ok */
  370. /* NOW CLOSE THE FILE DESCRIPTOR */
  371. close(fd);
  372. fcb->open_mode = -1;
  373. if (fcb->data != NULL)
  374. G_free(fcb->data);
  375. if (fcb->null_temp_name != NULL) {
  376. G_free(fcb->null_temp_name);
  377. fcb->null_temp_name = NULL;
  378. }
  379. /* if the cell file was written to a temporary file
  380. * move this temporary file into the cell file
  381. * if the move fails, tell the user, but go ahead and create
  382. * the support files
  383. */
  384. stat = 1;
  385. if (ok && (fcb->temp_name != NULL)) {
  386. G__file_name(path, CELL_DIR, fcb->name, fcb->mapset);
  387. remove(path);
  388. if (rename(fcb->temp_name, path)) {
  389. G_warning(_("closecell: can't move %s\nto cell file %s"),
  390. fcb->temp_name, path);
  391. stat = -1;
  392. }
  393. else {
  394. remove(fcb->temp_name);
  395. }
  396. }
  397. if (fcb->temp_name != NULL) {
  398. G_free(fcb->temp_name);
  399. }
  400. if (ok)
  401. write_support_files(fd);
  402. G_free(fcb->name);
  403. G_free(fcb->mapset);
  404. for (i = 0; i < NULL_ROWS_INMEM; i++)
  405. G_free(fcb->NULL_ROWS[i]);
  406. if (fcb->map_type != CELL_TYPE)
  407. G_quant_free(&fcb->quant);
  408. return stat;
  409. }
  410. /* returns 0 on success, 1 on failure */
  411. static int write_fp_format(int fd)
  412. {
  413. struct fileinfo *fcb = &G__.fileinfo[fd];
  414. struct Key_Value *format_kv;
  415. char path[GPATH_MAX];
  416. int stat;
  417. if (fcb->map_type == CELL_TYPE) {
  418. G_warning(_("unable to write f_format file for CELL maps"));
  419. return 0;
  420. }
  421. format_kv = G_create_key_value();
  422. if (fcb->map_type == FCELL_TYPE)
  423. G_set_key_value("type", "float", format_kv);
  424. else
  425. G_set_key_value("type", "double", format_kv);
  426. G_set_key_value("byte_order", "xdr", format_kv);
  427. if (fcb->open_mode == OPEN_NEW_COMPRESSED)
  428. G_set_key_value("lzw_compression_bits", "-1", format_kv);
  429. G__make_mapset_element_misc("cell_misc", fcb->name);
  430. G__file_name_misc(path, "cell_misc", FORMAT_FILE, fcb->name, fcb->mapset);
  431. G_write_key_value_file(path, format_kv, &stat);
  432. G_free_key_value(format_kv);
  433. return stat;
  434. }