main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. **********************************************************************
  3. *
  4. * MODULE: r3.support (GRASS core)
  5. *
  6. * AUTHOR(S): Soeren Gebbert vTI/AK based on r.support
  7. * Original r.support by Michael Shapiro - CERL
  8. *
  9. * PURPOSE: Build support files for raster map
  10. * - Edit header, units
  11. * - Update status (range)
  12. *
  13. * COPYRIGHT: (C) 2000-2007 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General
  16. * Public License (>=v2). Read the file COPYING that comes
  17. * with GRASS for details.
  18. *
  19. **********************************************************************/
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <grass/gis.h>
  24. #include <grass/raster.h>
  25. #include <grass/raster3d.h>
  26. #include <grass/glocale.h>
  27. #include "local_proto.h"
  28. #define MAX_TITLE_LEN 1022
  29. int main(int argc, char *argv[])
  30. {
  31. const char *mapset;
  32. struct GModule *module;
  33. struct Option *raster, *title_opt, *history_opt;
  34. struct Option *datasrc1_opt, *datasrc2_opt, *datadesc_opt;
  35. struct Option *map_opt, *units_opt, *vunits_opt;
  36. struct Option *load_opt, *save_opt;
  37. struct Flag *stats_flag;
  38. const char *infile;
  39. char title[MAX_TITLE_LEN + 1];
  40. struct History hist;
  41. /* Initialize GIS engine */
  42. G_gisinit(argv[0]);
  43. module = G_define_module();
  44. G_add_keyword(_("raster3d"));
  45. G_add_keyword(_("metadata"));
  46. G_add_keyword(_("voxel"));
  47. module->description = _("Allows creation and/or modification of "
  48. "3D raster map layer support files.");
  49. raster = G_define_standard_option(G_OPT_R3_MAP);
  50. title_opt = G_define_option();
  51. title_opt->key = "title";
  52. title_opt->key_desc = "phrase";
  53. title_opt->type = TYPE_STRING;
  54. title_opt->required = NO;
  55. title_opt->description = _("Text to use for map title");
  56. history_opt = G_define_option();
  57. history_opt->key = "history";
  58. history_opt->key_desc = "phrase";
  59. history_opt->type = TYPE_STRING;
  60. history_opt->required = NO;
  61. history_opt->description =
  62. _("Text to append to the next line of the map's metadata file");
  63. units_opt = G_define_option();
  64. units_opt->key = "unit";
  65. units_opt->type = TYPE_STRING;
  66. units_opt->required = NO;
  67. units_opt->description = _("The map data unit");
  68. vunits_opt = G_define_option();
  69. vunits_opt->key = "vunit";
  70. vunits_opt->type = TYPE_STRING;
  71. vunits_opt->required = NO;
  72. vunits_opt->description = _("The vertical unit of the map");
  73. datasrc1_opt = G_define_option();
  74. datasrc1_opt->key = "source1";
  75. datasrc1_opt->key_desc = "phrase";
  76. datasrc1_opt->type = TYPE_STRING;
  77. datasrc1_opt->required = NO;
  78. datasrc1_opt->description = _("Text to use for data source, line 1");
  79. datasrc2_opt = G_define_option();
  80. datasrc2_opt->key = "source2";
  81. datasrc2_opt->key_desc = "phrase";
  82. datasrc2_opt->type = TYPE_STRING;
  83. datasrc2_opt->required = NO;
  84. datasrc2_opt->description = _("Text to use for data source, line 2");
  85. datadesc_opt = G_define_option();
  86. datadesc_opt->key = "description";
  87. datadesc_opt->key_desc = "phrase";
  88. datadesc_opt->type = TYPE_STRING;
  89. datadesc_opt->required = NO;
  90. datadesc_opt->description =
  91. _("Text to use for data description or keyword(s)");
  92. map_opt = G_define_option();
  93. map_opt->key = "raster";
  94. map_opt->type = TYPE_STRING;
  95. map_opt->required = NO;
  96. map_opt->gisprompt = "old,cell,raster";
  97. map_opt->description = _("Raster map from which to copy category table");
  98. load_opt = G_define_standard_option(G_OPT_F_INPUT);
  99. load_opt->key = "loadhistory";
  100. load_opt->required = NO;
  101. load_opt->description = _("Text file from which to load history");
  102. save_opt = G_define_standard_option(G_OPT_F_OUTPUT);
  103. save_opt->key = "savehistory";
  104. save_opt->required = NO;
  105. save_opt->description = _("Text file in which to save history");
  106. stats_flag = G_define_flag();
  107. stats_flag->key = 's';
  108. stats_flag->description = _("Update range");
  109. /* Parse command-line options */
  110. if (G_parser(argc, argv))
  111. exit(EXIT_FAILURE);
  112. /* Make sure raster exists and set mapset */
  113. infile = raster->answer;
  114. mapset = G_find_raster3d(infile, G_mapset()); /* current mapset only for editing */
  115. if (!mapset || strcmp(mapset, G_mapset()) != 0)
  116. G_fatal_error(_("3D raster map <%s> not found"), infile);
  117. if (title_opt->answer) {
  118. strncpy(title, title_opt->answer, MAX_TITLE_LEN);
  119. title[MAX_TITLE_LEN - 1] = '\0'; /* strncpy doesn't null terminate over-sized input */
  120. G_strip(title);
  121. G_debug(3, "map title= [%s] (%d chars)", title, (int)strlen(title));
  122. Rast3d_read_history(raster->answer, "", &hist);
  123. Rast_set_history(&hist, HIST_TITLE, title);
  124. Rast3d_write_history(raster->answer, &hist);
  125. }
  126. if (save_opt->answer) {
  127. FILE *fp = fopen(save_opt->answer, "w");
  128. int i;
  129. if (!fp)
  130. G_fatal_error(_("Unable to open output file <%s>"), save_opt->answer);
  131. Rast3d_read_history(raster->answer, "", &hist);
  132. for (i = 0; i < Rast_history_length(&hist); i++)
  133. fprintf(fp, "%s\n", Rast_history_line(&hist, i));
  134. fclose(fp);
  135. }
  136. if (load_opt->answer) {
  137. FILE *fp = fopen(load_opt->answer, "r");
  138. if (!fp)
  139. G_fatal_error(_("Unable to open input file <%s>"), load_opt->answer);
  140. Rast3d_read_history(raster->answer, "", &hist);
  141. Rast_clear_history(&hist);
  142. for (;;) {
  143. char buf[80];
  144. if (!G_getl2(buf, sizeof(buf), fp))
  145. break;
  146. Rast_append_history(&hist, buf);
  147. }
  148. fclose(fp);
  149. Rast3d_write_history(raster->answer, &hist);
  150. }
  151. if (history_opt->answer) {
  152. Rast3d_read_history(raster->answer, "", &hist);
  153. /* two less than defined as if only one less a newline gets appended in the hist file. bug? */
  154. /* Should be RECORD_LEN, but r.info truncates at > 71 chars */
  155. if (strlen(history_opt->answer) > 71) {
  156. int i;
  157. for (i = 0; i < strlen(history_opt->answer); i += 71) {
  158. char buf[72];
  159. strncpy(buf, &history_opt->answer[i], sizeof(buf)-1);
  160. buf[sizeof(buf)-1] = '\0';
  161. Rast_append_history(&hist, buf);
  162. }
  163. }
  164. else
  165. Rast_append_history(&hist, history_opt->answer);
  166. Rast3d_write_history(raster->answer, &hist);
  167. }
  168. if(units_opt->answer || vunits_opt->answer) {
  169. RASTER3D_Map *map;
  170. map = Rast3d_open_cell_old(raster->answer, G_mapset(), \
  171. RASTER3D_DEFAULT_WINDOW, RASTER3D_TILE_SAME_AS_FILE, RASTER3D_USE_CACHE_DEFAULT);
  172. /* Modify the units */
  173. if (units_opt->answer)
  174. Rast3d_set_unit(map, units_opt->answer);
  175. if (vunits_opt->answer)
  176. Rast3d_set_vertical_unit(map, vunits_opt->answer);
  177. Rast3d_rewrite_header(map);
  178. Rast3d_close(map);
  179. }
  180. if (datasrc1_opt->answer || datasrc2_opt->answer || datadesc_opt->answer) {
  181. Rast3d_read_history(raster->answer, "", &hist);
  182. if (datasrc1_opt->answer)
  183. Rast_set_history(&hist, HIST_DATSRC_1, datasrc1_opt->answer);
  184. if (datasrc2_opt->answer)
  185. Rast_set_history(&hist, HIST_DATSRC_2, datasrc2_opt->answer);
  186. if (datadesc_opt->answer)
  187. Rast_set_history(&hist, HIST_KEYWRD, datadesc_opt->answer);
  188. Rast3d_write_history(raster->answer, &hist);
  189. }
  190. if (map_opt->answer) { /* use cats from another map */
  191. int fd;
  192. struct Categories cats;
  193. fd = Rast_open_old(infile, "");
  194. Rast_init_cats("", &cats);
  195. if (Rast3d_read_cats(map_opt->answer, "", &cats) < 0)
  196. G_fatal_error(_("Unable to read category file of raster map <%s>"),
  197. map_opt->answer);
  198. Rast3d_write_cats(infile, &cats);
  199. G_message(_("cats table for [%s] set to %s"),
  200. infile, map_opt->answer);
  201. Rast_close(fd);
  202. Rast_free_cats(&cats);
  203. }
  204. /* Check the histogram and range */
  205. if (stats_flag->answer)
  206. check_stats(raster->answer);
  207. return EXIT_SUCCESS;
  208. }