main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. *
  3. * MODULE: r.surf.contour
  4. * AUTHOR(S): Chuck Ehlschlaeger (original contributor)
  5. * Markus Neteler <neteler itc.it>,
  6. * Bernhard Reiter <bernhard intevation.de>,
  7. * Brad Douglas <rez touchofmadness.com>,
  8. * Huidae Cho <grass4u gmail.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Hamish Bowman <hamish_b yahoo.com>,
  11. * Jan-Oliver Wagner <jan intevation.de>,
  12. * Markus Metz
  13. * PURPOSE: Interpolates a raster elevation map from a rasterized
  14. * contour map
  15. * COPYRIGHT: (C) 1999-2010 by the GRASS Development Team
  16. *
  17. * This program is free software under the GNU General Public
  18. * License (>=v2). Read the file COPYING that comes with GRASS
  19. * for details.
  20. *
  21. *****************************************************************************/
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "contour.h"
  25. #include <unistd.h>
  26. #include <grass/gis.h>
  27. #include <grass/raster.h>
  28. #include <grass/glocale.h>
  29. int nrows;
  30. int ncols;
  31. int minc;
  32. int minr;
  33. int maxc;
  34. int maxr;
  35. int array_size;
  36. double i_val_l_f;
  37. DCELL **con;
  38. FLAG *seen, *mask;
  39. NODE *zero;
  40. int main(int argc, char *argv[])
  41. {
  42. int r, c;
  43. DCELL con1, con2;
  44. double d1, d2;
  45. DCELL *alt_row;
  46. const char *con_name, *alt_name;
  47. int file_fd;
  48. DCELL value;
  49. struct History history;
  50. struct GModule *module;
  51. struct Option *opt1, *opt2;
  52. G_gisinit(argv[0]);
  53. module = G_define_module();
  54. G_add_keyword(_("raster"));
  55. G_add_keyword(_("surface"));
  56. G_add_keyword(_("interpolation"));
  57. module->description =
  58. _("Generates surface raster map from rasterized contours.");
  59. opt1 = G_define_standard_option(G_OPT_R_INPUT);
  60. opt1->description = _("Name of input raster map containing contours");
  61. opt2 = G_define_standard_option(G_OPT_R_OUTPUT);
  62. if (G_parser(argc, argv))
  63. exit(EXIT_FAILURE);
  64. con_name = opt1->answer;
  65. alt_name = opt2->answer;
  66. nrows = Rast_window_rows();
  67. ncols = Rast_window_cols();
  68. i_val_l_f = nrows + ncols;
  69. con = read_cell(con_name);
  70. alt_row = (DCELL *) G_malloc(ncols * sizeof(DCELL));
  71. seen = flag_create(nrows, ncols);
  72. mask = flag_create(nrows, ncols);
  73. if (NULL != G_find_file("cell", "MASK", G_mapset())) {
  74. file_fd = Rast_open_old("MASK", G_mapset());
  75. for (r = 0; r < nrows; r++) {
  76. Rast_get_d_row_nomask(file_fd, alt_row, r);
  77. for (c = 0; c < ncols; c++)
  78. if (Rast_is_d_null_value(&(alt_row[c])) || alt_row[c] == 0)
  79. FLAG_SET(mask, r, c);
  80. }
  81. Rast_close(file_fd);
  82. }
  83. zero = (NODE *) G_malloc(INIT_AR * sizeof(NODE));
  84. minc = minr = 0;
  85. maxc = ncols - 1;
  86. maxr = nrows - 1;
  87. array_size = INIT_AR;
  88. file_fd = Rast_open_new(alt_name, DCELL_TYPE);
  89. for (r = 0; r < nrows; r++) {
  90. G_percent(r, nrows, 1);
  91. Rast_set_d_null_value(alt_row, ncols);
  92. for (c = 0; c < ncols; c++) {
  93. if (FLAG_GET(mask, r, c))
  94. continue;
  95. value = con[r][c];
  96. if (!Rast_is_d_null_value(&value)) {
  97. alt_row[c] = value;
  98. continue;
  99. }
  100. find_con(r, c, &d1, &d2, &con1, &con2);
  101. if (!Rast_is_d_null_value(&con2))
  102. alt_row[c] = d2 * con1 / (d1 + d2) +
  103. d1 * con2 / (d1 + d2);
  104. else
  105. alt_row[c] = con1;
  106. }
  107. Rast_put_row(file_fd, alt_row, DCELL_TYPE);
  108. }
  109. G_percent(1, 1, 1);
  110. free_cell(con);
  111. flag_destroy(seen);
  112. flag_destroy(mask);
  113. Rast_close(file_fd);
  114. Rast_short_history(alt_name, "raster", &history);
  115. Rast_command_history(&history);
  116. Rast_write_history(alt_name, &history);
  117. exit(EXIT_SUCCESS);
  118. }