main.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /****************************************************************************
  2. *
  3. * MODULE: r.walk
  4. * AUTHOR(S): Based on r.cost written by :
  5. * Antony Awaida,
  6. * Intelligent Engineering
  7. * Systems Laboratory,
  8. * M.I.T.
  9. * James Westervelt,
  10. * U.S.Army Construction Engineering Research Laboratory
  11. *
  12. * Updated for Grass 5
  13. * Pierre de Mouveaux (pmx@audiovu.com)
  14. *
  15. * Initial version of r.walk:
  16. * Steno Fontanari, 2002, ITC-irst
  17. *
  18. * GRASS 6.0 version of r.walk:
  19. * Franceschetti Simone, Sorrentino Diego, Mussi Fabiano and Pasolli Mattia
  20. * Correction by: Fontanari Steno, Napolitano Maurizio and Flor Roberto
  21. * In collaboration with: Franchi Matteo, Vaglia Beatrice, Bartucca Luisa,
  22. * Fava Valentina and Tolotti Mathias, 2004
  23. *
  24. * Updated for GRASS 6.1
  25. * Roberto Flor and Markus Neteler
  26. * Glynn Clements <glynn gclements.plus.com>, Soeren Gebbert <soeren.gebbert gmx.de>
  27. * Updated for calculation errors and directional surface generation
  28. * Colin Nielsen <colin.nielsen gmail com>
  29. * Updated for GRASS 7
  30. * Markus Metz
  31. * PURPOSE: anisotropic movements on cost surfaces
  32. * COPYRIGHT: (C) 1999-2014 by the GRASS Development Team
  33. *
  34. * This program is free software under the GNU General Public
  35. * License (>=v2). Read the file COPYING that comes with GRASS
  36. * for details.
  37. *
  38. *****************************************************************************/
  39. /*********************************************************************
  40. *
  41. * This is the main program for the minimum path cost analysis.
  42. * It generates a cumulative cost map (output) from an elevation (inputdtm
  43. * and cost map (inputcost) with respect to starting locations (coor).
  44. *
  45. * It takes as input the following:
  46. * 1) Cost of traversing each grid cell as given by an elevation map and
  47. * a cost map cell (inputcost).
  48. * 2) If starting points are not specified on the command line
  49. * then the output map must exist and contain the starting locations
  50. *
  51. * Otherwise the output map need not exist and the coor points
  52. * from the command line are used.
  53. *
  54. *********************************************************************/
  55. /*********************************************************************
  56. * The walking energy is computed for the human walk, based on Aitken,
  57. * 1977, Langmuir, 1984:
  58. *
  59. * {T= [(a)x(Delta S)] + [(b)x(Delta H Climb)]
  60. * +[(c)*(Delta H moderate downhill)]+[(d)*(Delta H steep downhill]}
  61. *
  62. * where T is time in seconds, Delta S distance in meter, Delta H the heigth difference
  63. *
  64. * The default a,b,c,d parameters used below have been measured using oxygen consumption in biomechanical
  65. * experiments.
  66. * Refs:
  67. * * Aitken, R. 1977. Wilderness areas in Scotland. Unpublished Ph.D. thesis. University of Aberdeen.
  68. * * Steno Fontanari, University of Trento, Italy, Ingegneria per l'Ambiente e
  69. * il Territorio, 2000-2001. Svilluppo di metodologie GIS per la determinazione dell'accessibilita'
  70. * territoriale come supporto alle decisioni nella gestione ambientale.
  71. * * Langmuir, E. 1984. Mountaincraft and leadership. The Scottish Sports Council/MLTB. Cordee,
  72. * Leicester.
  73. *
  74. * The total cost is computed as a linear combination of walking energy and a given friction cost map:
  75. *
  76. * TOTAL COST = [(WALKING ENERGY ) + (LAMBDA*FRICTION)]
  77. *
  78. * TODO: generalize formula to other species
  79. *************/
  80. /*
  81. *
  82. * 20 july 2004 - Pierre de Mouveaux. pmx@audiovu.com
  83. * Updated to use the Grass 5.0 floating point raster cell format.
  84. * Convert floats to double. Done ;)
  85. * 2001: original r.walk by Steno Fontanari, ITC-irst
  86. * 24 July 2004: WebValley 2004, fixed and enhanced by
  87. * Matteo Franchi Liceo Leonardo Da Vinci Trento
  88. * Roberto Flor ITC-irst
  89. * 7 December 2005: Grass 6.1 cleanup
  90. * Roberto Flor ITC-irst
  91. * Markus Neteler CEA
  92. */
  93. #include <stdlib.h>
  94. #include <unistd.h>
  95. #include <string.h>
  96. #include <math.h>
  97. #include <sys/types.h>
  98. #include <sys/stat.h>
  99. #include <fcntl.h>
  100. #include <grass/gis.h>
  101. #include <grass/raster.h>
  102. #include <grass/vector.h>
  103. #include <grass/segment.h>
  104. #include <grass/glocale.h>
  105. #include "cost.h"
  106. #include "stash.h"
  107. #define SEGCOLSIZE 64
  108. struct Cell_head window;
  109. struct start_pt *head_start_pt = NULL;
  110. struct start_pt *head_end_pt = NULL;
  111. int main(int argc, char *argv[])
  112. {
  113. const char *cum_cost_layer, *move_dir_layer;
  114. const char *cost_layer, *dtm_layer;
  115. const char *dtm_mapset, *cost_mapset, *search_mapset;
  116. void *dtm_cell, *cost_cell, *cum_cell, *dir_cell, *cell2 = NULL;
  117. SEGMENT cost_seg, dir_seg;
  118. double *value;
  119. char buf[400];
  120. extern struct Cell_head window;
  121. double NS_fac, EW_fac, DIAG_fac, H_DIAG_fac, V_DIAG_fac;
  122. double fcost_dtm, fcost_cost;
  123. double min_cost, old_min_cost;
  124. FCELL cur_dir;
  125. double zero = 0.0;
  126. int col = 0, row = 0, nrows = 0, ncols = 0;
  127. int maxcost, par_number;
  128. int nseg;
  129. int maxmem;
  130. int segments_in_memory;
  131. int cost_fd, cum_fd, dtm_fd, dir_fd;
  132. int have_stop_points = 0, dir = 0;
  133. double my_dtm, my_cost, check_dtm;
  134. double null_cost, dnullval;
  135. double a, b, c, d, lambda, slope_factor;
  136. int srows, scols;
  137. int total_reviewed;
  138. int keep_nulls = 1;
  139. int start_with_raster_vals = 1;
  140. int neighbor;
  141. long n_processed = 0;
  142. long total_cells;
  143. struct GModule *module;
  144. struct Flag *flag2, *flag3, *flag4, *flag5;
  145. struct Option *opt1, *opt2, *opt3, *opt4, *opt5, *opt6, *opt7, *opt8;
  146. struct Option *opt9, *opt10, *opt11, *opt12, *opt13, *opt14, *opt15;
  147. struct cost *pres_cell;
  148. struct start_pt *pres_start_pt = NULL;
  149. struct start_pt *pres_stop_pt = NULL;
  150. struct cc {
  151. double dtm; /* elevation model */
  152. double cost_in; /* friction costs */
  153. double cost_out; /* cumulative costs */
  154. } costs;
  155. void *ptr1, *ptr2;
  156. RASTER_MAP_TYPE dtm_data_type, cost_data_type, cum_data_type =
  157. DCELL_TYPE, dir_data_type = FCELL_TYPE;
  158. struct History history;
  159. double peak = 0.0;
  160. int dtm_dsize, cost_dsize;
  161. double disk_mb, mem_mb;
  162. /* Definition for dimension and region check */
  163. struct Cell_head dtm_cellhd, cost_cellhd;
  164. G_gisinit(argv[0]);
  165. module = G_define_module();
  166. G_add_keyword(_("raster"));
  167. G_add_keyword(_("cost surface"));
  168. G_add_keyword(_("cumulative costs"));
  169. G_add_keyword(_("cost allocation"));
  170. module->description =
  171. _("Creates a raster map showing the "
  172. "anisotropic cumulative cost of moving between different "
  173. "geographic locations on an input raster map "
  174. "whose cell category values represent cost.");
  175. opt12 = G_define_standard_option(G_OPT_R_ELEV);
  176. opt2 = G_define_standard_option(G_OPT_R_INPUT);
  177. opt2->key = "friction";
  178. opt2->description =
  179. _("Name of input raster map containing friction costs");
  180. opt1 = G_define_standard_option(G_OPT_R_OUTPUT);
  181. opt1->description = _("Name for output raster map to contain walking costs");
  182. opt11 = G_define_standard_option(G_OPT_R_OUTPUT);
  183. opt11->key = "outdir";
  184. opt11->required = NO;
  185. opt11->description =
  186. _("Name for output raster map to contain movement directions");
  187. opt11->guisection = _("Optional outputs");
  188. opt7 = G_define_standard_option(G_OPT_V_INPUT);
  189. opt7->key = "start_points";
  190. opt7->required = NO;
  191. opt7->label = _("Name of starting vector points map");
  192. opt7->guisection = _("Start");
  193. opt8 = G_define_standard_option(G_OPT_V_INPUT);
  194. opt8->key = "stop_points";
  195. opt8->required = NO;
  196. opt8->label = _("Name of stopping vector points map");
  197. opt8->guisection = _("Stop");
  198. opt9 = G_define_standard_option(G_OPT_R_INPUT);
  199. opt9->key = "start_raster";
  200. opt9->required = NO;
  201. opt9->description = _("Name of starting raster points map");
  202. opt9->guisection = _("Start");
  203. opt3 = G_define_standard_option(G_OPT_M_COORDS);
  204. opt3->key = "start_coordinates";
  205. opt3->multiple = YES;
  206. opt3->description =
  207. _("Coordinates of starting point(s) (E,N)");
  208. opt3->guisection = _("Start");
  209. opt4 = G_define_standard_option(G_OPT_M_COORDS);
  210. opt4->key = "stop_coordinates";
  211. opt4->multiple = YES;
  212. opt4->description =
  213. _("Coordinates of stopping point(s) (E,N)");
  214. opt4->guisection = _("Stop");
  215. opt5 = G_define_option();
  216. opt5->key = "max_cost";
  217. opt5->type = TYPE_INTEGER;
  218. opt5->key_desc = "value";
  219. opt5->required = NO;
  220. opt5->multiple = NO;
  221. opt5->answer = "0";
  222. opt5->description = _("Maximum cumulative cost");
  223. opt6 = G_define_option();
  224. opt6->key = "null_cost";
  225. opt6->type = TYPE_DOUBLE;
  226. opt6->key_desc = "value";
  227. opt6->required = NO;
  228. opt6->multiple = NO;
  229. opt6->description =
  230. _("Cost assigned to null cells. By default, null cells are excluded");
  231. opt6->guisection = _("NULL cells");
  232. opt10 = G_define_option();
  233. opt10->key = "percent_memory";
  234. opt10->type = TYPE_INTEGER;
  235. opt10->key_desc = "value";
  236. opt10->required = NO;
  237. opt10->multiple = NO;
  238. opt10->answer = "40";
  239. opt10->options = "0-100";
  240. opt10->description = _("Percent of map to keep in memory");
  241. opt15 = G_define_option();
  242. opt15->key = "walk_coeff";
  243. opt15->type = TYPE_STRING;
  244. opt15->key_desc = "a,b,c,d";
  245. opt15->required = NO;
  246. opt15->multiple = NO;
  247. opt15->answer = "0.72,6.0,1.9998,-1.9998";
  248. opt15->description =
  249. _("Coefficients for walking energy formula parameters a,b,c,d");
  250. opt15->guisection = _("Settings");
  251. opt14 = G_define_option();
  252. opt14->key = "lambda";
  253. opt14->type = TYPE_DOUBLE;
  254. opt14->required = NO;
  255. opt14->multiple = NO;
  256. opt14->answer = "1.0";
  257. opt14->description =
  258. _("Lambda coefficients for combining walking energy and friction cost");
  259. opt14->guisection = _("Settings");
  260. opt13 = G_define_option();
  261. opt13->key = "slope_factor";
  262. opt13->type = TYPE_DOUBLE;
  263. opt13->required = NO;
  264. opt13->multiple = NO;
  265. opt13->answer = "-0.2125";
  266. opt13->description =
  267. _("Slope factor determines travel energy cost per height step");
  268. opt13->guisection = _("Settings");
  269. flag2 = G_define_flag();
  270. flag2->key = 'k';
  271. flag2->description =
  272. _("Use the 'Knight's move'; slower, but more accurate");
  273. flag3 = G_define_flag();
  274. flag3->key = 'n';
  275. flag3->description = _("Keep null values in output raster map");
  276. flag3->guisection = _("NULL cells");
  277. flag4 = G_define_flag();
  278. flag4->key = 'r';
  279. flag4->description = _("Start with values in raster map");
  280. flag4->guisection = _("Start");
  281. flag5 = G_define_flag();
  282. flag5->key = 'i';
  283. flag5->description = _("Print info about disk space and memory requirements and exit");
  284. /* Parse options */
  285. if (G_parser(argc, argv))
  286. exit(EXIT_FAILURE);
  287. /* If no outdir is specified, set flag to skip all dir */
  288. if (opt11->answer != NULL)
  289. dir = 1;
  290. /* Get database window parameters */
  291. Rast_get_window(&window);
  292. /* Find north-south, east_west and diagonal factors */
  293. EW_fac = window.ew_res; /* Must be the physical distance */
  294. NS_fac = window.ns_res;
  295. DIAG_fac = (double)sqrt((double)(NS_fac * NS_fac + EW_fac * EW_fac));
  296. V_DIAG_fac =
  297. (double)sqrt((double)(4 * NS_fac * NS_fac + EW_fac * EW_fac));
  298. H_DIAG_fac =
  299. (double)sqrt((double)(NS_fac * NS_fac + 4 * EW_fac * EW_fac));
  300. Rast_set_d_null_value(&null_cost, 1);
  301. if (flag2->answer)
  302. total_reviewed = 16;
  303. else
  304. total_reviewed = 8;
  305. keep_nulls = flag3->answer;
  306. start_with_raster_vals = flag4->answer;
  307. {
  308. int count = 0;
  309. if (opt3->answers)
  310. count++;
  311. if (opt7->answers)
  312. count++;
  313. if (opt9->answers)
  314. count++;
  315. if (count != 1)
  316. G_fatal_error(_("Must specify exactly one of start_points, start_rast or coordinate"));
  317. }
  318. if (opt3->answers)
  319. if (!process_answers(opt3->answers, &head_start_pt, &pres_start_pt))
  320. G_fatal_error(_("No start points"));
  321. if (opt4->answers)
  322. have_stop_points =
  323. process_answers(opt4->answers, &head_end_pt, &pres_stop_pt);
  324. if (sscanf(opt5->answer, "%d", &maxcost) != 1 || maxcost < 0)
  325. G_fatal_error(_("Inappropriate maximum cost: %d"), maxcost);
  326. if (sscanf(opt10->answer, "%d", &maxmem) != 1 || maxmem < 0 ||
  327. maxmem > 100)
  328. G_fatal_error(_("Inappropriate percent memory: %d"), maxmem);
  329. /* Getting walking energy formula parameters */
  330. if ((par_number =
  331. sscanf(opt15->answer, "%lf,%lf,%lf,%lf", &a, &b, &c, &d)) != 4)
  332. G_fatal_error(_("Missing required value: got %d instead of 4"),
  333. par_number);
  334. else {
  335. G_message(_("Walking costs are a=%g b=%g c=%g d=%g"), a, b, c, d);
  336. }
  337. /* Getting lambda */
  338. if ((par_number = sscanf(opt14->answer, "%lf", &lambda)) != 1)
  339. G_fatal_error(_("Missing required value: %d"), par_number);
  340. else {
  341. G_message(_("Lambda is %g"), lambda);
  342. }
  343. /* Getting slope_factor */
  344. if ((par_number = sscanf(opt13->answer, "%lf", &slope_factor)) != 1)
  345. G_fatal_error(_("Missing required value: %d"), par_number);
  346. else {
  347. G_message(_("Slope_factor is %g"), slope_factor);
  348. }
  349. if ((opt6->answer == NULL) ||
  350. (sscanf(opt6->answer, "%lf", &null_cost) != 1)) {
  351. G_debug(1, "Null cells excluded from cost evaluation");
  352. Rast_set_d_null_value(&null_cost, 1);
  353. }
  354. else if (keep_nulls)
  355. G_debug(1,"Input null cell will be retained into output map");
  356. if (opt7->answer) {
  357. search_mapset = G_find_vector2(opt7->answer, "");
  358. if (search_mapset == NULL)
  359. G_fatal_error(_("Vector map <%s> not found"), opt7->answer);
  360. }
  361. if (!Rast_is_d_null_value(&null_cost)) {
  362. if (null_cost < 0.0) {
  363. G_warning(_("Assigning negative cost to null cell. Null cells excluded."));
  364. Rast_set_d_null_value(&null_cost, 1);
  365. }
  366. }
  367. else {
  368. keep_nulls = 0; /* handled automagically... */
  369. }
  370. dtm_layer = opt12->answer;
  371. cost_layer = opt2->answer;
  372. cum_cost_layer = opt1->answer;
  373. move_dir_layer = opt11->answer;
  374. /* Find number of rows and columns in window */
  375. nrows = Rast_window_rows();
  376. ncols = Rast_window_cols();
  377. /* Open cost cell layer for reading */
  378. dtm_mapset = G_find_raster2(dtm_layer, "");
  379. if (dtm_mapset == NULL)
  380. G_fatal_error(_("Raster map <%s> not found"), dtm_layer);
  381. dtm_fd = Rast_open_old(dtm_layer, "");
  382. cost_mapset = G_find_raster2(cost_layer, "");
  383. if (cost_mapset == NULL)
  384. G_fatal_error(_("Raster map <%s> not found"), cost_layer);
  385. cost_fd = Rast_open_old(cost_layer, cost_mapset);
  386. Rast_get_cellhd(dtm_layer, "", &dtm_cellhd);
  387. Rast_get_cellhd(cost_layer, "", &cost_cellhd);
  388. dtm_data_type = Rast_get_map_type(dtm_fd);
  389. cost_data_type = Rast_get_map_type(cost_fd);
  390. /* Parameters for map submatrices */
  391. switch (dtm_data_type) {
  392. case (CELL_TYPE):
  393. G_debug(1, "DTM_Source map is: Integer cell type");
  394. break;
  395. case (FCELL_TYPE):
  396. G_debug(1, "DTM_Source map is: Floating point (float) cell type");
  397. break;
  398. case (DCELL_TYPE):
  399. G_debug(1, "DTM_Source map is: Floating point (double) cell type");
  400. break;
  401. }
  402. G_debug(1, "DTM %d rows, %d cols", dtm_cellhd.rows, dtm_cellhd.cols);
  403. switch (cost_data_type) {
  404. case (CELL_TYPE):
  405. G_debug(1, "COST_Source map is: Integer cell type");
  406. break;
  407. case (FCELL_TYPE):
  408. G_debug(1, "COST_Source map is: Floating point (float) cell type");
  409. break;
  410. case (DCELL_TYPE):
  411. G_debug(1, "COST_Source map is: Floating point (double) cell type");
  412. break;
  413. }
  414. G_debug(1, "COST %d rows, %d cols", cost_cellhd.rows, cost_cellhd.cols);
  415. G_debug(1, " %d rows, %d cols", nrows, ncols);
  416. G_format_resolution(window.ew_res, buf, window.proj);
  417. G_debug(1, " EW resolution %s (%g)", buf, window.ew_res);
  418. G_format_resolution(window.ns_res, buf, window.proj);
  419. G_debug(1, " NS resolution %s (%g)", buf, window.ns_res);
  420. /* this is most probably the limitation of r.walk for large datasets
  421. * segment size needs to be reduced to avoid unecessary disk IO
  422. * but it doesn't make sense to go down to 1
  423. * so use 64 segment rows and cols for <= 200 million cells
  424. * for larger regions, 32 segment rows and cols
  425. * maybe go down to 16 for > 500 million cells ? */
  426. if ((double) nrows * ncols > 200000000)
  427. srows = scols = SEGCOLSIZE / 2;
  428. else
  429. srows = scols = SEGCOLSIZE;
  430. if (maxmem == 100) {
  431. srows = scols = 256;
  432. }
  433. /* calculate total number of segments */
  434. nseg = ((nrows + srows - 1) / srows) * ((ncols + scols - 1) / scols);
  435. if (maxmem > 0)
  436. segments_in_memory = (maxmem * nseg) / 100;
  437. /* maxmem = 0 */
  438. else
  439. segments_in_memory = 4 * (nrows / srows + ncols / scols + 2);
  440. if (segments_in_memory == 0)
  441. segments_in_memory = 1;
  442. /* report disk space and memory requirements */
  443. if (dir == 1) {
  444. disk_mb = (double) nrows * ncols * 28. / 1048576.;
  445. mem_mb = (double) srows * scols * 28. / 1048576. * segments_in_memory;
  446. mem_mb += nrows * ncols * 0.05 * 20. / 1048576.; /* for Dijkstra search */
  447. }
  448. else {
  449. disk_mb = (double) nrows * ncols * 24. / 1048576.;
  450. mem_mb = (double) srows * scols * 24. / 1048576. * segments_in_memory;
  451. mem_mb += nrows * ncols * 0.05 * 20. / 1048576.; /* for Dijkstra search */
  452. }
  453. if (flag5->answer) {
  454. fprintf(stdout, _("Will need at least %.2f MB of disk space"), disk_mb);
  455. fprintf(stdout, "\n");
  456. fprintf(stdout, _("Will need at least %.2f MB of memory"), mem_mb);
  457. fprintf(stdout, "\n");
  458. Rast_close(cost_fd);
  459. Rast_close(dtm_fd);
  460. exit(EXIT_SUCCESS);
  461. }
  462. G_verbose_message("--------------------------------------------");
  463. G_verbose_message(_("Will need at least %.2f MB of disk space"), disk_mb);
  464. G_verbose_message(_("Will need at least %.2f MB of memory"), mem_mb);
  465. G_verbose_message("--------------------------------------------");
  466. /* Create segmented format files for cost layer and output layer */
  467. G_verbose_message(_("Creating some temporary files..."));
  468. if (Segment_open(&cost_seg, G_tempfile(), nrows, ncols, srows, scols,
  469. sizeof(struct cc), segments_in_memory) != 1)
  470. G_fatal_error(_("Can not create temporary file"));
  471. if (dir == 1) {
  472. if (Segment_open(&dir_seg, G_tempfile(), nrows, ncols, srows, scols,
  473. sizeof(FCELL), segments_in_memory) != 1)
  474. G_fatal_error(_("Can not create temporary file"));
  475. }
  476. /* Write the dtm and cost layers in the segmented file */
  477. G_message(_("Reading raster maps <%s> and <%s>, initializing output..."),
  478. G_fully_qualified_name(dtm_layer, dtm_mapset),
  479. G_fully_qualified_name(cost_layer, cost_mapset));
  480. /* read required maps cost and dtm */
  481. {
  482. int skip_nulls;
  483. double p_dtm, p_cost;
  484. Rast_set_d_null_value(&dnullval, 1);
  485. costs.cost_out = dnullval;
  486. total_cells = nrows * ncols;
  487. skip_nulls = Rast_is_d_null_value(&null_cost);
  488. dtm_dsize = Rast_cell_size(dtm_data_type);
  489. cost_dsize = Rast_cell_size(cost_data_type);
  490. dtm_cell = Rast_allocate_buf(dtm_data_type);
  491. cost_cell = Rast_allocate_buf(cost_data_type);
  492. p_dtm = 0.0;
  493. p_cost = 0.0;
  494. for (row = 0; row < nrows; row++) {
  495. G_percent(row, nrows, 2);
  496. Rast_get_row(dtm_fd, dtm_cell, row, dtm_data_type);
  497. Rast_get_row(cost_fd, cost_cell, row, cost_data_type);
  498. /* INPUT NULL VALUES: ??? */
  499. ptr1 = cost_cell;
  500. ptr2 = dtm_cell;
  501. for (col = 0; col < ncols; col++) {
  502. if (Rast_is_null_value(ptr1, cost_data_type)) {
  503. p_cost = null_cost;
  504. if (skip_nulls) {
  505. total_cells--;
  506. }
  507. }
  508. else {
  509. switch (cost_data_type) {
  510. case CELL_TYPE:
  511. p_cost = *(CELL *)ptr1;
  512. break;
  513. case FCELL_TYPE:
  514. p_cost = *(FCELL *)ptr1;
  515. break;
  516. case DCELL_TYPE:
  517. p_cost = *(DCELL *)ptr1;
  518. break;
  519. }
  520. }
  521. costs.cost_in = p_cost;
  522. if (Rast_is_null_value(ptr2, dtm_data_type)) {
  523. p_dtm = null_cost;
  524. if (skip_nulls && !Rast_is_null_value(ptr1, cost_data_type)) {
  525. total_cells--;
  526. }
  527. }
  528. else {
  529. switch (dtm_data_type) {
  530. case CELL_TYPE:
  531. p_dtm = *(CELL *)ptr2;
  532. break;
  533. case FCELL_TYPE:
  534. p_dtm = *(FCELL *)ptr2;
  535. break;
  536. case DCELL_TYPE:
  537. p_dtm = *(DCELL *)ptr2;
  538. break;
  539. }
  540. }
  541. costs.dtm = p_dtm;
  542. Segment_put(&cost_seg, &costs, row, col);
  543. ptr1 = G_incr_void_ptr(ptr1, cost_dsize);
  544. ptr2 = G_incr_void_ptr(ptr2, dtm_dsize);
  545. }
  546. }
  547. G_free(dtm_cell);
  548. G_free(cost_cell);
  549. G_percent(1, 1, 1);
  550. }
  551. if (dir == 1) {
  552. G_message(_("Initializing directional output..."));
  553. for (row = 0; row < nrows; row++) {
  554. G_percent(row, nrows, 2);
  555. for (col = 0; col < ncols; col++) {
  556. Segment_put(&dir_seg, &dnullval, row, col);
  557. }
  558. }
  559. G_percent(1, 1, 1);
  560. }
  561. /* Scan the existing cum_cost_layer searching for starting points.
  562. * Create a heap of starting points ordered by increasing costs.
  563. */
  564. init_heap();
  565. /* read vector with start points */
  566. if (opt7->answer) {
  567. struct Map_info In;
  568. struct line_pnts *Points;
  569. struct line_cats *Cats;
  570. struct bound_box box;
  571. struct start_pt *new_start_pt;
  572. int type, got_one = 0;
  573. Points = Vect_new_line_struct();
  574. Cats = Vect_new_cats_struct();
  575. Vect_set_open_level(1); /* topology not required */
  576. if (1 > Vect_open_old(&In, opt7->answer, ""))
  577. G_fatal_error(_("Unable to open vector map <%s>"), opt7->answer);
  578. G_message(_("Reading vector map <%s> with start points..."),
  579. Vect_get_full_name(&In));
  580. Vect_rewind(&In);
  581. Vect_region_box(&window, &box);
  582. while (1) {
  583. /* register line */
  584. type = Vect_read_next_line(&In, Points, Cats);
  585. /* Note: check for dead lines is not needed, because they are skipped by V1_read_next_line_nat() */
  586. if (type == -1) {
  587. G_warning(_("Unable to read vector map"));
  588. continue;
  589. }
  590. else if (type == -2) {
  591. break;
  592. }
  593. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0, &box))
  594. continue;
  595. got_one = 1;
  596. col = (int)Rast_easting_to_col(Points->x[0], &window);
  597. row = (int)Rast_northing_to_row(Points->y[0], &window);
  598. new_start_pt =
  599. (struct start_pt *)(G_malloc(sizeof(struct start_pt)));
  600. new_start_pt->row = row;
  601. new_start_pt->col = col;
  602. new_start_pt->next = NULL;
  603. if (head_start_pt == NULL) {
  604. head_start_pt = new_start_pt;
  605. pres_start_pt = new_start_pt;
  606. new_start_pt->next = NULL;
  607. }
  608. else {
  609. pres_start_pt->next = new_start_pt;
  610. pres_start_pt = new_start_pt;
  611. }
  612. }
  613. Vect_close(&In);
  614. if (!got_one)
  615. G_fatal_error(_("No start points found in vector <%s>"), opt7->answer);
  616. }
  617. /* read vector with stop points */
  618. if (opt8->answer) {
  619. struct Map_info In;
  620. struct line_pnts *Points;
  621. struct line_cats *Cats;
  622. struct bound_box box;
  623. struct start_pt *new_start_pt;
  624. int type;
  625. G_message(_("Reading vector map <%s> with stop points..."), opt8->answer);
  626. Points = Vect_new_line_struct();
  627. Cats = Vect_new_cats_struct();
  628. Vect_set_open_level(1); /* topology not required */
  629. if (1 > Vect_open_old(&In, opt8->answer, ""))
  630. G_fatal_error(_("Unable to open vector map <%s>"), opt8->answer);
  631. Vect_rewind(&In);
  632. Vect_region_box(&window, &box);
  633. while (1) {
  634. /* register line */
  635. type = Vect_read_next_line(&In, Points, Cats);
  636. /* Note: check for dead lines is not needed, because they are skipped by V1_read_next_line_nat() */
  637. if (type == -1) {
  638. G_warning(_("Unable to read vector map"));
  639. continue;
  640. }
  641. else if (type == -2) {
  642. break;
  643. }
  644. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0, &box))
  645. continue;
  646. have_stop_points = 1;
  647. col = (int)Rast_easting_to_col(Points->x[0], &window);
  648. row = (int)Rast_northing_to_row(Points->y[0], &window);
  649. new_start_pt =
  650. (struct start_pt *)(G_malloc(sizeof(struct start_pt)));
  651. new_start_pt->row = row;
  652. new_start_pt->col = col;
  653. new_start_pt->next = NULL;
  654. if (head_end_pt == NULL) {
  655. head_end_pt = new_start_pt;
  656. pres_stop_pt = new_start_pt;
  657. new_start_pt->next = NULL;
  658. }
  659. else {
  660. pres_stop_pt->next = new_start_pt;
  661. pres_stop_pt = new_start_pt;
  662. }
  663. }
  664. Vect_close(&In);
  665. if (!have_stop_points)
  666. G_fatal_error(_("No stop points found in vector <%s>"), opt8->answer);
  667. }
  668. /* read raster with start points */
  669. if (opt9->answer) {
  670. int dsize2;
  671. int fd;
  672. RASTER_MAP_TYPE data_type2;
  673. int got_one = 0;
  674. search_mapset = G_find_raster(opt9->answer, "");
  675. if (search_mapset == NULL)
  676. G_fatal_error(_("Raster map <%s> not found"), opt9->answer);
  677. fd = Rast_open_old(opt9->answer, "");
  678. data_type2 = Rast_get_map_type(fd);
  679. dsize2 = Rast_cell_size(data_type2);
  680. cell2 = Rast_allocate_buf(data_type2);
  681. if (!cell2)
  682. G_fatal_error(_("Unable to allocate memory"));
  683. G_message(_("Reading raster map <%s> with start points..."), opt9->answer);
  684. for (row = 0; row < nrows; row++) {
  685. G_percent(row, nrows, 2);
  686. Rast_get_row(fd, cell2, row, data_type2);
  687. ptr2 = cell2;
  688. for (col = 0; col < ncols; col++) {
  689. /* Did I understand that concept of cummulative cost map? - (pmx) 12 april 2000 */
  690. if (!Rast_is_null_value(ptr2, data_type2)) {
  691. double cellval;
  692. Segment_get(&cost_seg, &costs, row, col);
  693. if (start_with_raster_vals == 1) {
  694. cellval = Rast_get_d_value(ptr2, data_type2);
  695. insert(cellval, row, col);
  696. costs.cost_out = cellval;
  697. Segment_put(&cost_seg, &costs, row, col);
  698. }
  699. else {
  700. value = &zero;
  701. insert(zero, row, col);
  702. costs.cost_out = *value;
  703. Segment_put(&cost_seg, &costs, row, col);
  704. }
  705. got_one = 1;
  706. }
  707. ptr2 = G_incr_void_ptr(ptr2, dsize2);
  708. }
  709. }
  710. G_percent(1, 1, 1);
  711. Rast_close(fd);
  712. G_free(cell2);
  713. if (!got_one)
  714. G_fatal_error(_("No start points"));
  715. }
  716. /* If the starting points are given on the command line start a linked
  717. * list of cells ordered by increasing costs
  718. */
  719. if (head_start_pt) {
  720. struct start_pt *top_start_pt = NULL;
  721. top_start_pt = head_start_pt;
  722. while (top_start_pt != NULL) {
  723. value = &zero;
  724. if (top_start_pt->row < 0 || top_start_pt->row >= nrows
  725. || top_start_pt->col < 0 || top_start_pt->col >= ncols)
  726. G_fatal_error(_("Specified starting location outside database window"));
  727. insert(zero, top_start_pt->row, top_start_pt->col);
  728. Segment_get(&cost_seg, &costs, top_start_pt->row,
  729. top_start_pt->col);
  730. costs.cost_out = *value;
  731. Segment_put(&cost_seg, &costs, top_start_pt->row,
  732. top_start_pt->col);
  733. top_start_pt = top_start_pt->next;
  734. }
  735. }
  736. /* Loop through the heap and perform at each cell the following:
  737. * 1) If an adjacent cell has not already been assigned a value compute
  738. * the min cost and assign it.
  739. * 2) Insert the adjacent cell in the heap.
  740. * 3) Free the memory allocated to the present cell.
  741. */
  742. G_debug(1, "total cells: %ld", total_cells);
  743. G_debug(1, "nrows x ncols: %d", nrows * ncols);
  744. G_message(_("Finding cost path..."));
  745. n_processed = 0;
  746. pres_cell = get_lowest();
  747. while (pres_cell != NULL) {
  748. struct cost *ct;
  749. double N_dtm, NE_dtm, E_dtm, SE_dtm, S_dtm, SW_dtm, W_dtm, NW_dtm;
  750. double NNE_dtm, ENE_dtm, ESE_dtm, SSE_dtm, SSW_dtm, WSW_dtm, WNW_dtm,
  751. NNW_dtm;
  752. double N_cost, NE_cost, E_cost, SE_cost, S_cost, SW_cost, W_cost,
  753. NW_cost;
  754. double NNE_cost, ENE_cost, ESE_cost, SSE_cost, SSW_cost, WSW_cost,
  755. WNW_cost, NNW_cost;
  756. N_dtm = NE_dtm = E_dtm = SE_dtm = S_dtm = SW_dtm = W_dtm = NW_dtm = dnullval;
  757. NNE_dtm = ENE_dtm = ESE_dtm = SSE_dtm = SSW_dtm = WSW_dtm = WNW_dtm = NNW_dtm = dnullval;
  758. N_cost = NE_cost = E_cost = SE_cost = S_cost = SW_cost = W_cost = NW_cost = dnullval;
  759. NNE_cost = ENE_cost = ESE_cost = SSE_cost = SSW_cost = WSW_cost = WNW_cost = NNW_cost = dnullval;
  760. /* If we have surpassed the user specified maximum cost, then quit */
  761. if (maxcost && ((double)maxcost < pres_cell->min_cost))
  762. break;
  763. /* If I've already been updated, delete me */
  764. Segment_get(&cost_seg, &costs, pres_cell->row, pres_cell->col);
  765. old_min_cost = costs.cost_out;
  766. if (!Rast_is_d_null_value(&old_min_cost)) {
  767. if (pres_cell->min_cost > old_min_cost) {
  768. delete(pres_cell);
  769. pres_cell = get_lowest();
  770. continue;
  771. }
  772. }
  773. my_dtm = costs.dtm;
  774. if (Rast_is_d_null_value(&my_dtm)) {
  775. delete(pres_cell);
  776. pres_cell = get_lowest();
  777. continue;
  778. }
  779. my_cost = costs.cost_in;
  780. if (Rast_is_d_null_value(&my_cost)) {
  781. delete(pres_cell);
  782. pres_cell = get_lowest();
  783. continue;
  784. }
  785. row = pres_cell->row;
  786. col = pres_cell->col;
  787. G_percent(n_processed++, total_cells, 1);
  788. /* 9 10 Order in which neighbors
  789. * 13 5 3 6 14 are visited (Knight move).
  790. * 1 2
  791. * 16 8 4 7 15
  792. * 12 11
  793. */
  794. /* drainage directions in degrees CCW from East
  795. * drainage directions are set for each neighbor and must be
  796. * read as from neighbor to current cell
  797. *
  798. * X = neighbor:
  799. *
  800. * 112.5 67.5
  801. * 157.5 135 90 45 22.5
  802. * 180 X 360
  803. * 202.5 225 270 315 337.5
  804. * 247.5 292.5
  805. *
  806. * X = present cell, directions for neighbors:
  807. *
  808. * 292.5 247.5
  809. * 337.5 315 270 225 202.5
  810. * 360 X 180
  811. * 22.5 45 90 135 157.5
  812. * 67.5 112.5
  813. */
  814. for (neighbor = 1; neighbor <= total_reviewed; neighbor++) {
  815. switch (neighbor) {
  816. case 1:
  817. col = pres_cell->col - 1;
  818. cur_dir = 360.0;
  819. break;
  820. case 2:
  821. col = pres_cell->col + 1;
  822. cur_dir = 180.0;
  823. break;
  824. case 3:
  825. row = pres_cell->row - 1;
  826. col = pres_cell->col;
  827. cur_dir = 270.0;
  828. break;
  829. case 4:
  830. row = pres_cell->row + 1;
  831. cur_dir = 90.0;
  832. break;
  833. case 5:
  834. row = pres_cell->row - 1;
  835. col = pres_cell->col - 1;
  836. cur_dir = 315.0;
  837. break;
  838. case 6:
  839. col = pres_cell->col + 1;
  840. cur_dir = 225.0;
  841. break;
  842. case 7:
  843. row = pres_cell->row + 1;
  844. cur_dir = 135.0;
  845. break;
  846. case 8:
  847. col = pres_cell->col - 1;
  848. cur_dir = 45.0;
  849. break;
  850. case 9:
  851. row = pres_cell->row - 2;
  852. col = pres_cell->col - 1;
  853. cur_dir = 292.5;
  854. break;
  855. case 10:
  856. col = pres_cell->col + 1;
  857. cur_dir = 247.5;
  858. break;
  859. case 11:
  860. row = pres_cell->row + 2;
  861. cur_dir = 112.5;
  862. break;
  863. case 12:
  864. col = pres_cell->col - 1;
  865. cur_dir = 67.5;
  866. break;
  867. case 13:
  868. row = pres_cell->row - 1;
  869. col = pres_cell->col - 2;
  870. cur_dir = 337.5;
  871. break;
  872. case 14:
  873. col = pres_cell->col + 2;
  874. cur_dir = 202.5;
  875. break;
  876. case 15:
  877. row = pres_cell->row + 1;
  878. cur_dir = 157.5;
  879. break;
  880. case 16:
  881. col = pres_cell->col - 2;
  882. cur_dir = 22.5;
  883. break;
  884. }
  885. if (row < 0 || row >= nrows)
  886. continue;
  887. if (col < 0 || col >= ncols)
  888. continue;
  889. min_cost = dnullval;
  890. Segment_get(&cost_seg, &costs, row, col);
  891. switch (neighbor) {
  892. case 1:
  893. W_dtm = costs.dtm;
  894. W_cost = costs.cost_in;
  895. if (Rast_is_d_null_value(&W_cost))
  896. continue;
  897. check_dtm = (W_dtm - my_dtm) / EW_fac;
  898. if (check_dtm >= 0)
  899. fcost_dtm = (double)(W_dtm - my_dtm) * b;
  900. else if (check_dtm < (slope_factor))
  901. fcost_dtm = (double)(W_dtm - my_dtm) * d;
  902. else
  903. fcost_dtm = (double)(W_dtm - my_dtm) * c;
  904. fcost_cost = (double)(W_cost + my_cost) / 2.0;
  905. min_cost =
  906. pres_cell->min_cost + fcost_dtm + (EW_fac * a) +
  907. lambda * fcost_cost * EW_fac;
  908. break;
  909. case 2:
  910. E_dtm = costs.dtm;
  911. E_cost = costs.cost_in;
  912. if (Rast_is_d_null_value(&E_cost))
  913. continue;
  914. check_dtm = (E_dtm - my_dtm) / EW_fac;
  915. if (check_dtm >= 0)
  916. fcost_dtm = (double)(E_dtm - my_dtm) * b;
  917. else if (check_dtm < (slope_factor))
  918. fcost_dtm = (double)(E_dtm - my_dtm) * d;
  919. else
  920. fcost_dtm = (double)(E_dtm - my_dtm) * c;
  921. fcost_cost = (double)(E_cost + my_cost) / 2.0;
  922. min_cost =
  923. pres_cell->min_cost + fcost_dtm + (EW_fac * a) +
  924. lambda * fcost_cost * EW_fac;
  925. break;
  926. case 3:
  927. N_dtm = costs.dtm;
  928. N_cost = costs.cost_in;
  929. if (Rast_is_d_null_value(&N_cost))
  930. continue;
  931. check_dtm = (N_dtm - my_dtm) / NS_fac;
  932. if (check_dtm >= 0)
  933. fcost_dtm = (double)(N_dtm - my_dtm) * b;
  934. else if (check_dtm < (slope_factor))
  935. fcost_dtm = (double)(N_dtm - my_dtm) * d;
  936. else
  937. fcost_dtm = (double)(N_dtm - my_dtm) * c;
  938. fcost_cost = (double)(N_cost + my_cost) / 2.0;
  939. min_cost =
  940. pres_cell->min_cost + fcost_dtm + (NS_fac * a) +
  941. lambda * fcost_cost * NS_fac;
  942. break;
  943. case 4:
  944. S_dtm = costs.dtm;
  945. S_cost = costs.cost_in;
  946. if (Rast_is_d_null_value(&S_cost))
  947. continue;
  948. check_dtm = (S_dtm - my_dtm) / NS_fac;
  949. if (check_dtm >= 0)
  950. fcost_dtm = (double)(S_dtm - my_dtm) * b;
  951. else if (check_dtm < (slope_factor))
  952. fcost_dtm = (double)(S_dtm - my_dtm) * d;
  953. else
  954. fcost_dtm = (double)(S_dtm - my_dtm) * c;
  955. fcost_cost = (double)(S_cost + my_cost) / 2.0;
  956. min_cost =
  957. pres_cell->min_cost + fcost_dtm + (NS_fac * a) +
  958. lambda * fcost_cost * NS_fac;
  959. break;
  960. case 5:
  961. NW_dtm = costs.dtm;
  962. NW_cost = costs.cost_in;
  963. if (Rast_is_d_null_value(&NW_cost))
  964. continue;
  965. check_dtm = (NW_dtm - my_dtm) / DIAG_fac;
  966. if (check_dtm >= 0)
  967. fcost_dtm = (double)(NW_dtm - my_dtm) * b;
  968. else if (check_dtm < (slope_factor))
  969. fcost_dtm = (double)(NW_dtm - my_dtm) * d;
  970. else
  971. fcost_dtm = (double)(NW_dtm - my_dtm) * c;
  972. fcost_cost = (double)(NW_cost + my_cost) / 2.0;
  973. min_cost =
  974. pres_cell->min_cost + fcost_dtm + (DIAG_fac * a) +
  975. lambda * fcost_cost * DIAG_fac;
  976. break;
  977. case 6:
  978. NE_dtm = costs.dtm;
  979. NE_cost = costs.cost_in;
  980. if (Rast_is_d_null_value(&NE_cost))
  981. continue;
  982. check_dtm = (NE_dtm - my_dtm) / DIAG_fac;
  983. if (check_dtm >= 0)
  984. fcost_dtm = (double)(NE_dtm - my_dtm) * b;
  985. else if (check_dtm < (slope_factor))
  986. fcost_dtm = (double)(NE_dtm - my_dtm) * d;
  987. else
  988. fcost_dtm = (double)(NE_dtm - my_dtm) * c;
  989. fcost_cost = (double)(NE_cost + my_cost) / 2.0;
  990. min_cost =
  991. pres_cell->min_cost + fcost_dtm + (DIAG_fac * a) +
  992. lambda * fcost_cost * DIAG_fac;
  993. break;
  994. case 7:
  995. SE_dtm = costs.dtm;
  996. SE_cost = costs.cost_in;
  997. if (Rast_is_d_null_value(&SE_cost))
  998. continue;
  999. check_dtm = (SE_dtm - my_dtm) / DIAG_fac;
  1000. if (check_dtm >= 0)
  1001. fcost_dtm = (double)(SE_dtm - my_dtm) * b;
  1002. else if (check_dtm < (slope_factor))
  1003. fcost_dtm = (double)(SE_dtm - my_dtm) * d;
  1004. else
  1005. fcost_dtm = (double)(SE_dtm - my_dtm) * c;
  1006. fcost_cost = (double)(SE_cost + my_cost) / 2.0;
  1007. min_cost =
  1008. pres_cell->min_cost + fcost_dtm + (DIAG_fac * a) +
  1009. lambda * fcost_cost * DIAG_fac;
  1010. break;
  1011. case 8:
  1012. SW_dtm = costs.dtm;
  1013. SW_cost = costs.cost_in;
  1014. if (Rast_is_d_null_value(&SW_cost))
  1015. continue;
  1016. check_dtm = (SW_dtm - my_dtm) / DIAG_fac;
  1017. if (check_dtm >= 0)
  1018. fcost_dtm = (double)(SW_dtm - my_dtm) * b;
  1019. else if (check_dtm < (slope_factor))
  1020. fcost_dtm = (double)(SW_dtm - my_dtm) * d;
  1021. else
  1022. fcost_dtm = (double)(SW_dtm - my_dtm) * c;
  1023. fcost_cost = (double)(SW_cost + my_cost) / 2.0;
  1024. min_cost =
  1025. pres_cell->min_cost + fcost_dtm + (DIAG_fac * a) +
  1026. lambda * fcost_cost * DIAG_fac;
  1027. break;
  1028. case 9:
  1029. NNW_dtm = costs.dtm;
  1030. NNW_cost = costs.cost_in;
  1031. if (Rast_is_d_null_value(&NNW_cost))
  1032. continue;
  1033. check_dtm = (NNW_dtm - my_dtm) / V_DIAG_fac;
  1034. if (check_dtm >= 0)
  1035. fcost_dtm = (double)(NNW_dtm - my_dtm) * b;
  1036. else if (check_dtm < (slope_factor))
  1037. fcost_dtm = (double)(NNW_dtm - my_dtm) * d;
  1038. else
  1039. fcost_dtm = (double)(NNW_dtm - my_dtm) * c;
  1040. fcost_cost =
  1041. (double)(N_cost + NW_cost + NNW_cost + my_cost) / 4.0;
  1042. min_cost =
  1043. pres_cell->min_cost + fcost_dtm + (V_DIAG_fac * a) +
  1044. lambda * fcost_cost * V_DIAG_fac;
  1045. break;
  1046. case 10:
  1047. NNE_dtm = costs.dtm;
  1048. NNE_cost = costs.cost_in;
  1049. if (Rast_is_d_null_value(&NNE_cost))
  1050. continue;
  1051. check_dtm = ((NNE_dtm - my_dtm) / V_DIAG_fac);
  1052. if (check_dtm >= 0)
  1053. fcost_dtm = (double)(NNE_dtm - my_dtm) * b;
  1054. else if (check_dtm < (slope_factor))
  1055. fcost_dtm = (double)(NNE_dtm - my_dtm) * d;
  1056. else
  1057. fcost_dtm = (double)(NNE_dtm - my_dtm) * c;
  1058. fcost_cost =
  1059. (double)(N_cost + NE_cost + NNE_cost + my_cost) / 4.0;
  1060. min_cost =
  1061. pres_cell->min_cost + fcost_dtm + (V_DIAG_fac * a) +
  1062. lambda * fcost_cost * V_DIAG_fac;
  1063. break;
  1064. case 11:
  1065. SSE_dtm = costs.dtm;
  1066. SSE_cost = costs.cost_in;
  1067. if (Rast_is_d_null_value(&SSE_cost))
  1068. continue;
  1069. check_dtm = (SSE_dtm - my_dtm) / V_DIAG_fac;
  1070. if (check_dtm >= 0)
  1071. fcost_dtm = (double)(SSE_dtm - my_dtm) * b;
  1072. else if (check_dtm < (slope_factor))
  1073. fcost_dtm = (double)(SSE_dtm - my_dtm) * d;
  1074. else
  1075. fcost_dtm = (double)(SSE_dtm - my_dtm) * c;
  1076. fcost_cost =
  1077. (double)(S_cost + SE_cost + SSE_cost + my_cost) / 4.0;
  1078. min_cost =
  1079. pres_cell->min_cost + fcost_dtm + (V_DIAG_fac * a) +
  1080. lambda * fcost_cost * V_DIAG_fac;
  1081. break;
  1082. case 12:
  1083. SSW_dtm = costs.dtm;
  1084. SSW_cost = costs.cost_in;
  1085. if (Rast_is_d_null_value(&SSW_cost))
  1086. continue;
  1087. check_dtm = (SSW_dtm - my_dtm) / V_DIAG_fac;
  1088. if (check_dtm >= 0)
  1089. fcost_dtm = (double)(SSW_dtm - my_dtm) * b;
  1090. else if (check_dtm < (slope_factor))
  1091. fcost_dtm = (double)(SSW_dtm - my_dtm) * d;
  1092. else
  1093. fcost_dtm = (double)(SSW_dtm - my_dtm) * c;
  1094. fcost_cost =
  1095. (double)(S_cost + SW_cost + SSW_cost + my_cost) / 4.0;
  1096. min_cost =
  1097. pres_cell->min_cost + fcost_dtm + (V_DIAG_fac * a) +
  1098. lambda * fcost_cost * V_DIAG_fac;
  1099. break;
  1100. case 13:
  1101. WNW_dtm = costs.dtm;
  1102. WNW_cost = costs.cost_in;
  1103. if (Rast_is_d_null_value(&WNW_cost))
  1104. continue;
  1105. check_dtm = (WNW_dtm - my_dtm) / H_DIAG_fac;
  1106. if (check_dtm >= 0)
  1107. fcost_dtm = (double)(WNW_dtm - my_dtm) * b;
  1108. else if (check_dtm < (slope_factor))
  1109. fcost_dtm = (double)(WNW_dtm - my_dtm) * d;
  1110. else
  1111. fcost_dtm = (double)(WNW_dtm - my_dtm) * c;
  1112. fcost_cost =
  1113. (double)(W_cost + NW_cost + WNW_cost + my_cost) / 4.0;
  1114. min_cost =
  1115. pres_cell->min_cost + fcost_dtm + (H_DIAG_fac * a) +
  1116. lambda * fcost_cost * H_DIAG_fac;
  1117. break;
  1118. case 14:
  1119. ENE_dtm = costs.dtm;
  1120. ENE_cost = costs.cost_in;
  1121. if (Rast_is_d_null_value(&ENE_cost))
  1122. continue;
  1123. check_dtm = (ENE_dtm - my_dtm) / H_DIAG_fac;
  1124. if (check_dtm >= 0)
  1125. fcost_dtm = (double)(ENE_dtm - my_dtm) * b;
  1126. else if (check_dtm < (slope_factor))
  1127. fcost_dtm = (double)(ENE_dtm - my_dtm) * d;
  1128. else
  1129. fcost_dtm = (double)(ENE_dtm - my_dtm) * c;
  1130. fcost_cost =
  1131. (double)(E_cost + NE_cost + ENE_cost + my_cost) / 4.0;
  1132. min_cost =
  1133. pres_cell->min_cost + fcost_dtm + (H_DIAG_fac * a) +
  1134. lambda * fcost_cost * H_DIAG_fac;
  1135. break;
  1136. case 15:
  1137. ESE_dtm = costs.dtm;
  1138. ESE_cost = costs.cost_in;
  1139. if (Rast_is_d_null_value(&ESE_cost))
  1140. continue;
  1141. check_dtm = (ESE_dtm - my_dtm) / H_DIAG_fac;
  1142. if (check_dtm >= 0)
  1143. fcost_dtm = (double)(ESE_dtm - my_dtm) * b;
  1144. else if (check_dtm < (slope_factor))
  1145. fcost_dtm = (double)(ESE_dtm - my_dtm) * d;
  1146. else
  1147. fcost_dtm = (double)(ESE_dtm - my_dtm) * c;
  1148. fcost_cost =
  1149. (double)(E_cost + SE_cost + ESE_cost + my_cost) / 4.0;
  1150. min_cost =
  1151. pres_cell->min_cost + fcost_dtm + (H_DIAG_fac * a) +
  1152. lambda * fcost_cost * H_DIAG_fac;
  1153. break;
  1154. case 16:
  1155. WSW_dtm = costs.dtm;
  1156. WSW_cost = costs.cost_in;
  1157. if (Rast_is_d_null_value(&WSW_cost))
  1158. continue;
  1159. check_dtm = (WSW_dtm - my_dtm) / H_DIAG_fac;
  1160. if (check_dtm >= 0)
  1161. fcost_dtm = (double)(WSW_dtm - my_dtm) * b;
  1162. else if (check_dtm < (slope_factor))
  1163. fcost_dtm = (double)(WSW_dtm - my_dtm) * d;
  1164. else
  1165. fcost_dtm = (double)(WSW_dtm - my_dtm) * c;
  1166. fcost_cost =
  1167. (double)(W_cost + SW_cost + WSW_cost + my_cost) / 4.0;
  1168. min_cost =
  1169. pres_cell->min_cost + fcost_dtm + (H_DIAG_fac * a) +
  1170. lambda * fcost_cost * H_DIAG_fac;
  1171. break;
  1172. }
  1173. if (Rast_is_d_null_value(&min_cost))
  1174. continue;
  1175. Segment_get(&cost_seg, &costs, row, col);
  1176. old_min_cost = costs.cost_out;
  1177. /* add to list */
  1178. if (Rast_is_d_null_value(&old_min_cost)) {
  1179. costs.cost_out = min_cost;
  1180. Segment_put(&cost_seg, &costs, row, col);
  1181. insert(min_cost, row, col);
  1182. if (dir == 1) {
  1183. Segment_put(&dir_seg, &cur_dir, row, col);
  1184. }
  1185. }
  1186. /* update with lower costs */
  1187. else if (old_min_cost > min_cost) {
  1188. costs.cost_out = min_cost;
  1189. Segment_put(&cost_seg, &costs, row, col);
  1190. insert(min_cost, row, col);
  1191. if (dir == 1) {
  1192. Segment_put(&dir_seg, &cur_dir, row, col);
  1193. }
  1194. }
  1195. }
  1196. if (have_stop_points && time_to_stop(pres_cell->row, pres_cell->col))
  1197. break;
  1198. ct = pres_cell;
  1199. delete(pres_cell);
  1200. pres_cell = get_lowest();
  1201. if (ct == pres_cell)
  1202. G_warning(_("Error, ct == pres_cell"));
  1203. }
  1204. G_percent(1, 1, 1);
  1205. /* free heap */
  1206. free_heap();
  1207. /* Open cumulative cost layer for writing */
  1208. cum_fd = Rast_open_new(cum_cost_layer, cum_data_type);
  1209. cum_cell = Rast_allocate_buf(cum_data_type);
  1210. /* Copy segmented map to output map */
  1211. G_message(_("Writing output raster map <%s>... "), cum_cost_layer);
  1212. cell2 = Rast_allocate_buf(dtm_data_type);
  1213. {
  1214. void *p;
  1215. void *p2;
  1216. int cum_dsize = Rast_cell_size(cum_data_type);
  1217. Rast_set_null_value(cell2, ncols, dtm_data_type);
  1218. for (row = 0; row < nrows; row++) {
  1219. G_percent(row, nrows, 2);
  1220. if (keep_nulls)
  1221. Rast_get_row(dtm_fd, cell2, row, dtm_data_type);
  1222. p = cum_cell;
  1223. p2 = cell2;
  1224. for (col = 0; col < ncols; col++) {
  1225. if (keep_nulls) {
  1226. if (Rast_is_null_value(p2, dtm_data_type)) {
  1227. Rast_set_null_value(p, 1, cum_data_type);
  1228. p = G_incr_void_ptr(p, cum_dsize);
  1229. p2 = G_incr_void_ptr(p2, dtm_dsize);
  1230. continue;
  1231. }
  1232. }
  1233. Segment_get(&cost_seg, &costs, row, col);
  1234. min_cost = costs.cost_out;
  1235. if (Rast_is_d_null_value(&min_cost)) {
  1236. Rast_set_null_value((p), 1, cum_data_type);
  1237. }
  1238. else {
  1239. if (min_cost > peak)
  1240. peak = min_cost;
  1241. switch (cum_data_type) {
  1242. case CELL_TYPE:
  1243. *(CELL *)p = (CELL)(min_cost + .5);
  1244. break;
  1245. case FCELL_TYPE:
  1246. *(FCELL *)p = (FCELL)(min_cost);
  1247. break;
  1248. case DCELL_TYPE:
  1249. *(DCELL *)p = (DCELL)(min_cost);
  1250. break;
  1251. }
  1252. }
  1253. p = G_incr_void_ptr(p, cum_dsize);
  1254. p2 = G_incr_void_ptr(p2, dtm_dsize);
  1255. }
  1256. Rast_put_row(cum_fd, cum_cell, cum_data_type);
  1257. }
  1258. G_percent(1, 1, 1);
  1259. G_free(cum_cell);
  1260. G_free(cell2);
  1261. }
  1262. if (dir == 1) {
  1263. void *p;
  1264. size_t dir_size = Rast_cell_size(dir_data_type);
  1265. dir_fd = Rast_open_new(move_dir_layer, dir_data_type);
  1266. dir_cell = Rast_allocate_buf(dir_data_type);
  1267. G_message(_("Writing output movement direction raster map <%s>..."), move_dir_layer);
  1268. for (row = 0; row < nrows; row++) {
  1269. p = dir_cell;
  1270. for (col = 0; col < ncols; col++) {
  1271. Segment_get(&dir_seg, &cur_dir, row, col);
  1272. *((FCELL *) p) = cur_dir;
  1273. p = G_incr_void_ptr(p, dir_size);
  1274. }
  1275. Rast_put_row(dir_fd, dir_cell, dir_data_type);
  1276. G_percent(row, nrows, 2);
  1277. }
  1278. G_percent(1, 1, 1);
  1279. G_free(dir_cell);
  1280. }
  1281. Segment_close(&cost_seg); /* release memory */
  1282. if (dir == 1)
  1283. Segment_close(&dir_seg);
  1284. Rast_close(dtm_fd);
  1285. Rast_close(cost_fd);
  1286. Rast_close(cum_fd);
  1287. if (dir == 1)
  1288. Rast_close(dir_fd);
  1289. /* writing history file */
  1290. Rast_short_history(cum_cost_layer, "raster", &history);
  1291. Rast_command_history(&history);
  1292. Rast_write_history(cum_cost_layer, &history);
  1293. if (dir == 1) {
  1294. Rast_short_history(move_dir_layer, "raster", &history);
  1295. Rast_command_history(&history);
  1296. Rast_write_history(move_dir_layer, &history);
  1297. }
  1298. /* Create colours for output map */
  1299. /*
  1300. * Rast_read_range (cum_cost_layer, "", &range);
  1301. * Rast_get_range_min_max(&range, &min, &max);
  1302. * G_make_color_wave(&colors,min, max);
  1303. * Rast_write_colors (cum_cost_layer,"",&colors);
  1304. */
  1305. G_done_msg(_("Peak cost value: %g"), peak);
  1306. exit(EXIT_SUCCESS);
  1307. }
  1308. int
  1309. process_answers(char **answers, struct start_pt **points,
  1310. struct start_pt **top_start_pt)
  1311. {
  1312. int col, row;
  1313. double east, north;
  1314. struct start_pt *new_start_pt;
  1315. int got_one = 0;
  1316. *points = NULL;
  1317. if (!answers)
  1318. return (0);
  1319. for (; *answers != NULL; answers += 2) {
  1320. if (!G_scan_easting(*answers, &east, G_projection()))
  1321. G_fatal_error(_("Illegal x coordinate <%s>"), *answers);
  1322. if (!G_scan_northing(*(answers + 1), &north, G_projection()))
  1323. G_fatal_error(_("Illegal y coordinate <%s>"), *(answers + 1));
  1324. if (east < window.west || east > window.east ||
  1325. north < window.south || north > window.north) {
  1326. G_warning(_("Warning, ignoring point outside window: %g, %g"),
  1327. east, north);
  1328. continue;
  1329. }
  1330. else
  1331. got_one = 1;
  1332. row = (window.north - north) / window.ns_res;
  1333. col = (east - window.west) / window.ew_res;
  1334. new_start_pt = (struct start_pt *)(G_malloc(sizeof(struct start_pt)));
  1335. new_start_pt->row = row;
  1336. new_start_pt->col = col;
  1337. new_start_pt->next = NULL;
  1338. if (*points == NULL) {
  1339. *points = new_start_pt;
  1340. *top_start_pt = new_start_pt;
  1341. new_start_pt->next = NULL;
  1342. }
  1343. else {
  1344. (*top_start_pt)->next = new_start_pt;
  1345. *top_start_pt = new_start_pt;
  1346. }
  1347. }
  1348. return (got_one);
  1349. }
  1350. int time_to_stop(int row, int col)
  1351. {
  1352. static int total = 0;
  1353. static int hits = 0;
  1354. struct start_pt *points;
  1355. if (total == 0) {
  1356. for (points = head_end_pt;
  1357. points != NULL; points = points->next, total++) ;
  1358. }
  1359. for (points = head_end_pt; points != NULL; points = points->next)
  1360. if (points->row == row && points->col == col) {
  1361. hits++;
  1362. if (hits == total)
  1363. return (1);
  1364. }
  1365. return (0);
  1366. }