main.c 11 KB

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