main.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. **********************************************************************
  3. *
  4. * MODULE: r.support (GRASS core)
  5. *
  6. * AUTHOR(S): Original by Michael Shapiro - CERL
  7. * Preliminary parser support by Markus Neteler, rast parameter
  8. * Port to 6.x by Brad Douglas
  9. *
  10. * PURPOSE: Build support files for raster map
  11. * - Edit header
  12. * - Update status (histogram, range)
  13. *
  14. * COPYRIGHT: (C) 2000-2007 by the GRASS Development Team
  15. *
  16. * This program is free software under the GNU General
  17. * Public License (>=v2). Read the file COPYING that comes
  18. * with GRASS for details.
  19. *
  20. **********************************************************************/
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <grass/gis.h>
  25. #include <grass/raster.h>
  26. #include <grass/glocale.h>
  27. #include "local_proto.h"
  28. /* two less than lib/gis/put_title.c Rast_put_cell_title()
  29. if only one less a newline gets appended in the cats file. bug? */
  30. #define MAX_TITLE_LEN 1022
  31. int main(int argc, char *argv[])
  32. {
  33. char rname[GNAME_MAX]; /* Reclassed map name */
  34. char rmapset[GMAPSET_MAX]; /* Reclassed mapset */
  35. const char *mapset; /* Raster mapset */
  36. struct Cell_head cellhd;
  37. struct GModule *module;
  38. struct Option *raster, *title_opt, *history_opt;
  39. struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt;
  40. struct Option *map_opt, *units_opt, *vdatum_opt;
  41. struct Option *load_opt, *save_opt;
  42. struct Flag *stats_flag, *null_flag, *del_flag;
  43. int is_reclass; /* Is raster reclass? */
  44. const char *infile;
  45. struct History hist;
  46. /* Initialize GIS engine */
  47. G_gisinit(argv[0]);
  48. module = G_define_module();
  49. G_add_keyword(_("raster"));
  50. G_add_keyword(_("metadata"));
  51. module->description = _("Allows creation and/or modification of "
  52. "raster map layer support files.");
  53. raster = G_define_standard_option(G_OPT_R_MAP);
  54. title_opt = G_define_option();
  55. title_opt->key = "title";
  56. title_opt->key_desc = "phrase";
  57. title_opt->type = TYPE_STRING;
  58. title_opt->required = NO;
  59. title_opt->description = _("Title for resultant raster map");
  60. history_opt = G_define_option();
  61. history_opt->key = "history";
  62. history_opt->key_desc = "phrase";
  63. history_opt->type = TYPE_STRING;
  64. history_opt->required = NO;
  65. history_opt->description =
  66. _("Text to append to the next line of the map's metadata file");
  67. units_opt = G_define_option();
  68. units_opt->key = "units";
  69. units_opt->type = TYPE_STRING;
  70. units_opt->required = NO;
  71. units_opt->description = _("Text to use for map data units");
  72. vdatum_opt = G_define_option();
  73. vdatum_opt->key = "vdatum";
  74. vdatum_opt->type = TYPE_STRING;
  75. vdatum_opt->required = NO;
  76. vdatum_opt->description = _("Text to use for map vertical datum");
  77. datasrc1_opt = G_define_option();
  78. datasrc1_opt->key = "source1";
  79. datasrc1_opt->key_desc = "phrase";
  80. datasrc1_opt->type = TYPE_STRING;
  81. datasrc1_opt->required = NO;
  82. datasrc1_opt->description = _("Text to use for data source, line 1");
  83. datasrc2_opt = G_define_option();
  84. datasrc2_opt->key = "source2";
  85. datasrc2_opt->key_desc = "phrase";
  86. datasrc2_opt->type = TYPE_STRING;
  87. datasrc2_opt->required = NO;
  88. datasrc2_opt->description = _("Text to use for data source, line 2");
  89. datadesc_opt = G_define_option();
  90. datadesc_opt->key = "description";
  91. datadesc_opt->key_desc = "phrase";
  92. datadesc_opt->type = TYPE_STRING;
  93. datadesc_opt->required = NO;
  94. datadesc_opt->description =
  95. _("Text to use for data description or keyword(s)");
  96. map_opt = G_define_option();
  97. map_opt->key = "raster";
  98. map_opt->type = TYPE_STRING;
  99. map_opt->required = NO;
  100. map_opt->gisprompt = "old,cell,raster";
  101. map_opt->description = _("Raster map from which to copy category table");
  102. load_opt = G_define_standard_option(G_OPT_F_INPUT);
  103. load_opt->key = "loadhistory";
  104. load_opt->required = NO;
  105. load_opt->description = _("Text file from which to load history");
  106. save_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  107. save_opt->key = "savehistory";
  108. save_opt->required = NO;
  109. save_opt->description = _("Text file in which to save history");
  110. stats_flag = G_define_flag();
  111. stats_flag->key = 's';
  112. stats_flag->description = _("Update statistics (histogram, range)");
  113. null_flag = G_define_flag();
  114. null_flag->key = 'n';
  115. null_flag->description = _("Create/reset the null file");
  116. del_flag = G_define_flag();
  117. del_flag->key = 'd';
  118. del_flag->description = _("Delete the null file");
  119. /* Parse command-line options */
  120. if (G_parser(argc, argv))
  121. exit(EXIT_FAILURE);
  122. /* Make sure raster exists and set mapset */
  123. infile = raster->answer;
  124. mapset = G_find_raster2(infile, G_mapset()); /* current mapset only for editing */
  125. if (!mapset || strcmp(mapset, G_mapset()) != 0)
  126. G_fatal_error(_("Raster map <%s> not found in current mapset"), infile);
  127. Rast_get_cellhd(raster->answer, "", &cellhd);
  128. is_reclass = (Rast_is_reclass(raster->answer, "", rname, rmapset) > 0);
  129. if (title_opt->answer) {
  130. Rast_put_cell_title(raster->answer, title_opt->answer);
  131. G_debug(3, "map title= [%s] (%li chars)", title_opt->answer, strlen(title_opt->answer));
  132. }
  133. if (save_opt->answer) {
  134. FILE *fp = fopen(save_opt->answer, "w");
  135. int i;
  136. if (!fp)
  137. G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer);
  138. Rast_read_history(raster->answer, "", &hist);
  139. for (i = 0; i < Rast_history_length(&hist); i++)
  140. fprintf(fp, "%s\n", Rast_history_line(&hist, i));
  141. fclose(fp);
  142. }
  143. if (load_opt->answer) {
  144. FILE *fp = fopen(load_opt->answer, "r");
  145. if (!fp)
  146. G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer);
  147. Rast_read_history(raster->answer, "", &hist);
  148. Rast_clear_history(&hist);
  149. for (;;) {
  150. char buf[80];
  151. if (!G_getl2(buf, sizeof(buf), fp))
  152. break;
  153. Rast_append_history(&hist, buf);
  154. }
  155. fclose(fp);
  156. Rast_write_history(raster->answer, &hist);
  157. }
  158. if (history_opt->answer) {
  159. Rast_read_history(raster->answer, "", &hist);
  160. /* two less than defined as if only one less a newline gets appended in the hist file. bug? */
  161. /* Should be RECORD_LEN, but r.info truncates at > 71 chars */
  162. if (strlen(history_opt->answer) > 71) {
  163. int i;
  164. for (i = 0; i < strlen(history_opt->answer); i += 71) {
  165. char buf[72];
  166. strncpy(buf, &history_opt->answer[i], sizeof(buf)-1);
  167. buf[sizeof(buf)-1] = '\0';
  168. Rast_append_history(&hist, buf);
  169. }
  170. }
  171. else
  172. Rast_append_history(&hist, history_opt->answer);
  173. Rast_write_history(raster->answer, &hist);
  174. }
  175. if (units_opt->answer)
  176. Rast_write_units(raster->answer, units_opt->answer);
  177. if (vdatum_opt->answer)
  178. Rast_write_vdatum(raster->answer, vdatum_opt->answer);
  179. if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
  180. Rast_read_history(raster->answer, "", &hist);
  181. if (datasrc1_opt->answer)
  182. Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer);
  183. if (datasrc2_opt->answer)
  184. Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer);
  185. if (datadesc_opt->answer)
  186. Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer);
  187. Rast_write_history(raster->answer, &hist);
  188. }
  189. if (map_opt->answer) { /* use cats from another map */
  190. int fd;
  191. struct Categories cats;
  192. fd = Rast_open_old(infile, "");
  193. Rast_init_cats("", &cats);
  194. if (Rast_read_cats(map_opt->answer, "", &cats) < 0)
  195. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  196. map_opt->answer);
  197. Rast_write_cats(infile, &cats);
  198. G_message(_("cats table for [%s] set to %s"),
  199. infile, map_opt->answer);
  200. Rast_close(fd);
  201. Rast_free_cats(&cats);
  202. }
  203. if (title_opt->answer || history_opt->answer || units_opt->answer
  204. || vdatum_opt->answer || datasrc1_opt->answer || datasrc2_opt->answer
  205. || datadesc_opt->answer || map_opt->answer)
  206. exit(EXIT_SUCCESS);
  207. /* Check the histogram and range */
  208. if (stats_flag->answer)
  209. check_stats(raster->answer);
  210. /* null file */
  211. if (null_flag->answer) {
  212. unsigned char *null_bits;
  213. int row, col;
  214. int fd;
  215. if (is_reclass)
  216. G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
  217. raster->answer);
  218. /* Create a file of no-nulls */
  219. null_bits = Rast__allocate_null_bits(cellhd.cols);
  220. for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++)
  221. null_bits[col] = 0;
  222. /* Open null file for writing */
  223. Rast_set_window(&cellhd);
  224. fd = Rast__open_null_write(raster->answer);
  225. G_message(_("Writing new null file for [%s]... "), raster->answer);
  226. for (row = 0; row < cellhd.rows; row++) {
  227. G_percent(row, cellhd.rows, 1);
  228. Rast__write_null_bits(fd, null_bits);
  229. }
  230. G_percent(row, cellhd.rows, 1);
  231. /* Cleanup */
  232. Rast__close_null(fd);
  233. G_free(null_bits);
  234. }
  235. if (del_flag->answer) {
  236. char path[GPATH_MAX];
  237. if (is_reclass)
  238. G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
  239. raster->answer);
  240. /* Write a file of no-nulls */
  241. G_message(_("Removing null file for [%s]...\n"), raster->answer);
  242. G_file_name_misc(path, "cell_misc", "null", raster->answer, G_mapset());
  243. unlink(path);
  244. G_file_name_misc(path, "cell_misc", "nullcmpr", raster->answer, G_mapset());
  245. unlink(path);
  246. G_done_msg(_("Done."));
  247. }
  248. return EXIT_SUCCESS;
  249. }