main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /****************************************************************************
  2. *
  3. * MODULE: r.null
  4. * AUTHOR(S): U.S.Army Construction Engineering Research Laboratory
  5. * PURPOSE: Manages NULL-values of given raster map.
  6. * COPYRIGHT: (C) 2008 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. *****************************************************************************/
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <grass/gis.h>
  17. #include <grass/raster.h>
  18. #include <grass/glocale.h>
  19. #include "mask.h"
  20. #include "local_proto.h"
  21. d_Mask d_mask;
  22. DCELL new_null;
  23. static struct Cell_head cellhd;
  24. static int parse_vallist(char **, d_Mask *);
  25. int main(int argc, char *argv[])
  26. {
  27. const char *name, *mapset;
  28. char rname[GNAME_MAX], rmapset[GMAPSET_MAX];
  29. char path[GPATH_MAX];
  30. int row, col, null_fd;
  31. unsigned char *null_bits;
  32. RASTER_MAP_TYPE map_type;
  33. int change_null = 0, create, remove, only_int, only_fp, only_null;
  34. int is_reclass;
  35. struct GModule *module;
  36. struct
  37. {
  38. struct Option *map;
  39. struct Option *setnull;
  40. struct Option *null;
  41. } parms;
  42. struct
  43. {
  44. struct Flag *f;
  45. struct Flag *n;
  46. struct Flag *i;
  47. struct Flag *c;
  48. struct Flag *r;
  49. } flags;
  50. G_gisinit(argv[0]);
  51. module = G_define_module();
  52. G_add_keyword(_("raster"));
  53. G_add_keyword(_("null data"));
  54. module->description = _("Manages NULL-values of given raster map.");
  55. parms.map = G_define_standard_option(G_OPT_R_MAP);
  56. parms.map->description = _("Raster map for which to edit null file");
  57. parms.setnull = G_define_option();
  58. parms.setnull->key = "setnull";
  59. parms.setnull->key_desc = "val[-val]";
  60. parms.setnull->type = TYPE_STRING;
  61. parms.setnull->required = NO;
  62. parms.setnull->multiple = YES;
  63. parms.setnull->description = _("List of cell values to be set to NULL");
  64. parms.null = G_define_option();
  65. parms.null->key = "null";
  66. parms.null->type = TYPE_DOUBLE;
  67. parms.null->required = NO;
  68. parms.null->multiple = NO;
  69. parms.null->description = _("The value to replace the null value by");
  70. flags.f = G_define_flag();
  71. flags.f->key = 'f';
  72. flags.f->description = _("Only do the work if the map is floating-point");
  73. flags.i = G_define_flag();
  74. flags.i->key = 'i';
  75. flags.i->description = _("Only do the work if the map is integer");
  76. flags.n = G_define_flag();
  77. flags.n->key = 'n';
  78. flags.n->description =
  79. _("Only do the work if the map doesn't have a NULL-value bitmap file");
  80. flags.c = G_define_flag();
  81. flags.c->key = 'c';
  82. flags.c->description =
  83. _("Create NULL-value bitmap file validating all data cells");
  84. flags.r = G_define_flag();
  85. flags.r->key = 'r';
  86. flags.r->description = _("Remove NULL-value bitmap file");
  87. if (G_parser(argc, argv))
  88. exit(EXIT_FAILURE);
  89. only_int = flags.i->answer;
  90. only_fp = flags.f->answer;
  91. only_null = flags.n->answer;
  92. create = flags.c->answer;
  93. remove = flags.r->answer;
  94. name = parms.map->answer;
  95. mapset = G_find_raster2(name, "");
  96. if (mapset == NULL)
  97. G_fatal_error(_("Raster map <%s> not found"), name);
  98. is_reclass = (Rast_is_reclass(name, mapset, rname, rmapset) > 0);
  99. if (is_reclass)
  100. G_fatal_error(_("Raster map <%s> is a reclass of map <%s@%s>. "
  101. "Consider to generate a copy with r.mapcalc. Exiting."),
  102. name, rname, rmapset);
  103. if (strcmp(mapset, G_mapset()) != 0)
  104. G_fatal_error(_("Raster map <%s> is not in your mapset <%s>"),
  105. name, G_mapset());
  106. if (parms.null->answer) {
  107. if (sscanf(parms.null->answer, "%lf", &new_null) == 1)
  108. change_null = 1;
  109. else
  110. G_fatal_error(_("%s is illegal entry for null"),
  111. parms.null->answer);
  112. }
  113. map_type = Rast_map_type(name, mapset);
  114. if (only_null && G_find_file2_misc("cell_misc", "null", name, mapset))
  115. G_fatal_error(_("Raster map <%s> already has a null bitmap file"), name);
  116. if (map_type == CELL_TYPE) {
  117. if (only_fp)
  118. G_fatal_error(_("<%s> is integer raster map (CELL)"),
  119. name);
  120. if ((double)((int)new_null) != new_null) {
  121. G_warning(_("<%s> is integer raster map (CELL). Using null=%d."),
  122. name, (int)new_null);
  123. new_null = (double)((int)new_null);
  124. }
  125. }
  126. else if (only_int)
  127. G_fatal_error(_("<%s> is floating pointing raster map"),
  128. name);
  129. parse_vallist(parms.setnull->answers, &d_mask);
  130. Rast_get_cellhd(name, mapset, &cellhd);
  131. if (create) {
  132. /* write a file of no-nulls */
  133. null_bits = (unsigned char *)Rast__allocate_null_bits(cellhd.cols);
  134. /* init all cells to 0's */
  135. for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++)
  136. null_bits[col] = 0;
  137. null_fd = G_open_new_misc("cell_misc", "null", name);
  138. G_verbose_message(_("Writing new null file for raster map <%s>..."),
  139. name);
  140. for (row = 0; row < cellhd.rows; row++) {
  141. G_percent(row, cellhd.rows, 1);
  142. Rast__write_null_bits(null_fd, null_bits, row, cellhd.cols, 0);
  143. }
  144. G_percent(row, cellhd.rows, 1);
  145. close(null_fd);
  146. G_done_msg(_("Raster map <%s> modified."), name);
  147. exit(EXIT_SUCCESS);
  148. }
  149. if (remove) {
  150. /* write a file of no-nulls */
  151. G_verbose_message(_("Removing null file for raster map <%s>..."),
  152. name);
  153. null_fd = G_open_new_misc("cell_misc", "null", name);
  154. G__file_name_misc(path, "cell_misc", "null", name, mapset);
  155. unlink(path);
  156. G_done_msg(_("Raster map <%s> modified."), name);
  157. exit(EXIT_SUCCESS);
  158. }
  159. process(name, mapset, change_null, map_type);
  160. exit(EXIT_SUCCESS);
  161. }
  162. static int parse_vallist(char **vallist, d_Mask * d_mask)
  163. {
  164. char buf[1024];
  165. char x[2];
  166. FILE *fd;
  167. init_d_mask_rules(d_mask);
  168. if (vallist == NULL)
  169. return 1;
  170. for (; *vallist; vallist++) {
  171. if (*vallist[0] == '/') {
  172. fd = fopen(*vallist, "r");
  173. if (fd == NULL) {
  174. perror(*vallist);
  175. G_usage();
  176. exit(1);
  177. }
  178. while (fgets(buf, sizeof buf, fd)) {
  179. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  180. continue;
  181. parse_d_mask_rule(buf, d_mask, *vallist);
  182. }
  183. fclose(fd);
  184. }
  185. else
  186. parse_d_mask_rule(*vallist, d_mask, (char *)NULL);
  187. }
  188. return 0;
  189. }
  190. int parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  191. {
  192. double a, b;
  193. char junk[128];
  194. /* #-# */
  195. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2)
  196. add_d_mask_rule(d_mask, a, b, 0);
  197. /* inf-# */
  198. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  199. add_d_mask_rule(d_mask, a, a, -1);
  200. /* #-inf */
  201. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  202. add_d_mask_rule(d_mask, a, a, 1);
  203. /* # */
  204. else if (sscanf(vallist, "%lf", &a) == 1)
  205. add_d_mask_rule(d_mask, a, a, 0);
  206. else {
  207. if (where)
  208. G_fatal_error(_("%s: %s: illegal value spec"), where, vallist);
  209. else
  210. G_fatal_error(_("%s: illegal value spec"), vallist);
  211. }
  212. return 0;
  213. }
  214. int process(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  215. {
  216. struct Colors colr;
  217. struct History hist;
  218. struct Categories cats;
  219. struct Quant quant;
  220. int colr_ok;
  221. int hist_ok;
  222. int cats_ok;
  223. int quant_ok;
  224. G_suppress_warnings(1);
  225. colr_ok = Rast_read_colors(name, mapset, &colr) > 0;
  226. hist_ok = Rast_read_history(name, mapset, &hist) >= 0;
  227. cats_ok = Rast_read_cats(name, mapset, &cats) >= 0;
  228. if (map_type != CELL_TYPE) {
  229. Rast_quant_init(&quant);
  230. quant_ok = Rast_read_quant(name, mapset, &quant);
  231. G_suppress_warnings(0);
  232. }
  233. if (doit(name, mapset, change_null, map_type))
  234. return 1;
  235. if (colr_ok) {
  236. Rast_write_colors(name, mapset, &colr);
  237. Rast_free_colors(&colr);
  238. }
  239. if (hist_ok)
  240. Rast_write_history(name, &hist);
  241. if (cats_ok) {
  242. cats.num = Rast_get_max_c_cat(name, mapset);
  243. Rast_write_cats(name, &cats);
  244. Rast_free_cats(&cats);
  245. }
  246. if (map_type != CELL_TYPE && quant_ok)
  247. Rast_write_quant(name, mapset, &quant);
  248. return 0;
  249. }
  250. int doit(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  251. {
  252. int new, old, row;
  253. void *rast;
  254. Rast_set_window(&cellhd);
  255. old = Rast_open_old(name, mapset);
  256. new = Rast_open_new(name, map_type);
  257. rast = Rast_allocate_buf(map_type);
  258. G_verbose_message(_("Writing new data for raster map <%s>..."), name);
  259. /* the null file is written automatically */
  260. for (row = 0; row < cellhd.rows; row++) {
  261. G_percent(row, cellhd.rows, 1);
  262. Rast_get_row_nomask(old, rast, row, map_type);
  263. mask_raster_array(rast, cellhd.cols, change_null, map_type);
  264. Rast_put_row(new, rast, map_type);
  265. }
  266. G_percent(row, cellhd.rows, 1);
  267. G_free(rast);
  268. Rast_close(old);
  269. if (row < cellhd.rows) {
  270. Rast_unopen(new);
  271. return 1;
  272. }
  273. Rast_close(new);
  274. return 0;
  275. }