main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /****************************************************************************
  2. *
  3. * MODULE: r3.neighbors
  4. *
  5. * AUTHOR(S): Original author
  6. * Soeren Gebbert soerengebbert <at> googlemail <dot> co
  7. * with code from r.series and r.neighbors for parameter menu handling
  8. *
  9. * PURPOSE: Makes each voxel value a function of the values assigned to the voxels
  10. * around it, and stores new voxel values in an output 3D raster map
  11. *
  12. * COPYRIGHT: (C) 2013 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <grass/raster3d.h>
  23. #include <grass/stats.h>
  24. #include <grass/gis.h>
  25. #include <grass/raster.h>
  26. #include <grass/glocale.h>
  27. int nx, ny, nz; /* Number of cells in x, y and z direction */
  28. int x_dist, y_dist, z_dist; /* Distance of cells from the center
  29. to the edge of the moving window */
  30. int x_size, y_size, z_size; /* The size of the moving window in x,
  31. y and z direction */
  32. int size; /* The maximum size of the value buffer */
  33. struct menu
  34. {
  35. stat_func *method; /* routine to compute new value */
  36. char *name; /* method name */
  37. char *text; /* menu display - full description */
  38. } menu[] = {
  39. {
  40. c_ave, "average", "average value"}, {
  41. c_median, "median", "median value"}, {
  42. c_mode, "mode", "most frequently occurring value"}, {
  43. c_min, "minimum", "lowest value"}, {
  44. c_max, "maximum", "highest value"}, {
  45. c_range, "range", "range value"}, {
  46. c_stddev, "stddev", "standard deviation"}, {
  47. c_sum, "sum", "sum of values"}, {
  48. c_count, "count", "count of non-NULL values"}, {
  49. c_var, "variance", "statistical variance"}, {
  50. c_divr, "diversity", "number of different values"}, {
  51. c_intr, "interspersion", "number of values different than center value"},
  52. {
  53. c_quart1, "quart1", "first quartile"}, {
  54. c_quart3, "quart3", "third quartile"}, {
  55. c_perc90, "perc90", "ninetieth percentile"}, {
  56. c_quant, "quantile", "arbitrary quantile"}, {
  57. NULL, NULL, NULL}
  58. };
  59. /* ************************************************************************* */
  60. static char *build_method_list(void)
  61. {
  62. char *buf = G_malloc(1024);
  63. char *p = buf;
  64. int i;
  65. for (i = 0; menu[i].name; i++) {
  66. char *q;
  67. if (i)
  68. *p++ = ',';
  69. for (q = menu[i].name; *q; p++, q++)
  70. *p = *q;
  71. }
  72. *p = '\0';
  73. return buf;
  74. }
  75. /* ************************************************************************* */
  76. static int find_method(const char *method_name)
  77. {
  78. int i;
  79. for (i = 0; menu[i].name; i++)
  80. if (strcmp(menu[i].name, method_name) == 0)
  81. return i;
  82. G_fatal_error(_("Unknown method <%s>"), method_name);
  83. return -1;
  84. }
  85. /* ************************************************************************* */
  86. typedef struct
  87. {
  88. struct Option *input, *output, *window, *method, *quantile;
  89. } paramType;
  90. paramType param;
  91. static void set_params()
  92. {
  93. param.input = G_define_standard_option(G_OPT_R3_INPUT);
  94. param.output = G_define_standard_option(G_OPT_R3_OUTPUT);
  95. param.method = G_define_option();
  96. param.method->key = "method";
  97. param.method->type = TYPE_STRING;
  98. param.method->required = YES;
  99. param.method->options = build_method_list();
  100. param.method->description = _("Aggregate operation");
  101. param.method->multiple = NO;
  102. param.quantile = G_define_option();
  103. param.quantile->key = "quantile";
  104. param.quantile->type = TYPE_DOUBLE;
  105. param.quantile->required = NO;
  106. param.quantile->description =
  107. _("Quantile to calculate for method=quantile");
  108. param.quantile->options = "0.0-1.0";
  109. param.quantile->multiple = NO;
  110. param.window = G_define_option();
  111. param.window->key = "window";
  112. param.window->type = TYPE_INTEGER;
  113. param.window->required = YES;
  114. param.window->key_desc = "x,y,z";
  115. param.window->description =
  116. _("The size of the window in x, y and z direction,"
  117. " values must be odd integer numbers, eg: 3,3,3");
  118. }
  119. /* ************************************************************************* */
  120. static int gather_values(RASTER3D_Map * map, DCELL * buff, int x,
  121. int y, int z)
  122. {
  123. int i, j, k, l;
  124. DCELL value;
  125. int start_z = z - z_dist;
  126. int start_y = y - y_dist;
  127. int start_x = x - x_dist;
  128. int end_z = start_z + z_size;
  129. int end_y = start_y + y_size;
  130. int end_x = start_x + x_size;
  131. if (start_z < 0)
  132. start_z = 0;
  133. if (start_y < 0)
  134. start_y = 0;
  135. if (start_x < 0)
  136. start_x = 0;
  137. if (end_z > nz)
  138. end_z = nz;
  139. if (end_y > ny)
  140. end_y = ny;
  141. if (end_x > nx)
  142. end_x = nx;
  143. l = 0;
  144. for (i = start_z; i < end_z; i++) {
  145. for (j = start_y; j < end_y; j++) {
  146. for (k = start_x; k < end_x; k++) {
  147. value = (DCELL) Rast3d_get_double(map, k, j, i);
  148. if (Rast_is_d_null_value(&value))
  149. continue;
  150. buff[l] = value;
  151. l++;
  152. }
  153. }
  154. }
  155. return l;
  156. }
  157. /* ************************************************************************* */
  158. int main(int argc, char **argv)
  159. {
  160. RASTER3D_Map *input;
  161. RASTER3D_Map *output;
  162. RASTER3D_Region region;
  163. struct GModule *module;
  164. stat_func *method_fn;
  165. double quantile;
  166. int x, y, z;
  167. /* Initialize GRASS */
  168. G_gisinit(argv[0]);
  169. module = G_define_module();
  170. G_add_keyword(_("raster3d"));
  171. G_add_keyword(_("algebra"));
  172. G_add_keyword(_("voxel"));
  173. G_add_keyword(_("statistics"));
  174. G_add_keyword(_("aggregation"));
  175. G_add_keyword(_("neighbor"));
  176. G_add_keyword(_("focal statistics"));
  177. G_add_keyword(_("filter"));
  178. module->description =
  179. _("Makes each voxel value a "
  180. "function of the values assigned to the voxels "
  181. "around it, and stores new voxel values in an output 3D raster map");
  182. /* Get parameters from user */
  183. set_params();
  184. if (G_parser(argc, argv))
  185. exit(EXIT_FAILURE);
  186. if (NULL == G_find_raster3d(param.input->answer, ""))
  187. Rast3d_fatal_error(_("3D raster map <%s> not found"),
  188. param.input->answer);
  189. Rast3d_init_defaults();
  190. Rast3d_get_window(&region);
  191. nx = region.cols;
  192. ny = region.rows;
  193. nz = region.depths;
  194. /* Size fo the moving window */
  195. x_size = atoi(param.window->answers[0]);
  196. y_size = atoi(param.window->answers[1]);
  197. z_size = atoi(param.window->answers[2]);
  198. /* Distances in all directions */
  199. x_dist = x_size / 2;
  200. y_dist = y_size / 2;
  201. z_dist = z_size / 2;
  202. /* Maximum size of the buffer */
  203. size = x_size * y_size * z_size;
  204. /* Set the computation method */
  205. method_fn = menu[find_method(param.method->answer)].method;
  206. if (param.quantile->answer)
  207. quantile = atof(param.quantile->answer);
  208. else
  209. quantile = 0.0;
  210. input = Rast3d_open_cell_old(param.input->answer,
  211. G_find_raster3d(param.input->answer, ""),
  212. &region, RASTER3D_TILE_SAME_AS_FILE,
  213. RASTER3D_USE_CACHE_DEFAULT);
  214. if (input == NULL)
  215. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"),
  216. param.input->answer);
  217. output =
  218. Rast3d_open_new_opt_tile_size(param.output->answer,
  219. RASTER3D_USE_CACHE_X, &region,
  220. DCELL_TYPE, 32);
  221. if (output == NULL)
  222. Rast3d_fatal_error(_("Unable to open 3D raster map <%s>"),
  223. param.output->answer);
  224. Rast3d_min_unlocked(output, RASTER3D_USE_CACHE_X);
  225. Rast3d_autolock_on(output);
  226. Rast3d_unlock_all(output);
  227. DCELL *buff = NULL, value;
  228. buff = (DCELL *) calloc(size, sizeof(DCELL));
  229. if (buff == NULL)
  230. Rast3d_fatal_error(_("Unable to allocate buffer"));
  231. for (z = 0; z < nz; z++) {
  232. G_percent(z, nz, 1);
  233. for (y = 0; y < ny; y++) {
  234. for (x = 0; x < nx; x++) {
  235. /* Gather values in moving window */
  236. int num = gather_values(input, buff, x, y, z);
  237. /* Compute the resulting value */
  238. if (num > 0)
  239. (*method_fn) (&value, buff, num, &quantile);
  240. else
  241. Rast_set_d_null_value(&value, 1);
  242. /* Write the value */
  243. Rast3d_put_double(output, x, y, z, value);
  244. }
  245. }
  246. }
  247. G_percent(z, nz, 1);
  248. free(buff);
  249. if (!Rast3d_flush_all_tiles(output))
  250. G_fatal_error(_("Error flushing tiles"));
  251. Rast3d_autolock_off(output);
  252. Rast3d_unlock_all(output);
  253. Rast3d_close(input);
  254. Rast3d_close(output);
  255. return 0;
  256. }