r3.null.main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/G3d.h>
  21. #include "mask_functions.h"
  22. #include <grass/glocale.h>
  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. 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. G3d_fatalError(_("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. G3D_Region region;
  77. int tileX, tileY, tileZ, x, y, z;
  78. double value;
  79. int doCompress, doLzw, doRle, precision;
  80. int cacheSize;
  81. cacheSize = G3d_cacheSizeEncode(G3D_USE_CACHE_XY, 1);
  82. if (NULL == G_find_grid3(name, ""))
  83. G3d_fatalError(_("Requested 3d raster map not found"));
  84. fprintf(stderr, "name %s Mapset %s \n", name, G_mapset());
  85. map = G3d_openCellOld(name, G_mapset(), G3D_DEFAULT_WINDOW,
  86. DCELL_TYPE, cacheSize);
  87. if (map == NULL)
  88. G3d_fatalError(_("modifyNull: error opening map"));
  89. G3d_getRegionStructMap(map, &region);
  90. G3d_getTileDimensionsMap(map, &tileX, &tileY, &tileZ);
  91. G3d_getCompressionMode(&doCompress, &doLzw, &doRle, &precision);
  92. mapOut = G3d_openNewParam(name, DCELL_TYPE, G3D_USE_CACHE_XY,
  93. &region, G3d_fileTypeMap(map),
  94. doLzw, doRle, G3d_tilePrecisionMap(map), tileX,
  95. tileY, tileZ);
  96. if (mapOut == NULL)
  97. G3d_fatalError(_("modifyNull: error opening tmp file"));
  98. G3d_minUnlocked(map, G3D_USE_CACHE_X);
  99. G3d_autolockOn(map);
  100. G3d_unlockAll(map);
  101. G3d_minUnlocked(mapOut, G3D_USE_CACHE_X);
  102. G3d_autolockOn(mapOut);
  103. G3d_unlockAll(mapOut);
  104. /*AV*/
  105. /* BEGIN OF ORIGINAL CODE */
  106. /*
  107. * for (z = 0; z < region.depths; z++) {
  108. * if ((z % tileZ) == 0) {
  109. * G3d_unlockAll (map);
  110. * G3d_unlockAll (mapOut);
  111. * }
  112. * for (y = 0; y < region.cols; y++)
  113. * for (x = 0; x < region.rows; x++) {
  114. */
  115. /* END OF ORIGINAL CODE */
  116. /*AV*/
  117. /* BEGIN OF MY CODE */
  118. for (z = 0; z < region.depths; z++) {
  119. if ((z % tileZ) == 0) {
  120. G3d_unlockAll(map);
  121. G3d_unlockAll(mapOut);
  122. }
  123. for (y = region.rows - 1; y >= 0; y--)
  124. for (x = 0; x < region.cols; x++) {
  125. /* END OF MY CODE */
  126. value = G3d_getDoubleRegion(map, x, y, z);
  127. if (G3d_isNullValueNum(&value, DCELL_TYPE)) {
  128. if (changeNull) {
  129. value = newNullVal;
  130. }
  131. }
  132. else if (mask_d_select((DCELL *) & value, maskRules)) {
  133. G3d_setNullValue(&value, 1, DCELL_TYPE);
  134. }
  135. G3d_putDouble(mapOut, x, y, z, value);
  136. }
  137. if ((z % tileZ) == 0) {
  138. if (!G3d_flushTilesInCube
  139. (mapOut, 0, 0, MAX(0, z - tileZ), region.rows - 1,
  140. region.cols - 1, z))
  141. G3d_fatalError(_("modifyNull: error flushing tiles in cube"));
  142. }
  143. }
  144. if (!G3d_flushAllTiles(mapOut))
  145. G3d_fatalError(_("modifyNull: error flushing all tiles"));
  146. G3d_autolockOff(map);
  147. G3d_unlockAll(map);
  148. G3d_autolockOff(mapOut);
  149. G3d_unlockAll(mapOut);
  150. if (!G3d_closeCell(map))
  151. G3d_fatalError(_("modifyNull: error closing map"));
  152. if (!G3d_closeCell(mapOut))
  153. G3d_fatalError(_("modifyNull: error closing tmp file"));
  154. }
  155. /*--------------------------------------------------------------------------*/
  156. int main(int argc, char **argv)
  157. {
  158. char *name;
  159. d_Mask *maskRules;
  160. int changeNull;
  161. double newNullVal;
  162. struct GModule *module;
  163. G_gisinit(argv[0]);
  164. module = G_define_module();
  165. module->keywords = _("raster3d, voxel");
  166. module->description =
  167. _("Explicitly create the 3D NULL-value bitmap file.");
  168. setParams();
  169. if (G_parser(argc, argv))
  170. exit(EXIT_FAILURE);
  171. getParams(&name, &maskRules, &changeNull, &newNullVal);
  172. modifyNull(name, maskRules, changeNull, newNullVal);
  173. exit(EXIT_SUCCESS);
  174. }