main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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, 2011 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General
  9. * Public License (>=v2). Read the file COPYING that
  10. * comes with GRASS 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 = _("Name of raster map for which to edit null values");
  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.setnull->guisection = _("Modify");
  65. parms.null = G_define_option();
  66. parms.null->key = "null";
  67. parms.null->type = TYPE_DOUBLE;
  68. parms.null->required = NO;
  69. parms.null->multiple = NO;
  70. parms.null->description = _("The value to replace the null value by");
  71. parms.null->guisection = _("Modify");
  72. flags.f = G_define_flag();
  73. flags.f->key = 'f';
  74. flags.f->description = _("Only do the work if the map is floating-point");
  75. flags.f->guisection = _("Check");
  76. flags.i = G_define_flag();
  77. flags.i->key = 'i';
  78. flags.i->description = _("Only do the work if the map is integer");
  79. flags.i->guisection = _("Check");
  80. flags.n = G_define_flag();
  81. flags.n->key = 'n';
  82. flags.n->description =
  83. _("Only do the work if the map doesn't have a NULL-value bitmap file");
  84. flags.n->guisection = _("Check");
  85. flags.c = G_define_flag();
  86. flags.c->key = 'c';
  87. flags.c->description =
  88. _("Create NULL-value bitmap file validating all data cells");
  89. flags.r = G_define_flag();
  90. flags.r->key = 'r';
  91. flags.r->description = _("Remove NULL-value bitmap file");
  92. flags.r->guisection = _("Remove");
  93. if (G_parser(argc, argv))
  94. exit(EXIT_FAILURE);
  95. only_int = flags.i->answer;
  96. only_fp = flags.f->answer;
  97. only_null = flags.n->answer;
  98. create = flags.c->answer;
  99. remove = flags.r->answer;
  100. name = parms.map->answer;
  101. mapset = G_find_raster2(name, "");
  102. if (mapset == NULL)
  103. G_fatal_error(_("Raster map <%s> not found"), name);
  104. is_reclass = (Rast_is_reclass(name, mapset, rname, rmapset) > 0);
  105. if (is_reclass)
  106. G_fatal_error(_("Raster map <%s> is a reclass of map <%s@%s>. "
  107. "Consider to generate a copy with r.mapcalc. Exiting."),
  108. name, rname, rmapset);
  109. if (strcmp(mapset, G_mapset()) != 0)
  110. G_fatal_error(_("Raster map <%s> is not in your mapset <%s>"),
  111. name, G_mapset());
  112. if (parms.null->answer) {
  113. if (sscanf(parms.null->answer, "%lf", &new_null) == 1)
  114. change_null = 1;
  115. else
  116. G_fatal_error(_("%s is illegal entry for null"),
  117. parms.null->answer);
  118. }
  119. map_type = Rast_map_type(name, mapset);
  120. if (only_null && G_find_file2_misc("cell_misc", "null", name, mapset))
  121. G_fatal_error(_("Raster map <%s> already has a null bitmap file"), name);
  122. if (map_type == CELL_TYPE) {
  123. if (only_fp)
  124. G_fatal_error(_("<%s> is integer raster map (CELL)"),
  125. name);
  126. if ((double)((int)new_null) != new_null) {
  127. G_warning(_("<%s> is integer raster map (CELL). Using null=%d."),
  128. name, (int)new_null);
  129. new_null = (double)((int)new_null);
  130. }
  131. }
  132. else if (only_int)
  133. G_fatal_error(_("<%s> is floating pointing raster map"),
  134. name);
  135. parse_vallist(parms.setnull->answers, &d_mask);
  136. Rast_get_cellhd(name, mapset, &cellhd);
  137. if (create) {
  138. /* write a file of no-nulls */
  139. null_bits = (unsigned char *)Rast__allocate_null_bits(cellhd.cols);
  140. /* init all cells to 0's */
  141. for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++)
  142. null_bits[col] = 0;
  143. null_fd = G_open_new_misc("cell_misc", "null", name);
  144. G_verbose_message(_("Writing new null file for raster map <%s>..."),
  145. name);
  146. for (row = 0; row < cellhd.rows; row++) {
  147. G_percent(row, cellhd.rows, 1);
  148. Rast__write_null_bits(null_fd, null_bits, row, cellhd.cols, 0);
  149. }
  150. G_percent(row, cellhd.rows, 1);
  151. close(null_fd);
  152. G_done_msg(_("Raster map <%s> modified."), name);
  153. exit(EXIT_SUCCESS);
  154. }
  155. if (remove) {
  156. /* write a file of no-nulls */
  157. G_verbose_message(_("Removing null file for raster map <%s>..."),
  158. name);
  159. null_fd = G_open_new_misc("cell_misc", "null", name);
  160. G_file_name_misc(path, "cell_misc", "null", name, mapset);
  161. unlink(path);
  162. G_done_msg(_("Raster map <%s> modified."), name);
  163. exit(EXIT_SUCCESS);
  164. }
  165. process(name, mapset, change_null, map_type);
  166. exit(EXIT_SUCCESS);
  167. }
  168. static int parse_vallist(char **vallist, d_Mask * d_mask)
  169. {
  170. char buf[1024];
  171. char x[2];
  172. FILE *fd;
  173. init_d_mask_rules(d_mask);
  174. if (vallist == NULL)
  175. return 1;
  176. for (; *vallist; vallist++) {
  177. if (*vallist[0] == '/') {
  178. fd = fopen(*vallist, "r");
  179. if (fd == NULL) {
  180. perror(*vallist);
  181. G_usage();
  182. exit(1);
  183. }
  184. while (fgets(buf, sizeof buf, fd)) {
  185. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  186. continue;
  187. parse_d_mask_rule(buf, d_mask, *vallist);
  188. }
  189. fclose(fd);
  190. }
  191. else
  192. parse_d_mask_rule(*vallist, d_mask, (char *)NULL);
  193. }
  194. return 0;
  195. }
  196. int parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  197. {
  198. double a, b;
  199. char junk[128];
  200. /* #-# */
  201. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2)
  202. add_d_mask_rule(d_mask, a, b, 0);
  203. /* inf-# */
  204. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  205. add_d_mask_rule(d_mask, a, a, -1);
  206. /* #-inf */
  207. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  208. add_d_mask_rule(d_mask, a, a, 1);
  209. /* # */
  210. else if (sscanf(vallist, "%lf", &a) == 1)
  211. add_d_mask_rule(d_mask, a, a, 0);
  212. else {
  213. if (where)
  214. G_fatal_error(_("%s: %s: illegal value spec"), where, vallist);
  215. else
  216. G_fatal_error(_("%s: illegal value spec"), vallist);
  217. }
  218. return 0;
  219. }
  220. int process(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  221. {
  222. struct Colors colr;
  223. struct History hist;
  224. struct Categories cats;
  225. struct Quant quant;
  226. int colr_ok;
  227. int hist_ok;
  228. int cats_ok;
  229. int quant_ok;
  230. G_suppress_warnings(1);
  231. colr_ok = Rast_read_colors(name, mapset, &colr) > 0;
  232. hist_ok = Rast_read_history(name, mapset, &hist) >= 0;
  233. cats_ok = Rast_read_cats(name, mapset, &cats) >= 0;
  234. if (map_type != CELL_TYPE) {
  235. Rast_quant_init(&quant);
  236. quant_ok = Rast_read_quant(name, mapset, &quant);
  237. G_suppress_warnings(0);
  238. }
  239. if (doit(name, mapset, change_null, map_type))
  240. return 1;
  241. if (colr_ok) {
  242. Rast_write_colors(name, mapset, &colr);
  243. Rast_free_colors(&colr);
  244. }
  245. if (hist_ok)
  246. Rast_write_history(name, &hist);
  247. if (cats_ok) {
  248. cats.num = Rast_get_max_c_cat(name, mapset);
  249. Rast_write_cats(name, &cats);
  250. Rast_free_cats(&cats);
  251. }
  252. if (map_type != CELL_TYPE && quant_ok)
  253. Rast_write_quant(name, mapset, &quant);
  254. return 0;
  255. }
  256. int doit(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  257. {
  258. int new, old, row;
  259. void *rast;
  260. Rast_set_window(&cellhd);
  261. old = Rast_open_old(name, mapset);
  262. new = Rast_open_new(name, map_type);
  263. rast = Rast_allocate_buf(map_type);
  264. G_verbose_message(_("Writing new data for raster map <%s>..."), name);
  265. /* the null file is written automatically */
  266. for (row = 0; row < cellhd.rows; row++) {
  267. G_percent(row, cellhd.rows, 1);
  268. Rast_get_row_nomask(old, rast, row, map_type);
  269. mask_raster_array(rast, cellhd.cols, change_null, map_type);
  270. Rast_put_row(new, rast, map_type);
  271. }
  272. G_percent(row, cellhd.rows, 1);
  273. G_free(rast);
  274. Rast_close(old);
  275. if (row < cellhd.rows) {
  276. Rast_unopen(new);
  277. return 1;
  278. }
  279. Rast_close(new);
  280. return 0;
  281. }