main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /***************************************************************************
  2. * MODULE: r3.mask
  3. *
  4. * AUTHOR(S): Roman Waupotitsch, Michael Shapiro, Helena Mitasova,
  5. * Bill Brown, Lubos Mitas, Jaro Hofierka
  6. *
  7. * PURPOSE: Establishes the current working 3D raster mask.
  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. /*--------------------------------------------------------------------------*/
  23. typedef struct
  24. {
  25. struct Option *map, *maskVals;
  26. } paramType;
  27. static paramType params;
  28. /*--------------------------------------------------------------------------*/
  29. void getParams(char **name, d_Mask ** maskRules)
  30. {
  31. *name = params.map->answer;
  32. Rast3d_parse_vallist(params.maskVals->answers, maskRules);
  33. }
  34. /*-------------------------------------------------------------------------*/
  35. static void makeMask(char *name, d_Mask * maskRules)
  36. {
  37. void *map, *mask;
  38. RASTER3D_Region region;
  39. int tileX, tileY, tileZ, x, y, z, cacheSize;
  40. double value;
  41. float floatNull;
  42. cacheSize = Rast3d_cache_size_encode(RASTER3D_USE_CACHE_XY, 1);
  43. if (NULL == G_find_raster3d(name, ""))
  44. Rast3d_fatal_error(_("3D raster map <%s> not found"), name);
  45. map = Rast3d_open_cell_old(name, G_mapset(), RASTER3D_DEFAULT_WINDOW,
  46. DCELL_TYPE, cacheSize);
  47. if (map == NULL)
  48. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"), name);
  49. Rast3d_get_region_struct_map(map, &region);
  50. Rast3d_get_tile_dimensions_map(map, &tileX, &tileY, &tileZ);
  51. mask = Rast3d_open_new_param(Rast3d_mask_file(), FCELL_TYPE, cacheSize,
  52. &region, FCELL_TYPE, RASTER3D_COMPRESSION, 0,
  53. tileX, tileY, tileZ);
  54. if (mask == NULL)
  55. Rast3d_fatal_error(_("Unable to open 3D raster mask file"));
  56. Rast3d_min_unlocked(map, RASTER3D_USE_CACHE_X);
  57. Rast3d_autolock_on(map);
  58. Rast3d_unlock_all(map);
  59. Rast3d_min_unlocked(mask, RASTER3D_USE_CACHE_X);
  60. Rast3d_autolock_on(mask);
  61. Rast3d_unlock_all(mask);
  62. Rast3d_set_null_value(&floatNull, 1, FCELL_TYPE);
  63. for (z = 0; z < region.depths; z++) {
  64. if ((z % tileZ) == 0) {
  65. Rast3d_unlock_all(map);
  66. Rast3d_unlock_all(mask);
  67. }
  68. for (y = 0; y < region.rows; y++) /* We count from north to south in the cube coordinate system */
  69. for (x = 0; x < region.cols; x++) {
  70. value = Rast3d_get_double_region(map, x, y, z);
  71. if (Rast3d_mask_d_select((DCELL *) & value, maskRules))
  72. Rast3d_put_float(mask, x, y, z, (float)floatNull); /* mask-out value */
  73. else
  74. Rast3d_put_float(mask, x, y, z, (float)0.0); /* not mask-out value */
  75. }
  76. if ((z % tileZ) == 0) {
  77. if (!Rast3d_flush_tiles_in_cube
  78. (mask, 0, 0, MAX(0, z - tileZ), region.rows - 1,
  79. region.cols - 1, z))
  80. Rast3d_fatal_error(_("makeMask: error flushing tiles in cube"));
  81. }
  82. }
  83. if (!Rast3d_flush_all_tiles(mask))
  84. Rast3d_fatal_error(_("makeMask: error flushing all tiles"));
  85. Rast3d_autolock_off(map);
  86. Rast3d_unlock_all(map);
  87. Rast3d_autolock_off(mask);
  88. Rast3d_unlock_all(mask);
  89. if (!Rast3d_close(mask))
  90. Rast3d_fatal_error(_("Unable to close 3D raster mask file"));
  91. if (!Rast3d_close(map))
  92. Rast3d_fatal_error(_("Unable to close raster map <%s>"), name);
  93. }
  94. /*--------------------------------------------------------------------------*/
  95. int main(int argc, char *argv[])
  96. {
  97. char *name;
  98. d_Mask *maskRules;
  99. struct GModule *module;
  100. G_gisinit(argv[0]);
  101. module = G_define_module();
  102. G_add_keyword(_("raster3d"));
  103. G_add_keyword(_("mask"));
  104. G_add_keyword(_("null data"));
  105. G_add_keyword(_("no-data"));
  106. G_add_keyword(_("voxel"));
  107. module->description =
  108. _("Establishes the current working 3D raster mask.");
  109. params.map = G_define_option();
  110. params.map->key = "map";
  111. params.map->type = TYPE_STRING;
  112. params.map->required = YES;
  113. params.map->multiple = NO;
  114. params.map->gisprompt = "old,grid3,3d-raster";
  115. params.map->description = _("3D raster map with reference values");
  116. params.maskVals = G_define_option();
  117. params.maskVals->key = "maskvalues";
  118. params.maskVals->key_desc = "val[-val]";
  119. params.maskVals->type = TYPE_STRING;
  120. params.maskVals->required = NO;
  121. params.maskVals->multiple = YES;
  122. params.maskVals->description = _("List of cell values to be masked out");
  123. if (G_parser(argc, argv))
  124. exit(EXIT_FAILURE);
  125. if (Rast3d_mask_file_exists())
  126. G_fatal_error(_("Cannot create mask file: RASTER3D_MASK already exists"));
  127. getParams(&name, &maskRules);
  128. makeMask(name, maskRules);
  129. exit(EXIT_SUCCESS);
  130. }
  131. /*--------------------------------------------------------------------------*/
  132. /*--------------------------------------------------------------------------*/
  133. /*--------------------------------------------------------------------------*/