main.c 11 KB

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