main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. char title[MAX_TITLE_LEN + 1];
  46. struct History hist;
  47. /* Initialize GIS engine */
  48. G_gisinit(argv[0]);
  49. module = G_define_module();
  50. G_add_keyword(_("raster"));
  51. G_add_keyword(_("metadata"));
  52. module->description = _("Allows creation and/or modification of "
  53. "raster map layer support files.");
  54. raster = G_define_standard_option(G_OPT_R_MAP);
  55. title_opt = G_define_option();
  56. title_opt->key = "title";
  57. title_opt->key_desc = "phrase";
  58. title_opt->type = TYPE_STRING;
  59. title_opt->required = NO;
  60. title_opt->description = _("Text to use for map title");
  61. history_opt = G_define_option();
  62. history_opt->key = "history";
  63. history_opt->key_desc = "phrase";
  64. history_opt->type = TYPE_STRING;
  65. history_opt->required = NO;
  66. history_opt->description =
  67. _("Text to append to the next line of the map's metadata file");
  68. units_opt = G_define_option();
  69. units_opt->key = "units";
  70. units_opt->type = TYPE_STRING;
  71. units_opt->required = NO;
  72. units_opt->description = _("Text to use for map data units");
  73. vdatum_opt = G_define_option();
  74. vdatum_opt->key = "vdatum";
  75. vdatum_opt->type = TYPE_STRING;
  76. vdatum_opt->required = NO;
  77. vdatum_opt->description = _("Text to use for map vertical datum");
  78. datasrc1_opt = G_define_option();
  79. datasrc1_opt->key = "source1";
  80. datasrc1_opt->key_desc = "phrase";
  81. datasrc1_opt->type = TYPE_STRING;
  82. datasrc1_opt->required = NO;
  83. datasrc1_opt->description = _("Text to use for data source, line 1");
  84. datasrc2_opt = G_define_option();
  85. datasrc2_opt->key = "source2";
  86. datasrc2_opt->key_desc = "phrase";
  87. datasrc2_opt->type = TYPE_STRING;
  88. datasrc2_opt->required = NO;
  89. datasrc2_opt->description = _("Text to use for data source, line 2");
  90. datadesc_opt = G_define_option();
  91. datadesc_opt->key = "description";
  92. datadesc_opt->key_desc = "phrase";
  93. datadesc_opt->type = TYPE_STRING;
  94. datadesc_opt->required = NO;
  95. datadesc_opt->description =
  96. _("Text to use for data description or keyword(s)");
  97. map_opt = G_define_option();
  98. map_opt->key = "raster";
  99. map_opt->type = TYPE_STRING;
  100. map_opt->required = NO;
  101. map_opt->gisprompt = "old,cell,raster";
  102. map_opt->description = _("Raster map from which to copy category table");
  103. load_opt = G_define_standard_option(G_OPT_F_INPUT);
  104. load_opt->key = "loadhistory";
  105. load_opt->required = NO;
  106. load_opt->description = _("Text file from which to load history");
  107. save_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  108. save_opt->key = "savehistory";
  109. save_opt->required = NO;
  110. save_opt->description = _("Text file in which to save history");
  111. stats_flag = G_define_flag();
  112. stats_flag->key = 's';
  113. stats_flag->description = _("Update statistics (histogram, range)");
  114. null_flag = G_define_flag();
  115. null_flag->key = 'n';
  116. null_flag->description = _("Create/reset the null file");
  117. del_flag = G_define_flag();
  118. del_flag->key = 'd';
  119. del_flag->description = _("Delete the null file");
  120. /* Parse command-line options */
  121. if (G_parser(argc, argv))
  122. exit(EXIT_FAILURE);
  123. /* Make sure raster exists and set mapset */
  124. infile = raster->answer;
  125. mapset = G_find_raster2(infile, G_mapset()); /* current mapset only for editing */
  126. if (!mapset || strcmp(mapset, G_mapset()) != 0)
  127. G_fatal_error(_("Raster map <%s> not found in current mapset"), infile);
  128. Rast_get_cellhd(raster->answer, "", &cellhd);
  129. is_reclass = (Rast_is_reclass(raster->answer, "", rname, rmapset) > 0);
  130. if (title_opt->answer) {
  131. strncpy(title, title_opt->answer, MAX_TITLE_LEN);
  132. title[MAX_TITLE_LEN - 1] = '\0'; /* strncpy doesn't null terminate oversized input */
  133. G_strip(title);
  134. G_debug(3, "map title= [%s] (%d chars)", title, strlen(title));
  135. Rast_put_cell_title(raster->answer, title);
  136. }
  137. if (save_opt->answer) {
  138. FILE *fp = fopen(save_opt->answer, "w");
  139. int i;
  140. if (!fp)
  141. G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer);
  142. Rast_read_history(raster->answer, "", &hist);
  143. for (i = 0; i < Rast_history_length(&hist); i++)
  144. fprintf(fp, "%s\n", Rast_history_line(&hist, i));
  145. fclose(fp);
  146. }
  147. if (load_opt->answer) {
  148. FILE *fp = fopen(load_opt->answer, "r");
  149. if (!fp)
  150. G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer);
  151. Rast_read_history(raster->answer, "", &hist);
  152. Rast_clear_history(&hist);
  153. for (;;) {
  154. char buf[80];
  155. if (!G_getl2(buf, sizeof(buf), fp))
  156. break;
  157. Rast_append_history(&hist, buf);
  158. }
  159. fclose(fp);
  160. Rast_write_history(raster->answer, &hist);
  161. }
  162. if (history_opt->answer) {
  163. Rast_read_history(raster->answer, "", &hist);
  164. /* two less than defined as if only one less a newline gets appended in the hist file. bug? */
  165. /* Should be RECORD_LEN, but r.info truncates at > 71 chars */
  166. if (strlen(history_opt->answer) > 71) {
  167. int i;
  168. for (i = 0; i < strlen(history_opt->answer); i += 71) {
  169. char buf[72];
  170. strncpy(buf, &history_opt->answer[i], sizeof(buf)-1);
  171. buf[sizeof(buf)-1] = '\0';
  172. Rast_append_history(&hist, buf);
  173. }
  174. }
  175. else
  176. Rast_append_history(&hist, history_opt->answer);
  177. Rast_write_history(raster->answer, &hist);
  178. }
  179. if (units_opt->answer)
  180. Rast_write_units(raster->answer, units_opt->answer);
  181. if (vdatum_opt->answer)
  182. Rast_write_vdatum(raster->answer, vdatum_opt->answer);
  183. if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
  184. Rast_read_history(raster->answer, "", &hist);
  185. if (datasrc1_opt->answer)
  186. Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer);
  187. if (datasrc2_opt->answer)
  188. Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer);
  189. if (datadesc_opt->answer)
  190. Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer);
  191. Rast_write_history(raster->answer, &hist);
  192. }
  193. if (map_opt->answer) { /* use cats from another map */
  194. int fd;
  195. struct Categories cats;
  196. fd = Rast_open_old(infile, "");
  197. Rast_init_cats("", &cats);
  198. if (Rast_read_cats(map_opt->answer, "", &cats) < 0)
  199. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  200. map_opt->answer);
  201. Rast_write_cats(infile, &cats);
  202. G_message(_("cats table for [%s] set to %s"),
  203. infile, map_opt->answer);
  204. Rast_close(fd);
  205. Rast_free_cats(&cats);
  206. }
  207. if (title_opt->answer || history_opt->answer || units_opt->answer
  208. || vdatum_opt->answer || datasrc1_opt->answer || datasrc2_opt->answer
  209. || datadesc_opt->answer || map_opt->answer)
  210. exit(EXIT_SUCCESS);
  211. /* Check the histogram and range */
  212. if (stats_flag->answer)
  213. check_stats(raster->answer);
  214. /* null file */
  215. if (null_flag->answer) {
  216. unsigned char *null_bits;
  217. int row, col;
  218. int null_fd;
  219. if (is_reclass)
  220. G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
  221. raster->answer);
  222. /* Create a file of no-nulls */
  223. null_bits = Rast__allocate_null_bits(cellhd.cols);
  224. for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++)
  225. null_bits[col] = 0;
  226. /* Open null file for writing */
  227. null_fd = G_open_new_misc("cell_misc", "null", raster->answer);
  228. G_message(_("Writing new null file for [%s]... "), raster->answer);
  229. for (row = 0; row < cellhd.rows; row++) {
  230. G_percent(row, cellhd.rows, 1);
  231. Rast__write_null_bits(null_fd, null_bits, row, cellhd.cols, 0);
  232. }
  233. G_percent(row, cellhd.rows, 1);
  234. /* Cleanup */
  235. close(null_fd);
  236. G_free(null_bits);
  237. }
  238. if (del_flag->answer) {
  239. int null_fd;
  240. char path[GPATH_MAX];
  241. if (is_reclass)
  242. G_fatal_error(_("[%s] is a reclass of another map. Exiting."),
  243. raster->answer);
  244. /* Write a file of no-nulls */
  245. G_message(_("Removing null file for [%s]...\n"), raster->answer);
  246. null_fd = G_open_new_misc("cell_misc", "null", raster->answer);
  247. G_file_name_misc(path, "cell_misc", "null", raster->answer, G_mapset());
  248. unlink(path);
  249. close(null_fd);
  250. G_done_msg(_("Done."));
  251. }
  252. return EXIT_SUCCESS;
  253. }