main.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /****************************************************************************
  2. *
  3. * MODULE: r.spreadpath
  4. * AUTHOR(S): Jianping Xu 1995: WIldfire SPread Simulation, WiSpS (original contributor)
  5. * Markus Neteler <neteler itc.it>
  6. * Roberto Flor <flor itc.it>, Brad Douglas <rez touchofmadness.com>,
  7. * Glynn Clements <glynn gclements.plus.com>, Jachym Cepicky <jachym les-ejk.cz>
  8. * PURPOSE: This is the main program for tracing out the shortest path(s)
  9. * based on the raster map showing back path cells from which the
  10. * cumulative costs were determined.
  11. * COPYRIGHT: (C) 2000-2006 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. /**********************************************************************/
  19. /* */
  20. /* This is the main program for tracing out the shortest path(s) */
  21. /* based on the raster map showing back path cells from which the */
  22. /* cumulative costs were determined. */
  23. /* */
  24. /**********************************************************************/
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <grass/segment.h>
  30. #include <grass/gis.h>
  31. #include <grass/raster.h>
  32. #include "stash.h"
  33. #include <grass/glocale.h>
  34. #include "local_proto.h"
  35. struct variables
  36. {
  37. char *alias;
  38. int position;
  39. } variables[] = {
  40. {"x_input", BACKCOL_LAYER},
  41. {"y_input", BACKROW_LAYER},
  42. {"coor", START_PT},
  43. {"output", PATH_LAYER}
  44. };
  45. char path_layer[64];
  46. char backrow_layer[64];
  47. char backcol_layer[64];
  48. struct point *head_start_pt = NULL;
  49. char *value;
  50. int nrows, ncols;
  51. SEGMENT in_row_seg, in_col_seg, out_seg;
  52. int main(int argc, char **argv)
  53. {
  54. int n,
  55. backrow, backcol,
  56. col, row,
  57. len, flag,
  58. srows, scols,
  59. backrow_fd, backcol_fd, path_fd, in_row_fd, in_col_fd, out_fd;
  60. const char *current_mapset,
  61. *search_mapset,
  62. *path_mapset,
  63. *backrow_mapset,
  64. *backcol_mapset, *in_row_file, *in_col_file, *out_file;
  65. CELL *cell;
  66. POINT *PRES_PT, *PRESENT_PT, *OLD_PT;
  67. struct Cell_head window;
  68. double east, north;
  69. struct Option *opt1, *opt2, *opt3, *opt4;
  70. struct GModule *module;
  71. G_gisinit(argv[0]);
  72. /* Set description */
  73. module = G_define_module();
  74. G_add_keyword(_("raster"));
  75. G_add_keyword(_("fire"));
  76. G_add_keyword(_("cumulative costs"));
  77. module->description =
  78. _("Recursively traces the least cost path backwards to "
  79. "cells from which the cumulative cost was determined.");
  80. opt1 = G_define_standard_option(G_OPT_R_INPUT);
  81. opt1->key = "x_input";
  82. opt1->description =
  83. _("Name of raster map containing back-path easting information");
  84. opt2 = G_define_standard_option(G_OPT_R_INPUT);
  85. opt2->key = "y_input";
  86. opt2->description =
  87. _("Name of raster map containing back-path northing information");
  88. opt3 = G_define_standard_option(G_OPT_M_COORDS);
  89. opt3->multiple = YES;
  90. opt3->description =
  91. _("The map E and N grid coordinates of starting points");
  92. opt4 = G_define_standard_option(G_OPT_R_OUTPUT);
  93. /* Do command line parsing */
  94. if (G_parser(argc, argv))
  95. exit(EXIT_FAILURE);
  96. current_mapset = G_mapset();
  97. in_row_file = G_tempfile();
  98. in_col_file = G_tempfile();
  99. out_file = G_tempfile();
  100. /* Get database window parameters */
  101. G_get_window(&window);
  102. /* Check if backrow layer exists in data base */
  103. search_mapset = "";
  104. strcpy(backrow_layer, opt2->answer);
  105. strcpy(backcol_layer, opt1->answer);
  106. backrow_mapset = G_find_raster(backrow_layer, search_mapset);
  107. backcol_mapset = G_find_raster(backcol_layer, search_mapset);
  108. if (backrow_mapset == NULL)
  109. G_fatal_error("%s - not found", backrow_layer);
  110. if (backcol_mapset == NULL)
  111. G_fatal_error("%s - not found", backcol_layer);
  112. search_mapset = "";
  113. strcpy(path_layer, opt4->answer);
  114. path_mapset = G_find_raster(path_layer, search_mapset);
  115. /* find number of rows and cols in window */
  116. nrows = Rast_window_rows();
  117. ncols = Rast_window_cols();
  118. cell = Rast_allocate_c_buf();
  119. /* Open back cell layers for reading */
  120. backrow_fd = Rast_open_old(backrow_layer, backrow_mapset);
  121. backcol_fd = Rast_open_old(backcol_layer, backcol_mapset);
  122. /* Parameters for map submatrices */
  123. len = sizeof(CELL);
  124. srows = nrows / 4 + 1;
  125. scols = ncols / 4 + 1;
  126. G_verbose_message(_("\eading the input map -%s- and -%s- and creating some temporary files..."),
  127. backrow_layer, backcol_layer);
  128. /* Create segmented files for back cell and output layers */
  129. in_row_fd = creat(in_row_file, 0666);
  130. Segment_format(in_row_fd, nrows, ncols, srows, scols, len);
  131. close(in_row_fd);
  132. in_col_fd = creat(in_col_file, 0666);
  133. Segment_format(in_col_fd, nrows, ncols, srows, scols, len);
  134. close(in_col_fd);
  135. out_fd = creat(out_file, 0666);
  136. Segment_format(out_fd, nrows, ncols, srows, scols, len);
  137. close(out_fd);
  138. /* Open initialize and segment all files */
  139. in_row_fd = open(in_row_file, 2);
  140. Segment_init(&in_row_seg, in_row_fd, 4);
  141. in_col_fd = open(in_col_file, 2);
  142. Segment_init(&in_col_seg, in_col_fd, 4);
  143. out_fd = open(out_file, 2);
  144. Segment_init(&out_seg, out_fd, 4);
  145. /* Write the back cell layers in the segmented files, and
  146. * Change UTM coordinates to ROWs and COLUMNs */
  147. for (row = 0; row < nrows; row++) {
  148. Rast_get_c_row(backrow_fd, cell, row);
  149. for (col = 0; col < ncols; col++)
  150. if (cell[col] > 0)
  151. cell[col] =
  152. (window.north - cell[col]) / window.ns_res /* - 0.5 */ ;
  153. else
  154. cell[col] = -1;
  155. Segment_put_row(&in_row_seg, cell, row);
  156. Rast_get_c_row(backcol_fd, cell, row);
  157. for (col = 0; col < ncols; col++)
  158. if (cell[col] > 0)
  159. cell[col] =
  160. (cell[col] - window.west) / window.ew_res /* - 0.5 */ ;
  161. Segment_put_row(&in_col_seg, cell, row);
  162. }
  163. /* Convert easting and northing from the command line to row and col */
  164. if (opt3->answer) {
  165. for (n = 0; opt3->answers[n] != NULL; n += 2) {
  166. G_scan_easting(opt3->answers[n], &east, G_projection());
  167. G_scan_northing(opt3->answers[n + 1], &north, G_projection());
  168. row = (window.north - north) / window.ns_res;
  169. col = (east - window.west) / window.ew_res;
  170. /* ignore pt outside window */
  171. if (east < window.west || east > window.east ||
  172. north < window.south || north > window.north) {
  173. G_warning("Ignoring point outside window: ");
  174. G_warning(" %.4f,%.4f", east, north);
  175. continue;
  176. }
  177. value = (char *)&backrow;
  178. Segment_get(&in_row_seg, value, row, col);
  179. /* ignore pt in no-data area */
  180. if (backrow < 0) {
  181. G_warning("Ignoring point in NO-DATA area :");
  182. G_warning(" %.4f,%.4f", east, north);
  183. continue;
  184. }
  185. value = (char *)&backcol;
  186. Segment_get(&in_col_seg, value, row, col);
  187. insert(&PRESENT_PT, row, col, backrow, backcol);
  188. }
  189. }
  190. /* Set flag according to input */
  191. if (path_mapset != NULL) {
  192. if (head_start_pt == NULL)
  193. /*output layer exists and start pts are not given on cmd line */
  194. flag = 1;
  195. /* output layer exists and starting pts are given on cmd line */
  196. else
  197. flag = 2;
  198. }
  199. else
  200. flag = 3; /* output layer does not previously exist */
  201. /* If the output layer containing the starting positions */
  202. /* create a linked list of of them */
  203. if (flag == 1) {
  204. path_fd = Rast_open_old(path_layer, path_mapset);
  205. /* Search for the marked starting pts and make list */
  206. for (row = 0; row < nrows; row++) {
  207. Rast_get_c_row(path_fd, cell, row);
  208. for (col = 0; col < ncols; col++) {
  209. if (cell[col] > 0) {
  210. value = (char *)&backrow;
  211. Segment_get(&in_row_seg, value, row, col);
  212. /* ignore pt in no-data area */
  213. if (backrow < 0) {
  214. G_warning("Ignoring point in NO-DATA area:");
  215. G_warning(" %.4f,%.4f\n",
  216. window.west + window.ew_res * (col + 0.5),
  217. window.north - window.ns_res * (row + 0.5));
  218. continue;
  219. }
  220. value = (char *)&backcol;
  221. Segment_get(&in_col_seg, value, row, col);
  222. insert(&PRESENT_PT, row, col, backrow, backcol);
  223. }
  224. } /* loop over cols */
  225. } /* loop over rows */
  226. Rast_close(path_fd);
  227. }
  228. /* loop over the starting points to find the least cost paths */
  229. G_verbose_message(_("Finding the least cost paths ..."));
  230. PRES_PT = head_start_pt;
  231. while (PRES_PT != NULL) {
  232. path_finder(PRES_PT->row, PRES_PT->col, PRES_PT->backrow,
  233. PRES_PT->backcol);
  234. OLD_PT = PRES_PT;
  235. PRES_PT = NEXT_PT;
  236. G_free(OLD_PT);
  237. }
  238. /* Write pending updates by Segment_put() to outputmap */
  239. Segment_flush(&out_seg);
  240. G_verbose_message(_("Writing the output map -%s-..."), path_layer);
  241. path_fd = Rast_open_c_new(path_layer);
  242. for (row = 0; row < nrows; row++) {
  243. Segment_get_row(&out_seg, cell, row);
  244. Rast_put_row(path_fd, cell, CELL_TYPE);
  245. }
  246. Segment_release(&in_row_seg); /* release memory */
  247. Segment_release(&in_col_seg);
  248. Segment_release(&out_seg);
  249. close(in_row_fd); /* close all files */
  250. close(in_col_fd);
  251. close(out_fd);
  252. Rast_close(path_fd);
  253. Rast_close(backrow_fd);
  254. Rast_close(backcol_fd);
  255. unlink(in_row_file); /* remove submatrix files */
  256. unlink(in_col_file);
  257. unlink(out_file);
  258. exit(EXIT_SUCCESS);
  259. }