main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /***************************************************************************
  2. * MODULE: r3.null
  3. *
  4. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova,
  5. * Bill Brown, Lubos Mitas, Jaro Hofierka
  6. *
  7. * PURPOSE: Explicitly create the 3D NULL-value bitmap file.
  8. *
  9. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  10. *
  11. * This program is free software under the GNU General Public
  12. * License (>=v2). Read the file COPYING that comes with GRASS
  13. * for details.
  14. *
  15. *****************************************************************************/
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <grass/gis.h>
  20. #include <grass/raster3d.h>
  21. #include <grass/glocale.h>
  22. #undef MAX
  23. #define MAX(a,b) (a > b ? a : b)
  24. typedef struct
  25. {
  26. struct Option *map, *setNull, *null;
  27. } paramType;
  28. static paramType params;
  29. /* function prototypes */
  30. static void setParams(void);
  31. static void getParams(char **name, d_Mask ** maskRules, int *changeNull,
  32. double *newNullVal);
  33. static void modifyNull(char *name, d_Mask * maskRules, int changeNull,
  34. double newNullVal);
  35. static void setParams(void)
  36. {
  37. params.map = G_define_option();
  38. params.map->key = "map";
  39. params.map->type = TYPE_STRING;
  40. params.map->required = YES;
  41. params.map->multiple = NO;
  42. params.map->gisprompt = "old,grid3,3d-raster";
  43. params.map->description =
  44. _("3D raster map for which to modify null values");
  45. params.setNull = G_define_option();
  46. params.setNull->key = "setnull";
  47. params.setNull->key_desc = "val[-val]";
  48. params.setNull->type = TYPE_STRING;
  49. params.setNull->required = NO;
  50. params.setNull->multiple = YES;
  51. params.setNull->description = _("List of cell values to be set to NULL");
  52. params.null = G_define_option();
  53. params.null->key = "null";
  54. params.null->type = TYPE_DOUBLE;
  55. params.null->required = NO;
  56. params.null->multiple = NO;
  57. params.null->description = _("The value to replace the null value by");
  58. }
  59. /*--------------------------------------------------------------------------*/
  60. static void
  61. getParams(char **name, d_Mask ** maskRules, int *changeNull,
  62. double *newNullVal)
  63. {
  64. *name = params.map->answer;
  65. Rast3d_parse_vallist(params.setNull->answers, maskRules);
  66. *changeNull = (params.null->answer != NULL);
  67. if (*changeNull)
  68. if (sscanf(params.null->answer, "%lf", newNullVal) != 1)
  69. Rast3d_fatal_error(_("Illegal value for null"));
  70. }
  71. /*-------------------------------------------------------------------------*/
  72. static void
  73. modifyNull(char *name, d_Mask * maskRules, int changeNull, double newNullVal)
  74. {
  75. void *map, *mapOut;
  76. RASTER3D_Region region;
  77. int tileX, tileY, tileZ, x, y, z;
  78. double value;
  79. int doCompress, doLzw, doRle, precision;
  80. int cacheSize;
  81. cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);
  82. if (NULL == G_find_raster3d(name, ""))
  83. Rast3d_fatal_error(_("3D raster map <%s> not found"), name);
  84. G_debug(1, "Changing NULLs of map %s in %s", name, G_mapset());
  85. map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
  86. DCELL_TYPE, cacheSize);
  87. if (map == NULL)
  88. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  89. Rast3d_get_region_struct_map(map, &region);
  90. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  91. Rast3d_get_compression_mode(&doCompress, &precision);
  92. mapOut = Rast3d_open_new_param(name, DCELL_TYPE, RASTER3D_USE_CACHE_XY,
  93. &region, Rast3d_file_type_map(map),
  94. doCompress, Rast3d_tile_precision_map(map), tileX,
  95. tileY, tileZ);
  96. if (mapOut == NULL)
  97. Rast3d_fatal_error(_("modifyNull: error opening tmp file"));
  98. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  99. Rast3d_autolock_on(map);
  100. Rast3d_unlock_all(map);
  101. Rast3d_min_unlocked(mapOut, RASTER3D_USE_CACHE_X);
  102. Rast3d_autolock_on(mapOut);
  103. Rast3d_unlock_all(mapOut);
  104. for (z = 0; z < region.depths; z++) {
  105. if ((z % tileZ) == 0) {
  106. Rast3d_unlock_all(map);
  107. Rast3d_unlock_all(mapOut);
  108. }
  109. for (y = 0; y < region.rows; y++)
  110. for (x = 0; x < region.cols; x++) {
  111. value = Rast3d_get_double_region(map, x, y, z);
  112. if (Rast3d_is_null_value_num(&value, DCELL_TYPE)) {
  113. if (changeNull) {
  114. value = newNullVal;
  115. }
  116. }
  117. else if (Rast3d_mask_d_select((DCELL *) & value, maskRules)) {
  118. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  119. }
  120. Rast3d_put_double(mapOut, x, y, z, value);
  121. }
  122. }
  123. if (!Rast3d_flush_all_tiles(mapOut))
  124. Rast3d_fatal_error(_("modifyNull: error flushing all tiles"));
  125. Rast3d_autolock_off(map);
  126. Rast3d_unlock_all(map);
  127. Rast3d_autolock_off(mapOut);
  128. Rast3d_unlock_all(mapOut);
  129. if (!Rast3d_close(map))
  130. Rast3d_fatal_error(_("Unable to close 3D raster map <%s>"), name);
  131. if (!Rast3d_close(mapOut))
  132. Rast3d_fatal_error(_("modifyNull: Unable to close tmp file"));
  133. }
  134. /*--------------------------------------------------------------------------*/
  135. int main(int argc, char **argv)
  136. {
  137. char *name;
  138. d_Mask *maskRules;
  139. int changeNull;
  140. double newNullVal;
  141. struct GModule *module;
  142. G_gisinit(argv[0]);
  143. module = G_define_module();
  144. G_add_keyword(_("raster3d"));
  145. G_add_keyword(_("null data"));
  146. G_add_keyword(_("voxel"));
  147. module->description =
  148. _("Explicitly create the 3D NULL-value bitmap file.");
  149. setParams();
  150. if (G_parser(argc, argv))
  151. exit(EXIT_FAILURE);
  152. getParams(&name, &maskRules, &changeNull, &newNullVal);
  153. modifyNull(name, maskRules, changeNull, newNullVal);
  154. exit(EXIT_SUCCESS);
  155. }