main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /****************************************************************************
  2. *
  3. * MODULE: r.uslek
  4. * AUTHOR(S): Yann Chemin - yann.chemin@gmail.com
  5. * PURPOSE: Transforms percentage of texture (sand/clay/silt)
  6. * into USDA 1951 (p209) soil texture classes and then
  7. * into USLE soil erodibility factor (K) as an output
  8. *
  9. * COPYRIGHT: (C) 2002-2008, 2010 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/raster.h>
  21. #include <grass/glocale.h>
  22. #define POLYGON_DIMENSION 20
  23. double tex2usle_k(int texture, double om_in);
  24. int prct2tex(double sand_input, double clay_input, double silt_input);
  25. double point_in_triangle(double point_x, double point_y, double point_z,
  26. double t1_x, double t1_y, double t1_z, double t2_x,
  27. double t2_y, double t2_z, double t3_x, double t3_y,
  28. double t3_z);
  29. int main(int argc, char *argv[])
  30. {
  31. int nrows, ncols;
  32. int row, col;
  33. struct GModule *module;
  34. struct Option *input1, *input2, *input3, *input4, *output1;
  35. struct History history; /*metadata */
  36. char *result; /*output raster name */
  37. int infd_psand, infd_psilt, infd_pclay, infd_pomat;
  38. int outfd;
  39. char *psand, *psilt, *pclay, *pomat;
  40. void *inrast_psand, *inrast_psilt, *inrast_pclay, *inrast_pomat;
  41. DCELL *outrast;
  42. /************************************/
  43. G_gisinit(argv[0]);
  44. module = G_define_module();
  45. G_add_keyword(_("raster"));
  46. G_add_keyword(_("hydrology"));
  47. G_add_keyword(_("soil"));
  48. G_add_keyword(_("erosion"));
  49. module->description = _("Computes USLE Soil Erodibility Factor (K).");
  50. /* Define the different options */
  51. input1 = G_define_standard_option(G_OPT_R_INPUT);
  52. input1->key = "psand";
  53. input1->description = _("Name of soil sand fraction raster map [0.0-1.0]");
  54. input2 = G_define_standard_option(G_OPT_R_INPUT);
  55. input2->key = "pclay";
  56. input2->description = _("Name of soil clay fraction raster map [0.0-1.0]");
  57. input3 = G_define_standard_option(G_OPT_R_INPUT);
  58. input3->key = "psilt";
  59. input3->description = _("Name of soil silt fraction raster map [0.0-1.0]");
  60. input4 = G_define_standard_option(G_OPT_R_INPUT);
  61. input4->key = "pomat";
  62. input4->description = _("Name of soil organic matter raster map [0.0-1.0]");
  63. output1 = G_define_standard_option(G_OPT_R_OUTPUT);
  64. output1->description = _("Name for output USLE K factor raster map [t.ha.hr/ha.MJ.mm]");
  65. /********************/
  66. if (G_parser(argc, argv))
  67. exit(EXIT_FAILURE);
  68. psand = input1->answer;
  69. pclay = input2->answer;
  70. psilt = input3->answer;
  71. pomat = input4->answer;
  72. result = output1->answer;
  73. /***************************************************/
  74. infd_psand = Rast_open_old(psand, "");
  75. inrast_psand = Rast_allocate_d_buf();
  76. infd_psilt = Rast_open_old(psilt, "");
  77. inrast_psilt = Rast_allocate_d_buf();
  78. infd_pclay = Rast_open_old(pclay, "");
  79. inrast_pclay = Rast_allocate_d_buf();
  80. infd_pomat = Rast_open_old(pomat, "");
  81. inrast_pomat = Rast_allocate_d_buf();
  82. /***************************************************/
  83. nrows = Rast_window_rows();
  84. ncols = Rast_window_cols();
  85. outrast = Rast_allocate_d_buf();
  86. /* Create New raster files */
  87. outfd = Rast_open_new(result, DCELL_TYPE);
  88. /* Process pixels */
  89. for (row = 0; row < nrows; row++)
  90. {
  91. DCELL d;
  92. DCELL d_sand;
  93. DCELL d_clay;
  94. DCELL d_silt;
  95. DCELL d_om;
  96. G_percent(row, nrows, 2);
  97. /* read soil input maps */
  98. Rast_get_d_row(infd_psand, inrast_psand, row);
  99. Rast_get_d_row(infd_psilt, inrast_psilt, row);
  100. Rast_get_d_row(infd_pclay, inrast_pclay, row);
  101. Rast_get_d_row(infd_pomat, inrast_pomat, row);
  102. /*process the data */
  103. for (col = 0; col < ncols; col++)
  104. {
  105. d_sand = ((DCELL *) inrast_psand)[col];
  106. d_silt = ((DCELL *) inrast_psilt)[col];
  107. d_clay = ((DCELL *) inrast_pclay)[col];
  108. d_om = ((DCELL *) inrast_pomat)[col];
  109. if (Rast_is_d_null_value(&d_sand) ||
  110. Rast_is_d_null_value(&d_clay) ||
  111. Rast_is_d_null_value(&d_silt))
  112. Rast_set_d_null_value(&outrast[col], 1);
  113. else {
  114. /***************************************/
  115. /* In case some map input not standard */
  116. if ((d_sand + d_clay + d_silt) != 1.0)
  117. Rast_set_d_null_value(&outrast[col], 1);
  118. /* if OM==NULL then make it 0.0 */
  119. else if (Rast_is_d_null_value(&d_om))
  120. d_om = 0.0;
  121. else {
  122. /************************************/
  123. /* convert to usle_k */
  124. d = (double)prct2tex(d_sand, d_clay, d_silt);
  125. d = tex2usle_k((int)d, d_om);
  126. outrast[col] = d;
  127. }
  128. }
  129. }
  130. Rast_put_d_row(outfd, outrast);
  131. }
  132. G_free(inrast_psand);
  133. G_free(inrast_psilt);
  134. G_free(inrast_pclay);
  135. G_free(inrast_pomat);
  136. Rast_close(infd_psand);
  137. Rast_close(infd_psilt);
  138. Rast_close(infd_pclay);
  139. Rast_close(infd_pomat);
  140. G_free(outrast);
  141. Rast_close(outfd);
  142. Rast_short_history(result, "raster", &history);
  143. Rast_command_history(&history);
  144. Rast_write_history(result, &history);
  145. exit(EXIT_SUCCESS);
  146. }