main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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, verbose = 1,
  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 Flag *flag1;
  71. struct GModule *module;
  72. G_gisinit(argv[0]);
  73. /* Set description */
  74. module = G_define_module();
  75. G_add_keyword(_("raster"));
  76. module->description =
  77. _("Recursively traces the least cost path backwards to "
  78. "cells from which the cumulative cost was determined.");
  79. opt1 = G_define_option();
  80. opt1->key = "x_input";
  81. opt1->type = TYPE_STRING;
  82. opt1->required = YES;
  83. opt1->gisprompt = "old,cell,raster";
  84. opt1->description =
  85. _("Name of raster map containing back-path easting information");
  86. opt2 = G_define_option();
  87. opt2->key = "y_input";
  88. opt2->type = TYPE_STRING;
  89. opt2->required = YES;
  90. opt2->gisprompt = "old,cell,raster";
  91. opt2->description =
  92. _("Name of raster map containing back-path northing information");
  93. opt3 = G_define_option();
  94. opt3->key = "coordinate";
  95. opt3->type = TYPE_STRING;
  96. opt3->multiple = YES;
  97. opt3->key_desc = "x,y";
  98. opt3->description =
  99. _("The map E and N grid coordinates of starting points");
  100. opt4 = G_define_option();
  101. opt4->key = "output";
  102. opt4->type = TYPE_STRING;
  103. opt4->required = YES;
  104. opt4->gisprompt = "new,cell,raster";
  105. opt4->description = _("Name of spread path raster map");
  106. flag1 = G_define_flag();
  107. flag1->key = 'v';
  108. flag1->description = _("Run verbosely");
  109. /* Do command line parsing */
  110. if (G_parser(argc, argv))
  111. exit(EXIT_FAILURE);
  112. current_mapset = G_mapset();
  113. in_row_file = G_tempfile();
  114. in_col_file = G_tempfile();
  115. out_file = G_tempfile();
  116. /* Get database window parameters */
  117. G_get_window(&window);
  118. verbose = flag1->answer;
  119. /* Check if backrow layer exists in data base */
  120. search_mapset = "";
  121. strcpy(backrow_layer, opt2->answer);
  122. strcpy(backcol_layer, opt1->answer);
  123. backrow_mapset = G_find_raster(backrow_layer, search_mapset);
  124. backcol_mapset = G_find_raster(backcol_layer, search_mapset);
  125. if (backrow_mapset == NULL)
  126. G_fatal_error("%s - not found", backrow_layer);
  127. if (backcol_mapset == NULL)
  128. G_fatal_error("%s - not found", backcol_layer);
  129. search_mapset = "";
  130. strcpy(path_layer, opt4->answer);
  131. path_mapset = G_find_raster(path_layer, search_mapset);
  132. /* find number of rows and cols in window */
  133. nrows = G_window_rows();
  134. ncols = G_window_cols();
  135. cell = Rast_allocate_c_buf();
  136. /* Open back cell layers for reading */
  137. backrow_fd = Rast_open_old(backrow_layer, backrow_mapset);
  138. if (backrow_fd < 0)
  139. G_fatal_error("%s - can't open raster map", backrow_layer);
  140. backcol_fd = Rast_open_old(backcol_layer, backcol_mapset);
  141. if (backcol_fd < 0)
  142. G_fatal_error("%s - can't open raster map", backcol_layer);
  143. /* Parameters for map submatrices */
  144. len = sizeof(CELL);
  145. srows = nrows / 4 + 1;
  146. scols = ncols / 4 + 1;
  147. if (verbose)
  148. G_message
  149. ("\nReading the input map -%s- and -%s- and creating some temporary files...",
  150. backrow_layer, backcol_layer);
  151. /* Create segmented files for back cell and output layers */
  152. in_row_fd = creat(in_row_file, 0666);
  153. segment_format(in_row_fd, nrows, ncols, srows, scols, len);
  154. close(in_row_fd);
  155. in_col_fd = creat(in_col_file, 0666);
  156. segment_format(in_col_fd, nrows, ncols, srows, scols, len);
  157. close(in_col_fd);
  158. out_fd = creat(out_file, 0666);
  159. segment_format(out_fd, nrows, ncols, srows, scols, len);
  160. close(out_fd);
  161. /* Open initialize and segment all files */
  162. in_row_fd = open(in_row_file, 2);
  163. segment_init(&in_row_seg, in_row_fd, 4);
  164. in_col_fd = open(in_col_file, 2);
  165. segment_init(&in_col_seg, in_col_fd, 4);
  166. out_fd = open(out_file, 2);
  167. segment_init(&out_seg, out_fd, 4);
  168. /* Write the back cell layers in the segmented files, and
  169. * Change UTM coordinates to ROWs and COLUMNs */
  170. for (row = 0; row < nrows; row++) {
  171. if (Rast_get_c_row(backrow_fd, cell, row) < 0)
  172. G_fatal_error("unable to get map row %d", row);
  173. for (col = 0; col < ncols; col++)
  174. if (cell[col] > 0)
  175. cell[col] =
  176. (window.north - cell[col]) / window.ns_res /* - 0.5 */ ;
  177. else
  178. cell[col] = -1;
  179. segment_put_row(&in_row_seg, cell, row);
  180. if (Rast_get_c_row(backcol_fd, cell, row) < 0)
  181. G_fatal_error("unable to get map row %d", row);
  182. for (col = 0; col < ncols; col++)
  183. if (cell[col] > 0)
  184. cell[col] =
  185. (cell[col] - window.west) / window.ew_res /* - 0.5 */ ;
  186. segment_put_row(&in_col_seg, cell, row);
  187. }
  188. /* Convert easting and northing from the command line to row and col */
  189. if (opt3->answer) {
  190. for (n = 0; opt3->answers[n] != NULL; n += 2) {
  191. G_scan_easting(opt3->answers[n], &east, G_projection());
  192. G_scan_northing(opt3->answers[n + 1], &north, G_projection());
  193. row = (window.north - north) / window.ns_res;
  194. col = (east - window.west) / window.ew_res;
  195. /* ignore pt outside window */
  196. if (east < window.west || east > window.east ||
  197. north < window.south || north > window.north) {
  198. G_warning("Ignoring point outside window: ");
  199. G_warning(" %.4f,%.4f", east, north);
  200. continue;
  201. }
  202. value = (char *)&backrow;
  203. segment_get(&in_row_seg, value, row, col);
  204. /* ignore pt in no-data area */
  205. if (backrow < 0) {
  206. G_warning("Ignoring point in NO-DATA area :");
  207. G_warning(" %.4f,%.4f", east, north);
  208. continue;
  209. }
  210. value = (char *)&backcol;
  211. segment_get(&in_col_seg, value, row, col);
  212. insert(&PRESENT_PT, row, col, backrow, backcol);
  213. }
  214. }
  215. /* Set flag according to input */
  216. if (path_mapset != NULL) {
  217. if (head_start_pt == NULL)
  218. /*output layer exists and start pts are not given on cmd line */
  219. flag = 1;
  220. /* output layer exists and starting pts are given on cmd line */
  221. else
  222. flag = 2;
  223. }
  224. else
  225. flag = 3; /* output layer does not previously exist */
  226. /* If the output layer containing the starting positions */
  227. /* create a linked list of of them */
  228. if (flag == 1) {
  229. path_fd = Rast_open_old(path_layer, path_mapset);
  230. if (path_fd < 0)
  231. G_fatal_error("%s -can't open raster map", path_layer);
  232. /* Search for the marked starting pts and make list */
  233. for (row = 0; row < nrows; row++) {
  234. if (Rast_get_c_row(path_fd, cell, row) < 0)
  235. G_fatal_error("unable to get map row %d", row);
  236. for (col = 0; col < ncols; col++) {
  237. if (cell[col] > 0) {
  238. value = (char *)&backrow;
  239. segment_get(&in_row_seg, value, row, col);
  240. /* ignore pt in no-data area */
  241. if (backrow < 0) {
  242. G_warning("Ignoring point in NO-DATA area:");
  243. G_warning(" %.4f,%.4f\n",
  244. window.west + window.ew_res * (col + 0.5),
  245. window.north - window.ns_res * (row + 0.5));
  246. continue;
  247. }
  248. value = (char *)&backcol;
  249. segment_get(&in_col_seg, value, row, col);
  250. insert(&PRESENT_PT, row, col, backrow, backcol);
  251. }
  252. } /* loop over cols */
  253. } /* loop over rows */
  254. Rast_close(path_fd);
  255. }
  256. /* loop over the starting points to find the least cost paths */
  257. if (verbose)
  258. G_message("\nFinding the least cost paths ...");
  259. PRES_PT = head_start_pt;
  260. while (PRES_PT != NULL) {
  261. path_finder(PRES_PT->row, PRES_PT->col, PRES_PT->backrow,
  262. PRES_PT->backcol);
  263. OLD_PT = PRES_PT;
  264. PRES_PT = NEXT_PT;
  265. G_free(OLD_PT);
  266. }
  267. /* Write pending updates by segment_put() to outputmap */
  268. segment_flush(&out_seg);
  269. if (verbose)
  270. G_message("\nWriting the output map -%s-...", path_layer);
  271. path_fd = Rast_open_c_new(path_layer);
  272. for (row = 0; row < nrows; row++) {
  273. segment_get_row(&out_seg, cell, row);
  274. if (Rast_put_row(path_fd, cell, CELL_TYPE) < 0)
  275. G_fatal_error("unable to write map row %d", row);
  276. }
  277. if (verbose)
  278. G_message("finished.");
  279. segment_release(&in_row_seg); /* release memory */
  280. segment_release(&in_col_seg);
  281. segment_release(&out_seg);
  282. close(in_row_fd); /* close all files */
  283. close(in_col_fd);
  284. close(out_fd);
  285. Rast_close(path_fd);
  286. Rast_close(backrow_fd);
  287. Rast_close(backcol_fd);
  288. unlink(in_row_file); /* remove submatrix files */
  289. unlink(in_col_file);
  290. unlink(out_file);
  291. exit(EXIT_SUCCESS);
  292. }