main.c 5.5 KB

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