main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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, 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, recreate;
  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. struct Flag *z;
  50. } flags;
  51. G_gisinit(argv[0]);
  52. module = G_define_module();
  53. G_add_keyword(_("raster"));
  54. G_add_keyword(_("map management"));
  55. G_add_keyword(_("null data"));
  56. G_add_keyword(_("no-data"));
  57. module->description = _("Manages NULL-values of given raster map.");
  58. parms.map = G_define_standard_option(G_OPT_R_MAP);
  59. parms.map->description = _("Name of raster map for which to edit null values");
  60. parms.setnull = G_define_option();
  61. parms.setnull->key = "setnull";
  62. parms.setnull->key_desc = "val[-val]";
  63. parms.setnull->type = TYPE_STRING;
  64. parms.setnull->required = NO;
  65. parms.setnull->multiple = YES;
  66. parms.setnull->description = _("List of cell values to be set to NULL");
  67. parms.setnull->guisection = _("Modify");
  68. parms.null = G_define_option();
  69. parms.null->key = "null";
  70. parms.null->type = TYPE_DOUBLE;
  71. parms.null->required = NO;
  72. parms.null->multiple = NO;
  73. parms.null->description = _("The value to replace the null value by");
  74. parms.null->guisection = _("Modify");
  75. flags.f = G_define_flag();
  76. flags.f->key = 'f';
  77. flags.f->description = _("Only do the work if the map is floating-point");
  78. flags.f->guisection = _("Check");
  79. flags.i = G_define_flag();
  80. flags.i->key = 'i';
  81. flags.i->description = _("Only do the work if the map is integer");
  82. flags.i->guisection = _("Check");
  83. flags.n = G_define_flag();
  84. flags.n->key = 'n';
  85. flags.n->description =
  86. _("Only do the work if the map doesn't have a NULL-value bitmap file");
  87. flags.n->guisection = _("Check");
  88. flags.c = G_define_flag();
  89. flags.c->key = 'c';
  90. flags.c->description =
  91. _("Create NULL-value bitmap file validating all data cells");
  92. flags.r = G_define_flag();
  93. flags.r->key = 'r';
  94. flags.r->description = _("Remove NULL-value bitmap file");
  95. flags.r->guisection = _("Remove");
  96. flags.z = G_define_flag();
  97. flags.z->key = 'z';
  98. flags.z->description =
  99. _("Re-create NULL-value bitmap file (to compress or uncompress)");
  100. if (G_parser(argc, argv))
  101. exit(EXIT_FAILURE);
  102. only_int = flags.i->answer;
  103. only_fp = flags.f->answer;
  104. only_null = flags.n->answer;
  105. create = flags.c->answer;
  106. remove = flags.r->answer;
  107. recreate = flags.z->answer;
  108. name = parms.map->answer;
  109. mapset = G_find_raster2(name, "");
  110. if (mapset == NULL)
  111. G_fatal_error(_("Raster map <%s> not found"), name);
  112. is_reclass = (Rast_is_reclass(name, mapset, rname, rmapset) > 0);
  113. if (is_reclass)
  114. G_fatal_error(_("Raster map <%s> is a reclass of map <%s@%s>. "
  115. "Consider to generate a copy with r.mapcalc. Exiting."),
  116. name, rname, rmapset);
  117. if (G_find_file2_misc("cell_misc", "vrt", name, "")) {
  118. G_fatal_error(_("<%s> is a virtual raster map. "
  119. "Consider to generate a copy with r.mapcalc. Exiting."),
  120. name);
  121. }
  122. if (strcmp(mapset, G_mapset()) != 0)
  123. G_fatal_error(_("Raster map <%s> is not in your mapset <%s>"),
  124. name, G_mapset());
  125. if (parms.null->answer) {
  126. if (sscanf(parms.null->answer, "%lf", &new_null) == 1)
  127. change_null = 1;
  128. else
  129. G_fatal_error(_("%s is illegal entry for null"),
  130. parms.null->answer);
  131. }
  132. map_type = Rast_map_type(name, mapset);
  133. if (only_null && G_find_file2_misc("cell_misc", "null", name, mapset))
  134. G_fatal_error(_("Raster map <%s> already has a null bitmap file"), name);
  135. if (map_type == CELL_TYPE) {
  136. if (only_fp)
  137. G_fatal_error(_("<%s> is integer raster map (CELL)"),
  138. name);
  139. if ((double)((int)new_null) != new_null) {
  140. G_warning(_("<%s> is integer raster map (CELL). Using null=%d."),
  141. name, (int)new_null);
  142. new_null = (double)((int)new_null);
  143. }
  144. }
  145. else if (only_int)
  146. G_fatal_error(_("<%s> is floating pointing raster map"),
  147. name);
  148. parse_vallist(parms.setnull->answers, &d_mask);
  149. Rast_get_cellhd(name, mapset, &cellhd);
  150. if (create) {
  151. /* write a file of no-nulls */
  152. null_bits = Rast__allocate_null_bits(cellhd.cols);
  153. /* init all cells to 0's */
  154. for (col = 0; col < Rast__null_bitstream_size(cellhd.cols); col++)
  155. null_bits[col] = 0;
  156. Rast_set_window(&cellhd);
  157. fd = Rast__open_null_write(name);
  158. G_verbose_message(_("Writing new null file for raster map <%s>..."),
  159. name);
  160. for (row = 0; row < cellhd.rows; row++) {
  161. G_percent(row, cellhd.rows, 1);
  162. Rast__write_null_bits(fd, null_bits);
  163. }
  164. G_percent(row, cellhd.rows, 1);
  165. Rast__close_null(fd);
  166. G_done_msg(_("Raster map <%s> modified."), name);
  167. exit(EXIT_SUCCESS);
  168. }
  169. if (recreate) {
  170. /* (un-)compress NULL file */
  171. int in_fd;
  172. char *nullcompr = getenv("GRASS_COMPRESS_NULLS");
  173. int donullcompr = (nullcompr && atoi(nullcompr)) ? 1 : 0;
  174. G_debug(1, "NULL compression is currently %s", donullcompr ? "enabled" : "disabled");
  175. if (donullcompr) {
  176. if (G_find_file2_misc("cell_misc", "nullcmpr", name, mapset)) {
  177. G_message(_("The NULL file is already compressed, nothing to do."));
  178. exit(EXIT_SUCCESS);
  179. }
  180. }
  181. else {
  182. if (G_find_file2_misc("cell_misc", "null", name, mapset)) {
  183. G_message(_("The NULL file is already uncompressed, nothing to do."));
  184. exit(EXIT_SUCCESS);
  185. }
  186. }
  187. null_bits = Rast__allocate_null_bits(cellhd.cols);
  188. Rast__init_null_bits(null_bits, cellhd.cols);
  189. Rast_set_window(&cellhd);
  190. in_fd = Rast_open_old(name, mapset);
  191. fd = Rast__open_null_write(name);
  192. G_verbose_message(_("Writing new null file for raster map <%s>..."),
  193. name);
  194. for (row = 0; row < cellhd.rows; row++) {
  195. G_percent(row, cellhd.rows, 1);
  196. Rast__read_null_bits(in_fd, row, null_bits);
  197. Rast__write_null_bits(fd, null_bits);
  198. }
  199. G_percent(row, cellhd.rows, 1);
  200. Rast__close_null(fd);
  201. Rast_close(in_fd);
  202. G_done_msg(_("Raster map <%s> modified."), name);
  203. exit(EXIT_SUCCESS);
  204. }
  205. if (remove) {
  206. /* remove NULL file */
  207. G_verbose_message(_("Removing null file for raster map <%s>..."),
  208. name);
  209. G_file_name_misc(path, "cell_misc", "null", name, mapset);
  210. unlink(path);
  211. G_file_name_misc(path, "cell_misc", "nullcmpr", name, mapset);
  212. unlink(path);
  213. G_done_msg(_("Raster map <%s> modified."), name);
  214. exit(EXIT_SUCCESS);
  215. }
  216. process(name, mapset, change_null, map_type);
  217. exit(EXIT_SUCCESS);
  218. }
  219. static int parse_vallist(char **vallist, d_Mask * d_mask)
  220. {
  221. char buf[1024];
  222. char x[2];
  223. FILE *fd;
  224. init_d_mask_rules(d_mask);
  225. if (vallist == NULL)
  226. return 1;
  227. for (; *vallist; vallist++) {
  228. if (*vallist[0] == '/') {
  229. fd = fopen(*vallist, "r");
  230. if (fd == NULL) {
  231. perror(*vallist);
  232. G_usage();
  233. exit(1);
  234. }
  235. while (fgets(buf, sizeof buf, fd)) {
  236. if (sscanf(buf, "%1s", x) != 1 || *x == '#')
  237. continue;
  238. parse_d_mask_rule(buf, d_mask, *vallist);
  239. }
  240. fclose(fd);
  241. }
  242. else
  243. parse_d_mask_rule(*vallist, d_mask, (char *)NULL);
  244. }
  245. return 0;
  246. }
  247. int parse_d_mask_rule(char *vallist, d_Mask * d_mask, char *where)
  248. {
  249. double a, b;
  250. char junk[128];
  251. /* #-# */
  252. if (sscanf(vallist, "%lf-%lf", &a, &b) == 2)
  253. add_d_mask_rule(d_mask, a, b, 0);
  254. /* inf-# */
  255. else if (sscanf(vallist, "%[^ -\t]-%lf", junk, &a) == 2)
  256. add_d_mask_rule(d_mask, a, a, -1);
  257. /* #-inf */
  258. else if (sscanf(vallist, "%lf-%[^ \t]", &a, junk) == 2)
  259. add_d_mask_rule(d_mask, a, a, 1);
  260. /* # */
  261. else if (sscanf(vallist, "%lf", &a) == 1)
  262. add_d_mask_rule(d_mask, a, a, 0);
  263. else {
  264. if (where)
  265. G_fatal_error(_("%s: %s: illegal value spec"), where, vallist);
  266. else
  267. G_fatal_error(_("%s: illegal value spec"), vallist);
  268. }
  269. return 0;
  270. }
  271. int process(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  272. {
  273. struct Colors colr;
  274. struct History hist;
  275. struct Categories cats;
  276. struct Quant quant;
  277. int colr_ok;
  278. int hist_ok;
  279. int cats_ok;
  280. int quant_ok;
  281. G_suppress_warnings(1);
  282. colr_ok = Rast_read_colors(name, mapset, &colr) > 0;
  283. hist_ok = Rast_read_history(name, mapset, &hist) >= 0;
  284. cats_ok = Rast_read_cats(name, mapset, &cats) >= 0;
  285. if (map_type != CELL_TYPE) {
  286. Rast_quant_init(&quant);
  287. quant_ok = Rast_read_quant(name, mapset, &quant);
  288. G_suppress_warnings(0);
  289. }
  290. if (doit(name, mapset, change_null, map_type))
  291. return 1;
  292. if (colr_ok) {
  293. Rast_write_colors(name, mapset, &colr);
  294. Rast_free_colors(&colr);
  295. }
  296. if (hist_ok)
  297. Rast_write_history(name, &hist);
  298. if (cats_ok) {
  299. cats.num = Rast_get_max_c_cat(name, mapset);
  300. Rast_write_cats(name, &cats);
  301. Rast_free_cats(&cats);
  302. }
  303. if (map_type != CELL_TYPE && quant_ok)
  304. Rast_write_quant(name, mapset, &quant);
  305. return 0;
  306. }
  307. int doit(const char *name, const char *mapset, int change_null, RASTER_MAP_TYPE map_type)
  308. {
  309. int new, old, row;
  310. void *rast;
  311. Rast_set_window(&cellhd);
  312. old = Rast_open_old(name, mapset);
  313. new = Rast_open_new(name, map_type);
  314. rast = Rast_allocate_buf(map_type);
  315. G_verbose_message(_("Writing new data for raster map <%s>..."), name);
  316. /* the null file is written automatically */
  317. for (row = 0; row < cellhd.rows; row++) {
  318. G_percent(row, cellhd.rows, 1);
  319. Rast_get_row_nomask(old, rast, row, map_type);
  320. mask_raster_array(rast, cellhd.cols, change_null, map_type);
  321. Rast_put_row(new, rast, map_type);
  322. }
  323. G_percent(row, cellhd.rows, 1);
  324. G_free(rast);
  325. Rast_close(old);
  326. if (row < cellhd.rows) {
  327. Rast_unopen(new);
  328. return 1;
  329. }
  330. Rast_close(new);
  331. return 0;
  332. }