main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. #define MAX(a,b) (a > b ? a : b)
  23. typedef struct
  24. {
  25. struct Option *map, *setNull, *null;
  26. } paramType;
  27. static paramType params;
  28. /* function prototypes */
  29. static void setParams(void);
  30. static void getParams(char **name, d_Mask ** maskRules, int *changeNull,
  31. double *newNullVal);
  32. static void modifyNull(char *name, d_Mask * maskRules, int changeNull,
  33. double newNullVal);
  34. static void setParams(void)
  35. {
  36. params.map = G_define_option();
  37. params.map->key = "map";
  38. params.map->type = TYPE_STRING;
  39. params.map->required = YES;
  40. params.map->multiple = NO;
  41. params.map->gisprompt = "old,grid3,3d-raster";
  42. params.map->description =
  43. _("3d raster map for which to modify null values");
  44. params.setNull = G_define_option();
  45. params.setNull->key = "setnull";
  46. params.setNull->key_desc = "val[-val]";
  47. params.setNull->type = TYPE_STRING;
  48. params.setNull->required = NO;
  49. params.setNull->multiple = YES;
  50. params.setNull->description = _("List of cell values to be set to NULL");
  51. params.null = G_define_option();
  52. params.null->key = "null";
  53. params.null->type = TYPE_DOUBLE;
  54. params.null->required = NO;
  55. params.null->multiple = NO;
  56. params.null->description = _("The value to replace the null value by");
  57. }
  58. /*--------------------------------------------------------------------------*/
  59. static void
  60. getParams(char **name, d_Mask ** maskRules, int *changeNull,
  61. double *newNullVal)
  62. {
  63. *name = params.map->answer;
  64. Rast3d_parse_vallist(params.setNull->answers, maskRules);
  65. *changeNull = (params.null->answer != NULL);
  66. if (*changeNull)
  67. if (sscanf(params.null->answer, "%lf", newNullVal) != 1)
  68. Rast3d_fatal_error(_("Illegal value for null"));
  69. }
  70. /*-------------------------------------------------------------------------*/
  71. static void
  72. modifyNull(char *name, d_Mask * maskRules, int changeNull, double newNullVal)
  73. {
  74. void *map, *mapOut;
  75. RASTER3D_Region region;
  76. int tileX, tileY, tileZ, x, y, z;
  77. double value;
  78. int doCompress, doLzw, doRle, precision;
  79. int cacheSize;
  80. cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);
  81. if (NULL == G_find_raster3d(name, ""))
  82. Rast3d_fatal_error(_("3D raster map <%s> not found"), name);
  83. fprintf(stderr, "name %s Mapset %s \n", name, G_mapset());
  84. map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
  85. DCELL_TYPE, cacheSize);
  86. if (map == NULL)
  87. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  88. Rast3d_get_region_struct_map(map, &region);
  89. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  90. Rast3d_get_compression_mode(&doCompress, &precision);
  91. mapOut = Rast3d_open_new_param(name, DCELL_TYPE, RASTER3D_USE_CACHE_XY,
  92. &region, Rast3d_file_type_map(map),
  93. doCompress, Rast3d_tile_precision_map(map), tileX,
  94. tileY, tileZ);
  95. if (mapOut == NULL)
  96. Rast3d_fatal_error(_("modifyNull: error opening tmp file"));
  97. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  98. Rast3d_autolock_on(map);
  99. Rast3d_unlock_all(map);
  100. Rast3d_min_unlocked(mapOut, RASTER3D_USE_CACHE_X);
  101. Rast3d_autolock_on(mapOut);
  102. Rast3d_unlock_all(mapOut);
  103. for (z = 0; z < region.depths; z++) {
  104. if ((z % tileZ) == 0) {
  105. Rast3d_unlock_all(map);
  106. Rast3d_unlock_all(mapOut);
  107. }
  108. for (y = 0; y < region.rows; y++)
  109. for (x = 0; x < region.cols; x++) {
  110. value = Rast3d_get_double_region(map, x, y, z);
  111. if (Rast3d_is_null_value_num(&value, DCELL_TYPE)) {
  112. if (changeNull) {
  113. value = newNullVal;
  114. }
  115. }
  116. else if (Rast3d_mask_d_select((DCELL *) & value, maskRules)) {
  117. Rast3d_set_null_value(&value, 1, DCELL_TYPE);
  118. }
  119. Rast3d_put_double(mapOut, x, y, z, value);
  120. }
  121. if ((z % tileZ) == 0) {
  122. if (!Rast3d_flush_tiles_in_cube
  123. (mapOut, 0, 0, MAX(0, z - tileZ), region.rows - 1,
  124. region.cols - 1, z))
  125. Rast3d_fatal_error(_("modifyNull: error flushing tiles in cube"));
  126. }
  127. }
  128. if (!Rast3d_flush_all_tiles(mapOut))
  129. Rast3d_fatal_error(_("modifyNull: error flushing all tiles"));
  130. Rast3d_autolock_off(map);
  131. Rast3d_unlock_all(map);
  132. Rast3d_autolock_off(mapOut);
  133. Rast3d_unlock_all(mapOut);
  134. if (!Rast3d_close(map))
  135. Rast3d_fatal_error(_("Unable to close 3D raster map <%s>"), name);
  136. if (!Rast3d_close(mapOut))
  137. Rast3d_fatal_error(_("modifyNull: Unable to close tmp file"));
  138. }
  139. /*--------------------------------------------------------------------------*/
  140. int main(int argc, char **argv)
  141. {
  142. char *name;
  143. d_Mask *maskRules;
  144. int changeNull;
  145. double newNullVal;
  146. struct GModule *module;
  147. G_gisinit(argv[0]);
  148. module = G_define_module();
  149. G_add_keyword(_("raster3d"));
  150. G_add_keyword(_("voxel"));
  151. module->description =
  152. _("Explicitly create the 3D NULL-value bitmap file.");
  153. setParams();
  154. if (G_parser(argc, argv))
  155. exit(EXIT_FAILURE);
  156. getParams(&name, &maskRules, &changeNull, &newNullVal);
  157. modifyNull(name, maskRules, changeNull, newNullVal);
  158. exit(EXIT_SUCCESS);
  159. }