main.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387
  1. /****************************************************************************
  2. *
  3. * MODULE: r.slope.aspect
  4. * AUTHOR(S): Michael Shapiro and
  5. * Olga Waupotitsch (original CERL contributors),
  6. * Markus Neteler <neteler itc.it>,
  7. * Bernhard Reiter <bernhard intevation.de>,
  8. * Brad Douglas <rez touchofmadness.com>,
  9. * Glynn Clements <glynn gclements.plus.com>,
  10. * Hamish Bowman <hamish_b yahoo.com>,
  11. * Jachym Cepicky <jachym les-ejk.cz>,
  12. * Jan-Oliver Wagner <jan intevation.de>,
  13. * Radim Blazek <radim.blazek gmail.com>
  14. * PURPOSE: generates raster maps of slope, aspect, curvatures and
  15. * first and second order partial derivatives from a raster map
  16. * of true elevation values
  17. * COPYRIGHT: (C) 1999-2011 by the GRASS Development Team
  18. *
  19. * This program is free software under the GNU General Public
  20. * License (>=v2). Read the file COPYING that comes with GRASS
  21. * for details.
  22. *
  23. *****************************************************************************/
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <math.h>
  27. #include <grass/gis.h>
  28. #include <grass/raster.h>
  29. #include <grass/glocale.h>
  30. /* 10/99 from GMSL, updated to new GRASS 5 code style , changed default "prec" to float */
  31. #define abs(x) ((x)<0?-(x):(x))
  32. /**************************************************************************
  33. * input is from command line.
  34. * arguments are elevation file, slope file, aspect file, profile curvature
  35. * file and tangential curvature file
  36. * elevation filename required
  37. * either slope filename or aspect filename or profile curvature filename
  38. * or tangential curvature filename required
  39. * usage: r.slope.aspect [-av] elevation=input slope=output1 aspect=output2
  40. * pcurv=output3 tcurv=output4 format=name prec=name zfactor=value
  41. * min_slp_allowed=value dx=output5 dy=output6 dxx=output7
  42. * dyy=output8 dxy=output9
  43. * -a don't align window
  44. * -q quiet
  45. **************************************************************************/
  46. /* some changes made to code to retrieve correct distances when using
  47. lat/lon projection. changes involve recalculating H and V. see
  48. comments within code. */
  49. /* added colortables for topographic parameters and fixed
  50. * the sign bug for the second order derivatives (jh) */
  51. int main(int argc, char *argv[])
  52. {
  53. struct Categories cats;
  54. int elevation_fd;
  55. int aspect_fd;
  56. int slope_fd;
  57. int pcurv_fd;
  58. int tcurv_fd;
  59. int dx_fd;
  60. int dy_fd;
  61. int dxx_fd;
  62. int dyy_fd;
  63. int dxy_fd;
  64. DCELL *elev_cell[3], *temp;
  65. DCELL *c1, *c2, *c3, *c4, *c5, *c6, *c7, *c8, *c9;
  66. DCELL tmp1, tmp2;
  67. FCELL dat1, dat2;
  68. CELL cat;
  69. void *asp_raster, *asp_ptr = NULL;
  70. void *slp_raster, *slp_ptr = NULL;
  71. void *pcurv_raster, *pcurv_ptr = NULL;
  72. void *tcurv_raster, *tcurv_ptr = NULL;
  73. void *dx_raster, *dx_ptr = NULL;
  74. void *dy_raster, *dy_ptr = NULL;
  75. void *dxx_raster, *dxx_ptr = NULL;
  76. void *dyy_raster, *dyy_ptr = NULL;
  77. void *dxy_raster, *dxy_ptr = NULL;
  78. int i;
  79. RASTER_MAP_TYPE out_type, data_type;
  80. int Wrap; /* global wraparound */
  81. struct Cell_head window, cellhd;
  82. struct History hist;
  83. struct Colors colors;
  84. const char *elev_name;
  85. const char *aspect_name;
  86. const char *slope_name;
  87. const char *pcurv_name;
  88. const char *tcurv_name;
  89. const char *dx_name;
  90. const char *dy_name;
  91. const char *dxx_name;
  92. const char *dyy_name;
  93. const char *dxy_name;
  94. char buf[300];
  95. int nrows, row;
  96. int ncols, col;
  97. double north, east, south, west, ns_med;
  98. double radians_to_degrees;
  99. double degrees_to_radians;
  100. double H, V;
  101. double dx; /* partial derivative in ew direction */
  102. double dy; /* partial derivative in ns direction */
  103. double dxx, dxy, dyy;
  104. double s3, s4, s5, s6;
  105. double pcurv, tcurv;
  106. double scik1 = 100000.;
  107. double zfactor;
  108. double factor;
  109. double aspect, min_asp = 360., max_asp = 0.;
  110. double dnorm1, dx2, dy2, grad2, grad, dxy2;
  111. double gradmin = 0.001;
  112. double c1min = 0., c1max = 0., c2min = 0., c2max = 0.;
  113. double answer[92];
  114. double degrees;
  115. double tan_ans;
  116. double key;
  117. double slp_in_perc, slp_in_deg;
  118. double min_slp = 900., max_slp = 0., min_slp_allowed;
  119. int low, hi, test = 0;
  120. int deg = 0;
  121. int perc = 0;
  122. char *slope_fmt;
  123. struct GModule *module;
  124. struct
  125. {
  126. struct Option *elevation, *slope_fmt, *slope, *aspect, *pcurv, *tcurv,
  127. *zfactor, *min_slp_allowed, *out_precision,
  128. *dx, *dy, *dxx, *dyy, *dxy;
  129. } parm;
  130. struct
  131. {
  132. struct Flag *a;
  133. } flag;
  134. G_gisinit(argv[0]);
  135. module = G_define_module();
  136. G_add_keyword(_("raster"));
  137. G_add_keyword(_("terrain"));
  138. module->label = _("Generates raster maps of slope, aspect, curvatures and "
  139. "partial derivatives from a elevation raster map.");
  140. module->description = _("Aspect is calculated counterclockwise from east.");
  141. parm.elevation = G_define_standard_option(G_OPT_R_ELEV);
  142. parm.slope = G_define_standard_option(G_OPT_R_OUTPUT);
  143. parm.slope->key = "slope";
  144. parm.slope->required = NO;
  145. parm.slope->description = _("Name for output slope raster map");
  146. parm.slope->guisection = _("Outputs");
  147. parm.aspect = G_define_standard_option(G_OPT_R_OUTPUT);
  148. parm.aspect->key = "aspect";
  149. parm.aspect->required = NO;
  150. parm.aspect->description = _("Name for output aspect raster map");
  151. parm.aspect->guisection = _("Outputs");
  152. parm.slope_fmt = G_define_option();
  153. parm.slope_fmt->key = "format";
  154. parm.slope_fmt->type = TYPE_STRING;
  155. parm.slope_fmt->required = NO;
  156. parm.slope_fmt->answer = "degrees";
  157. parm.slope_fmt->options = "degrees,percent";
  158. parm.slope_fmt->description = _("Format for reporting the slope");
  159. parm.slope_fmt->guisection = _("Settings");
  160. parm.out_precision = G_define_option();
  161. parm.out_precision->key = "precision";
  162. parm.out_precision->type = TYPE_STRING;
  163. parm.out_precision->options = "CELL,FCELL,DCELL";
  164. parm.out_precision->description =
  165. _("Type of output aspect and slope maps");
  166. parm.out_precision->answer = "FCELL";
  167. parm.out_precision->guisection = _("Settings");
  168. parm.pcurv = G_define_standard_option(G_OPT_R_OUTPUT);
  169. parm.pcurv->key = "pcurv";
  170. parm.pcurv->required = NO;
  171. parm.pcurv->description =
  172. _("Name for output profile curvature raster map");
  173. parm.pcurv->guisection = _("Outputs");
  174. parm.tcurv = G_define_standard_option(G_OPT_R_OUTPUT);
  175. parm.tcurv->key = "tcurv";
  176. parm.tcurv->required = NO;
  177. parm.tcurv->description =
  178. _("Name for output tangential curvature raster map");
  179. parm.tcurv->guisection = _("Outputs");
  180. parm.dx = G_define_standard_option(G_OPT_R_OUTPUT);
  181. parm.dx->key = "dx";
  182. parm.dx->required = NO;
  183. parm.dx->description =
  184. _("Name for output first order partial derivative dx (E-W slope) raster map");
  185. parm.dx->guisection = _("Outputs");
  186. parm.dy = G_define_standard_option(G_OPT_R_OUTPUT);
  187. parm.dy->key = "dy";
  188. parm.dy->required = NO;
  189. parm.dy->description =
  190. _("Name for output first order partial derivative dy (N-S slope) raster map");
  191. parm.dy->guisection = _("Outputs");
  192. parm.dxx = G_define_standard_option(G_OPT_R_OUTPUT);
  193. parm.dxx->key = "dxx";
  194. parm.dxx->required = NO;
  195. parm.dxx->description =
  196. _("Name for output second order partial derivative dxx raster map");
  197. parm.dxx->guisection = _("Outputs");
  198. parm.dyy = G_define_standard_option(G_OPT_R_OUTPUT);
  199. parm.dyy->key = "dyy";
  200. parm.dyy->required = NO;
  201. parm.dyy->description =
  202. _("Name for output second order partial derivative dyy raster map");
  203. parm.dyy->guisection = _("Outputs");
  204. parm.dxy = G_define_standard_option(G_OPT_R_OUTPUT);
  205. parm.dxy->key = "dxy";
  206. parm.dxy->required = NO;
  207. parm.dxy->description =
  208. _("Name for output second order partial derivative dxy raster map");
  209. parm.dxy->guisection = _("Outputs");
  210. parm.zfactor = G_define_option();
  211. parm.zfactor->key = "zfactor";
  212. parm.zfactor->description =
  213. _("Multiplicative factor to convert elevation units to meters");
  214. parm.zfactor->type = TYPE_DOUBLE;
  215. parm.zfactor->required = NO;
  216. parm.zfactor->answer = "1.0";
  217. parm.zfactor->guisection = _("Settings");
  218. parm.min_slp_allowed = G_define_option();
  219. parm.min_slp_allowed->key = "min_slp_allowed";
  220. parm.min_slp_allowed->description =
  221. _("Minimum slope val. (in percent) for which aspect is computed");
  222. parm.min_slp_allowed->type = TYPE_DOUBLE;
  223. parm.min_slp_allowed->required = NO;
  224. parm.min_slp_allowed->answer = "0.0";
  225. parm.min_slp_allowed->guisection = _("Settings");
  226. flag.a = G_define_flag();
  227. flag.a->key = 'a';
  228. flag.a->description =
  229. _("Do not align the current region to the elevation layer");
  230. flag.a->guisection = _("Settings");
  231. if (G_parser(argc, argv))
  232. exit(EXIT_FAILURE);
  233. radians_to_degrees = 180.0 / M_PI;
  234. degrees_to_radians = M_PI / 180.0;
  235. /* INC BY ONE
  236. answer[0] = 0.0;
  237. answer[91] = 15000.0;
  238. for (i = 1; i < 91; i++)
  239. {
  240. degrees = i - .5;
  241. tan_ans = tan ( degrees / radians_to_degrees );
  242. answer[i] = tan_ans * tan_ans;
  243. }
  244. */
  245. answer[0] = 0.0;
  246. answer[90] = 15000.0;
  247. for (i = 0; i < 90; i++) {
  248. degrees = i + .5;
  249. tan_ans = tan(degrees / radians_to_degrees);
  250. answer[i] = tan_ans * tan_ans;
  251. }
  252. elev_name = parm.elevation->answer;
  253. slope_name = parm.slope->answer;
  254. aspect_name = parm.aspect->answer;
  255. pcurv_name = parm.pcurv->answer;
  256. tcurv_name = parm.tcurv->answer;
  257. dx_name = parm.dx->answer;
  258. dy_name = parm.dy->answer;
  259. dxx_name = parm.dxx->answer;
  260. dyy_name = parm.dyy->answer;
  261. dxy_name = parm.dxy->answer;
  262. G_check_input_output_name(elev_name, slope_name, G_FATAL_EXIT);
  263. G_check_input_output_name(elev_name, aspect_name, G_FATAL_EXIT);
  264. G_check_input_output_name(elev_name, pcurv_name, G_FATAL_EXIT);
  265. G_check_input_output_name(elev_name, tcurv_name, G_FATAL_EXIT);
  266. G_check_input_output_name(elev_name, dx_name, G_FATAL_EXIT);
  267. G_check_input_output_name(elev_name, dy_name, G_FATAL_EXIT);
  268. G_check_input_output_name(elev_name, dxx_name, G_FATAL_EXIT);
  269. G_check_input_output_name(elev_name, dyy_name, G_FATAL_EXIT);
  270. G_check_input_output_name(elev_name, dxy_name, G_FATAL_EXIT);
  271. if (sscanf(parm.zfactor->answer, "%lf", &zfactor) != 1 || zfactor <= 0.0) {
  272. G_fatal_error(_("%s=%s - must be a positive number"),
  273. parm.zfactor->key, parm.zfactor->answer);
  274. }
  275. if (sscanf(parm.min_slp_allowed->answer, "%lf", &min_slp_allowed) != 1 ||
  276. min_slp_allowed < 0.0) {
  277. G_fatal_error(_("%s=%s - must be a non-negative number"),
  278. parm.min_slp_allowed->key,
  279. parm.min_slp_allowed->answer);
  280. }
  281. slope_fmt = parm.slope_fmt->answer;
  282. if (strcmp(slope_fmt, "percent") == 0)
  283. perc = 1;
  284. else if (strcmp(slope_fmt, "degrees") == 0)
  285. deg = 1;
  286. if (slope_name == NULL && aspect_name == NULL
  287. && pcurv_name == NULL && tcurv_name == NULL
  288. && dx_name == NULL && dy_name == NULL
  289. && dxx_name == NULL && dyy_name == NULL && dxy_name == NULL) {
  290. G_fatal_error(_("You must specify at least one of the parameters: "
  291. "<%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s> or <%s>"),
  292. parm.slope->key, parm.aspect->key, parm.pcurv->key,
  293. parm.tcurv->key, parm.dx->key, parm.dy->key,
  294. parm.dxx->key, parm.dyy->key, parm.dxy->key);
  295. }
  296. /* set the window from the header for the elevation file */
  297. if (!flag.a->answer) {
  298. G_get_window(&window);
  299. Rast_get_cellhd(elev_name, "", &cellhd);
  300. Rast_align_window(&window, &cellhd);
  301. Rast_set_window(&window);
  302. }
  303. if (strcmp(parm.out_precision->answer, "DCELL") == 0)
  304. out_type = DCELL_TYPE;
  305. else if (strcmp(parm.out_precision->answer, "FCELL") == 0)
  306. out_type = FCELL_TYPE;
  307. else if (strcmp(parm.out_precision->answer, "CELL") == 0)
  308. out_type = CELL_TYPE;
  309. else
  310. G_fatal_error(_("Wrong raster type: %s"), parm.out_precision->answer);
  311. data_type = out_type;
  312. /* data type is the type of data being processed,
  313. out_type is type of map being created */
  314. /* ? why not use Rast_map_type() then ? */
  315. G_get_set_window(&window);
  316. nrows = Rast_window_rows();
  317. ncols = Rast_window_cols();
  318. if (((window.west == (window.east - 360.))
  319. || (window.east == (window.west - 360.))) &&
  320. (G_projection() == PROJECTION_LL)) {
  321. Wrap = 1;
  322. ncols += 2;
  323. }
  324. else
  325. Wrap = 0;
  326. /* H = window.ew_res * 4 * 2/ zfactor; *//* horizontal (east-west) run
  327. times 4 for weighted difference */
  328. /* V = window.ns_res * 4 * 2/ zfactor; *//* vertical (north-south) run
  329. times 4 for weighted difference */
  330. /* give warning if location units are different from meters and zfactor=1 */
  331. factor = G_database_units_to_meters_factor();
  332. if (factor != 1.0)
  333. G_warning(_("Converting units to meters, factor=%.6f"), factor);
  334. G_begin_distance_calculations();
  335. north = Rast_row_to_northing(0.5, &window);
  336. ns_med = Rast_row_to_northing(1.5, &window);
  337. south = Rast_row_to_northing(2.5, &window);
  338. east = Rast_col_to_easting(2.5, &window);
  339. west = Rast_col_to_easting(0.5, &window);
  340. V = G_distance(east, north, east, south) * 4 / zfactor;
  341. H = G_distance(east, ns_med, west, ns_med) * 4 / zfactor;
  342. /* ____________________________
  343. |c1 |c2 |c3 |
  344. | | | |
  345. | | north | |
  346. | | | |
  347. |________|________|________|
  348. |c4 |c5 |c6 |
  349. | | | |
  350. | east | ns_med | west |
  351. | | | |
  352. |________|________|________|
  353. |c7 |c8 |c9 |
  354. | | | |
  355. | | south | |
  356. | | | |
  357. |________|________|________|
  358. */
  359. /* open the elevation file for reading */
  360. elevation_fd = Rast_open_old(elev_name, "");
  361. elev_cell[0] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  362. Rast_set_d_null_value(elev_cell[0], ncols);
  363. elev_cell[1] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  364. Rast_set_d_null_value(elev_cell[1], ncols);
  365. elev_cell[2] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  366. Rast_set_d_null_value(elev_cell[2], ncols);
  367. if (slope_name != NULL) {
  368. slope_fd = Rast_open_new(slope_name, out_type);
  369. slp_raster = Rast_allocate_buf(data_type);
  370. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  371. Rast_put_row(slope_fd, slp_raster, data_type);
  372. }
  373. else {
  374. slp_raster = NULL;
  375. slope_fd = -1;
  376. }
  377. if (aspect_name != NULL) {
  378. aspect_fd = Rast_open_new(aspect_name, out_type);
  379. asp_raster = Rast_allocate_buf(data_type);
  380. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  381. Rast_put_row(aspect_fd, asp_raster, data_type);
  382. }
  383. else {
  384. asp_raster = NULL;
  385. aspect_fd = -1;
  386. }
  387. if (pcurv_name != NULL) {
  388. pcurv_fd = Rast_open_new(pcurv_name, out_type);
  389. pcurv_raster = Rast_allocate_buf(data_type);
  390. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  391. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  392. }
  393. else {
  394. pcurv_raster = NULL;
  395. pcurv_fd = -1;
  396. }
  397. if (tcurv_name != NULL) {
  398. tcurv_fd = Rast_open_new(tcurv_name, out_type);
  399. tcurv_raster = Rast_allocate_buf(data_type);
  400. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  401. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  402. }
  403. else {
  404. tcurv_raster = NULL;
  405. tcurv_fd = -1;
  406. }
  407. if (dx_name != NULL) {
  408. dx_fd = Rast_open_new(dx_name, out_type);
  409. dx_raster = Rast_allocate_buf(data_type);
  410. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  411. Rast_put_row(dx_fd, dx_raster, data_type);
  412. }
  413. else {
  414. dx_raster = NULL;
  415. dx_fd = -1;
  416. }
  417. if (dy_name != NULL) {
  418. dy_fd = Rast_open_new(dy_name, out_type);
  419. dy_raster = Rast_allocate_buf(data_type);
  420. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  421. Rast_put_row(dy_fd, dy_raster, data_type);
  422. }
  423. else {
  424. dy_raster = NULL;
  425. dy_fd = -1;
  426. }
  427. if (dxx_name != NULL) {
  428. dxx_fd = Rast_open_new(dxx_name, out_type);
  429. dxx_raster = Rast_allocate_buf(data_type);
  430. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  431. Rast_put_row(dxx_fd, dxx_raster, data_type);
  432. }
  433. else {
  434. dxx_raster = NULL;
  435. dxx_fd = -1;
  436. }
  437. if (dyy_name != NULL) {
  438. dyy_fd = Rast_open_new(dyy_name, out_type);
  439. dyy_raster = Rast_allocate_buf(data_type);
  440. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  441. Rast_put_row(dyy_fd, dyy_raster, data_type);
  442. }
  443. else {
  444. dyy_raster = NULL;
  445. dyy_fd = -1;
  446. }
  447. if (dxy_name != NULL) {
  448. dxy_fd = Rast_open_new(dxy_name, out_type);
  449. dxy_raster = Rast_allocate_buf(data_type);
  450. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  451. Rast_put_row(dxy_fd, dxy_raster, data_type);
  452. }
  453. else {
  454. dxy_raster = NULL;
  455. dxy_fd = -1;
  456. }
  457. if (aspect_fd < 0 && slope_fd < 0 && pcurv_fd < 0 && tcurv_fd < 0
  458. && dx_fd < 0 && dy_fd < 0 && dxx_fd < 0 && dyy_fd < 0 && dxy_fd < 0)
  459. exit(EXIT_FAILURE);
  460. if (Wrap) {
  461. Rast_get_d_row_nomask(elevation_fd, elev_cell[1] + 1, 0);
  462. elev_cell[1][0] = elev_cell[1][Rast_window_cols() - 1];
  463. elev_cell[1][Rast_window_cols() + 1] = elev_cell[1][2];
  464. }
  465. else
  466. Rast_get_d_row_nomask(elevation_fd, elev_cell[1], 0);
  467. if (Wrap) {
  468. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, 1);
  469. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  470. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  471. }
  472. else
  473. Rast_get_d_row_nomask(elevation_fd, elev_cell[2], 1);
  474. G_verbose_message(_("Percent complete..."));
  475. for (row = 2; row < nrows; row++) {
  476. /* if projection is Lat/Lon, recalculate V and H */
  477. if (G_projection() == PROJECTION_LL) {
  478. north = Rast_row_to_northing((row - 2 + 0.5), &window);
  479. ns_med = Rast_row_to_northing((row - 1 + 0.5), &window);
  480. south = Rast_row_to_northing((row + 0.5), &window);
  481. east = Rast_col_to_easting(2.5, &window);
  482. west = Rast_col_to_easting(0.5, &window);
  483. V = G_distance(east, north, east, south) * 4 / zfactor;
  484. H = G_distance(east, ns_med, west, ns_med) * 4 / zfactor;
  485. /* ____________________________
  486. |c1 |c2 |c3 |
  487. | | | |
  488. | | north | |
  489. | | | |
  490. |________|________|________|
  491. |c4 |c5 |c6 |
  492. | | | |
  493. | east | ns_med | west |
  494. | | | |
  495. |________|________|________|
  496. |c7 |c8 |c9 |
  497. | | | |
  498. | | south | |
  499. | | | |
  500. |________|________|________|
  501. */
  502. }
  503. G_percent(row, nrows, 2);
  504. temp = elev_cell[0];
  505. elev_cell[0] = elev_cell[1];
  506. elev_cell[1] = elev_cell[2];
  507. elev_cell[2] = temp;
  508. if (Wrap) {
  509. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, row);
  510. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  511. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  512. }
  513. else
  514. Rast_get_d_row_nomask(elevation_fd, elev_cell[2], row);
  515. c1 = elev_cell[0];
  516. c2 = c1 + 1;
  517. c3 = c1 + 2;
  518. c4 = elev_cell[1];
  519. c5 = c4 + 1;
  520. c6 = c4 + 2;
  521. c7 = elev_cell[2];
  522. c8 = c7 + 1;
  523. c9 = c7 + 2;
  524. if (aspect_fd >= 0) {
  525. if (Wrap)
  526. asp_ptr = asp_raster;
  527. else
  528. asp_ptr =
  529. G_incr_void_ptr(asp_raster, Rast_cell_size(data_type));
  530. }
  531. if (slope_fd >= 0) {
  532. if (Wrap)
  533. slp_ptr = slp_raster;
  534. else
  535. slp_ptr =
  536. G_incr_void_ptr(slp_raster, Rast_cell_size(data_type));
  537. }
  538. if (pcurv_fd >= 0) {
  539. if (Wrap)
  540. pcurv_ptr = pcurv_raster;
  541. else
  542. pcurv_ptr =
  543. G_incr_void_ptr(pcurv_raster, Rast_cell_size(data_type));
  544. }
  545. if (tcurv_fd >= 0) {
  546. if (Wrap)
  547. tcurv_ptr = tcurv_raster;
  548. else
  549. tcurv_ptr =
  550. G_incr_void_ptr(tcurv_raster, Rast_cell_size(data_type));
  551. }
  552. if (dx_fd >= 0) {
  553. if (Wrap)
  554. dx_ptr = dx_raster;
  555. else
  556. dx_ptr = G_incr_void_ptr(dx_raster, Rast_cell_size(data_type));
  557. }
  558. if (dy_fd >= 0) {
  559. if (Wrap)
  560. dy_ptr = dy_raster;
  561. else
  562. dy_ptr = G_incr_void_ptr(dy_raster, Rast_cell_size(data_type));
  563. }
  564. if (dxx_fd >= 0) {
  565. if (Wrap)
  566. dxx_ptr = dxx_raster;
  567. else
  568. dxx_ptr =
  569. G_incr_void_ptr(dxx_raster, Rast_cell_size(data_type));
  570. }
  571. if (dyy_fd >= 0) {
  572. if (Wrap)
  573. dyy_ptr = dyy_raster;
  574. else
  575. dyy_ptr =
  576. G_incr_void_ptr(dyy_raster, Rast_cell_size(data_type));
  577. }
  578. if (dxy_fd >= 0) {
  579. if (Wrap)
  580. dxy_ptr = dxy_raster;
  581. else
  582. dxy_ptr =
  583. G_incr_void_ptr(dxy_raster, Rast_cell_size(data_type));
  584. }
  585. /*skip first cell of the row */
  586. for (col = ncols - 2; col-- > 0;
  587. c1++, c2++, c3++, c4++, c5++, c6++, c7++, c8++, c9++) {
  588. /* DEBUG:
  589. fprintf(stdout, "\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n",
  590. *c1, *c2, *c3, *c4, *c5, *c6, *c7, *c8, *c9);
  591. */
  592. if (Rast_is_d_null_value(c1) || Rast_is_d_null_value(c2) ||
  593. Rast_is_d_null_value(c3) || Rast_is_d_null_value(c4) ||
  594. Rast_is_d_null_value(c5) || Rast_is_d_null_value(c6) ||
  595. Rast_is_d_null_value(c7) || Rast_is_d_null_value(c8) ||
  596. Rast_is_d_null_value(c9)) {
  597. if (slope_fd > 0) {
  598. Rast_set_null_value(slp_ptr, 1, data_type);
  599. slp_ptr =
  600. G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  601. }
  602. if (aspect_fd > 0) {
  603. Rast_set_null_value(asp_ptr, 1, data_type);
  604. asp_ptr =
  605. G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  606. }
  607. if (pcurv_fd > 0) {
  608. Rast_set_null_value(pcurv_ptr, 1, data_type);
  609. pcurv_ptr =
  610. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  611. }
  612. if (tcurv_fd > 0) {
  613. Rast_set_null_value(tcurv_ptr, 1, data_type);
  614. tcurv_ptr =
  615. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  616. }
  617. if (dx_fd > 0) {
  618. Rast_set_null_value(dx_ptr, 1, data_type);
  619. dx_ptr =
  620. G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  621. }
  622. if (dy_fd > 0) {
  623. Rast_set_null_value(dy_ptr, 1, data_type);
  624. dy_ptr =
  625. G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  626. }
  627. if (dxx_fd > 0) {
  628. Rast_set_null_value(dxx_ptr, 1, data_type);
  629. dxx_ptr =
  630. G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  631. }
  632. if (dyy_fd > 0) {
  633. Rast_set_null_value(dyy_ptr, 1, data_type);
  634. dyy_ptr =
  635. G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  636. }
  637. if (dxy_fd > 0) {
  638. Rast_set_null_value(dxy_ptr, 1, data_type);
  639. dxy_ptr =
  640. G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  641. }
  642. continue;
  643. } /* no data */
  644. dx = ((*c1 + *c4 + *c4 + *c7) - (*c3 + *c6 + *c6 + *c9)) / H;
  645. dy = ((*c7 + *c8 + *c8 + *c9) - (*c1 + *c2 + *c2 + *c3)) / V;
  646. /* compute topographic parameters */
  647. key = dx * dx + dy * dy;
  648. slp_in_perc = 100 * sqrt(key);
  649. slp_in_deg = atan(sqrt(key)) * radians_to_degrees;
  650. /* now update min and max */
  651. if (deg) {
  652. if (min_slp > slp_in_deg)
  653. min_slp = slp_in_deg;
  654. if (max_slp < slp_in_deg)
  655. max_slp = slp_in_deg;
  656. }
  657. else {
  658. if (min_slp > slp_in_perc)
  659. min_slp = slp_in_perc;
  660. if (max_slp < slp_in_perc)
  661. max_slp = slp_in_perc;
  662. }
  663. if (slp_in_perc < min_slp_allowed)
  664. slp_in_perc = 0.;
  665. if (deg && out_type == CELL_TYPE) {
  666. /* INC BY ONE
  667. low = 1;
  668. hi = 91;
  669. */
  670. low = 0;
  671. hi = 90;
  672. test = 20;
  673. while (hi >= low) {
  674. if (key >= answer[test])
  675. low = test + 1;
  676. else if (key < answer[test - 1])
  677. hi = test - 1;
  678. else
  679. break;
  680. test = (low + hi) / 2;
  681. }
  682. }
  683. else if (perc && out_type == CELL_TYPE)
  684. /* INCR_BY_ONE */
  685. /* test = slp_in_perc + 1.5; *//* All the slope categories are
  686. incremented by 1 */
  687. test = slp_in_perc + .5;
  688. if (slope_fd > 0) {
  689. if (data_type == CELL_TYPE)
  690. *((CELL *) slp_ptr) = (CELL) test;
  691. else {
  692. if (deg)
  693. Rast_set_d_value(slp_ptr,
  694. (DCELL) slp_in_deg, data_type);
  695. else
  696. Rast_set_d_value(slp_ptr,
  697. (DCELL) slp_in_perc, data_type);
  698. }
  699. slp_ptr = G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  700. } /* computing slope */
  701. if (aspect_fd > 0) {
  702. if (key == 0.)
  703. aspect = 0.;
  704. else if (dx == 0) {
  705. if (dy > 0)
  706. aspect = 90.;
  707. else
  708. aspect = 270.;
  709. }
  710. else {
  711. aspect = (atan2(dy, dx) / degrees_to_radians);
  712. if ((aspect <= 0.5) && (aspect > 0) &&
  713. out_type == CELL_TYPE)
  714. aspect = 360.;
  715. if (aspect <= 0.)
  716. aspect = 360. + aspect;
  717. }
  718. /* if it's not the case that the slope for this cell
  719. is below specified minimum */
  720. if (!((slope_fd > 0) && (slp_in_perc < min_slp_allowed))) {
  721. if (out_type == CELL_TYPE)
  722. *((CELL *) asp_ptr) = (CELL) (aspect + .5);
  723. else
  724. Rast_set_d_value(asp_ptr,
  725. (DCELL) aspect, data_type);
  726. }
  727. else
  728. Rast_set_null_value(asp_ptr, 1, data_type);
  729. asp_ptr = G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  730. /* now update min and max */
  731. if (min_asp > aspect)
  732. min_asp = aspect;
  733. if (max_asp < aspect)
  734. max_asp = aspect;
  735. } /* computing aspect */
  736. if (dx_fd > 0) {
  737. if (out_type == CELL_TYPE)
  738. *((CELL *) dx_ptr) = (CELL) (scik1 * dx);
  739. else
  740. Rast_set_d_value(dx_ptr, (DCELL) dx, data_type);
  741. dx_ptr = G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  742. }
  743. if (dy_fd > 0) {
  744. if (out_type == CELL_TYPE)
  745. *((CELL *) dy_ptr) = (CELL) (scik1 * dy);
  746. else
  747. Rast_set_d_value(dy_ptr, (DCELL) dy, data_type);
  748. dy_ptr = G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  749. }
  750. if (dxx_fd <= 0 && dxy_fd <= 0 && dyy_fd <= 0 &&
  751. pcurv_fd <= 0 && tcurv_fd <= 0)
  752. continue;
  753. /* compute second order derivatives */
  754. s4 = *c1 + *c3 + *c7 + *c9 - *c5 * 8.;
  755. s5 = *c4 * 4. + *c6 * 4. - *c8 * 2. - *c2 * 2.;
  756. s6 = *c8 * 4. + *c2 * 4. - *c4 * 2. - *c6 * 2.;
  757. s3 = *c7 - *c9 + *c3 - *c1;
  758. dxx = -(s4 + s5) / ((3. / 32.) * H * H);
  759. dyy = -(s4 + s6) / ((3. / 32.) * V * V);
  760. dxy = -s3 / ((1. / 16.) * H * V);
  761. if (dxx_fd > 0) {
  762. if (out_type == CELL_TYPE)
  763. *((CELL *) dxx_ptr) = (CELL) (scik1 * dxx);
  764. else
  765. Rast_set_d_value(dxx_ptr, (DCELL) dxx, data_type);
  766. dxx_ptr = G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  767. }
  768. if (dyy_fd > 0) {
  769. if (out_type == CELL_TYPE)
  770. *((CELL *) dyy_ptr) = (CELL) (scik1 * dyy);
  771. else
  772. Rast_set_d_value(dyy_ptr, (DCELL) dyy, data_type);
  773. dyy_ptr = G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  774. }
  775. if (dxy_fd > 0) {
  776. if (out_type == CELL_TYPE)
  777. *((CELL *) dxy_ptr) = (CELL) (scik1 * dxy);
  778. else
  779. Rast_set_d_value(dxy_ptr, (DCELL) dxy, data_type);
  780. dxy_ptr = G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  781. }
  782. /* compute curvature */
  783. if (pcurv_fd <= 0 && tcurv_fd <= 0)
  784. continue;
  785. grad2 = key; /*dx2 + dy2 */
  786. grad = sqrt(grad2);
  787. if (grad <= gradmin) {
  788. pcurv = 0.;
  789. tcurv = 0.;
  790. }
  791. else {
  792. dnorm1 = sqrt(grad2 + 1.);
  793. dxy2 = 2. * dxy * dx * dy;
  794. dx2 = dx * dx;
  795. dy2 = dy * dy;
  796. pcurv = (dxx * dx2 + dxy2 + dyy * dy2) /
  797. (grad2 * dnorm1 * dnorm1 * dnorm1);
  798. tcurv = (dxx * dy2 - dxy2 + dyy * dx2) / (grad2 * dnorm1);
  799. if (c1min > pcurv)
  800. c1min = pcurv;
  801. if (c1max < pcurv)
  802. c1max = pcurv;
  803. if (c2min > tcurv)
  804. c2min = tcurv;
  805. if (c2max < tcurv)
  806. c2max = tcurv;
  807. }
  808. if (pcurv_fd > 0) {
  809. if (out_type == CELL_TYPE)
  810. *((CELL *) pcurv_ptr) = (CELL) (scik1 * pcurv);
  811. else
  812. Rast_set_d_value(pcurv_ptr, (DCELL) pcurv, data_type);
  813. pcurv_ptr =
  814. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  815. }
  816. if (tcurv_fd > 0) {
  817. if (out_type == CELL_TYPE)
  818. *((CELL *) tcurv_ptr) = (CELL) (scik1 * tcurv);
  819. else
  820. Rast_set_d_value(tcurv_ptr, (DCELL) tcurv, data_type);
  821. tcurv_ptr =
  822. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  823. }
  824. } /* column for loop */
  825. if (aspect_fd > 0)
  826. Rast_put_row(aspect_fd, asp_raster, data_type);
  827. if (slope_fd > 0)
  828. Rast_put_row(slope_fd, slp_raster, data_type);
  829. if (pcurv_fd > 0)
  830. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  831. if (tcurv_fd > 0)
  832. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  833. if (dx_fd > 0)
  834. Rast_put_row(dx_fd, dx_raster, data_type);
  835. if (dy_fd > 0)
  836. Rast_put_row(dy_fd, dy_raster, data_type);
  837. if (dxx_fd > 0)
  838. Rast_put_row(dxx_fd, dxx_raster, data_type);
  839. if (dyy_fd > 0)
  840. Rast_put_row(dyy_fd, dyy_raster, data_type);
  841. if (dxy_fd > 0)
  842. Rast_put_row(dxy_fd, dxy_raster, data_type);
  843. } /* row loop */
  844. G_percent(row, nrows, 2);
  845. Rast_close(elevation_fd);
  846. G_debug(1, "Creating support files...");
  847. G_verbose_message(_("Elevation products for mapset <%s> in <%s>"),
  848. G_mapset(), G_location());
  849. if (aspect_fd >= 0) {
  850. DCELL min, max;
  851. struct FPRange range;
  852. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  853. Rast_put_row(aspect_fd, asp_raster, data_type);
  854. Rast_close(aspect_fd);
  855. if (out_type != CELL_TYPE)
  856. Rast_quantize_fp_map_range(aspect_name, G_mapset(), 0., 360., 0,
  857. 360);
  858. Rast_read_cats(aspect_name, G_mapset(), &cats);
  859. Rast_set_cats_title
  860. ("Aspect counterclockwise in degrees from east", &cats);
  861. G_verbose_message(_("Min computed aspect %.4f, max computed aspect %.4f"),
  862. min_asp, max_asp);
  863. /* the categries quant intervals are 1.0 long, plus
  864. we are using reverse order so that the label looked up
  865. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  866. the one defile for i-1.5, i-.5 interval which is added later */
  867. for (i = ceil(max_asp); i >= 1; i--) {
  868. if (i == 360)
  869. sprintf(buf, "east");
  870. else if (i == 360)
  871. sprintf(buf, "east");
  872. else if (i == 45)
  873. sprintf(buf, "north ccw of east");
  874. else if (i == 90)
  875. sprintf(buf, "north");
  876. else if (i == 135)
  877. sprintf(buf, "north ccw of west");
  878. else if (i == 180)
  879. sprintf(buf, "west");
  880. else if (i == 225)
  881. sprintf(buf, "south ccw of west");
  882. else if (i == 270)
  883. sprintf(buf, "south");
  884. else if (i == 315)
  885. sprintf(buf, "south ccw of east");
  886. else
  887. sprintf(buf, "%d degree%s ccw from east", i,
  888. i == 1 ? "" : "s");
  889. if (data_type == CELL_TYPE) {
  890. Rast_set_c_cat((CELL *) &i, (CELL *) &i, buf, &cats);
  891. continue;
  892. }
  893. tmp1 = (double)i - .5;
  894. tmp2 = (double)i + .5;
  895. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  896. }
  897. if (data_type == CELL_TYPE) {
  898. cat = 0;
  899. Rast_set_c_cat(&cat, &cat, "no aspect", &cats);
  900. }
  901. else {
  902. tmp1 = 0.;
  903. tmp2 = .5;
  904. Rast_set_d_cat(&tmp1, &tmp2, "no aspect", &cats);
  905. }
  906. Rast_write_cats(aspect_name, &cats);
  907. Rast_free_cats(&cats);
  908. /* write colors for aspect file */
  909. Rast_init_colors(&colors);
  910. Rast_read_fp_range(aspect_name, G_mapset(), &range);
  911. Rast_get_fp_range_min_max(&range, &min, &max);
  912. Rast_make_aspect_fp_colors(&colors, min, max);
  913. Rast_write_colors(aspect_name, G_mapset(), &colors);
  914. /* writing history file */
  915. Rast_short_history(aspect_name, "raster", &hist);
  916. Rast_append_format_history(&hist, "aspect map elev = %s", elev_name);
  917. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  918. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  919. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  920. Rast_command_history(&hist);
  921. Rast_write_history(aspect_name, &hist);
  922. G_message(_("Aspect raster map <%s> complete"), aspect_name);
  923. }
  924. if (slope_fd >= 0) {
  925. /* colortable for slopes */
  926. CELL val1, val2;
  927. Rast_init_colors(&colors);
  928. val1 = 0;
  929. val2 = 2;
  930. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  931. val1 = 2;
  932. val2 = 5;
  933. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  934. val1 = 5;
  935. val2 = 10;
  936. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  937. val1 = 10;
  938. val2 = 15;
  939. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  940. val1 = 15;
  941. val2 = 30;
  942. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  943. val1 = 30;
  944. val2 = 50;
  945. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  946. val1 = 50;
  947. val2 = 90;
  948. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  949. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  950. Rast_put_row(slope_fd, slp_raster, data_type);
  951. Rast_close(slope_fd);
  952. if (out_type != CELL_TYPE) {
  953. /* INCR_BY_ONE
  954. if(deg)
  955. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 1, 91);
  956. else
  957. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp, max_slp,
  958. (CELL) min_slp + 1, (CELL) ceil(max_slp) + 1);
  959. */
  960. Rast_write_colors(slope_name, G_mapset(), &colors);
  961. if (deg)
  962. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 0,
  963. 90);
  964. else /* percent */
  965. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp,
  966. max_slp, (CELL) min_slp,
  967. (CELL) ceil(max_slp));
  968. }
  969. Rast_read_cats(slope_name, G_mapset(), &cats);
  970. if (deg)
  971. Rast_set_cats_title("slope in degrees", &cats);
  972. else if (perc)
  973. Rast_set_cats_title("percent slope", &cats);
  974. G_verbose_message(_("Min computed slope %.4f, max computed slope %.4f"),
  975. min_slp, max_slp);
  976. /* the categries quant intervals are 1.0 long, plus
  977. we are using reverse order so that the label looked up
  978. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  979. the one defined for i-1.5, i-.5 interval which is added later */
  980. for (i = ceil(max_slp); i > /* INC BY ONE >= */ 0; i--) {
  981. if (deg)
  982. sprintf(buf, "%d degree%s", i, i == 1 ? "" : "s");
  983. else if (perc)
  984. sprintf(buf, "%d percent", i);
  985. if (data_type == CELL_TYPE) {
  986. /* INCR_BY_ONE
  987. Rast_set_c_cat(i+1, buf, &cats);
  988. */
  989. Rast_set_c_cat(&i, &i, buf, &cats);
  990. continue;
  991. }
  992. /* INCR_BY_ONE
  993. tmp1 = (DCELL) i+.5;
  994. tmp2 = (DCELL) i+1.5;
  995. */
  996. tmp1 = (DCELL) i - .5;
  997. tmp2 = (DCELL) i + .5;
  998. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  999. }
  1000. if (data_type == CELL_TYPE) {
  1001. cat = 0;
  1002. Rast_set_c_cat(&cat, &cat, "zero slope", &cats);
  1003. }
  1004. /* INCR_BY_ONE
  1005. Rast_set_c_cat(0, "no data", &cats);
  1006. */
  1007. else {
  1008. tmp1 = 0;
  1009. tmp2 = 0.5;
  1010. Rast_set_d_cat(&tmp1, &tmp2, "zero slope", &cats);
  1011. }
  1012. /* INCR_BY_ONE
  1013. Rast_set_d_cat (&tmp1, &tmp1, "no data", &cats);
  1014. */
  1015. Rast_write_cats(slope_name, &cats);
  1016. /* writing history file */
  1017. Rast_short_history(slope_name, "raster", &hist);
  1018. Rast_append_format_history(&hist, "slope map elev = %s", elev_name);
  1019. Rast_append_format_history(&hist, "zfactor = %.2f format = %s", zfactor,
  1020. parm.slope_fmt->answer);
  1021. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1022. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1023. Rast_command_history(&hist);
  1024. Rast_write_history(slope_name, &hist);
  1025. G_message(_("Slope raster map <%s> complete"), slope_name);
  1026. }
  1027. /* colortable for curvatures */
  1028. if (pcurv_fd >= 0 || tcurv_fd >= 0) {
  1029. Rast_init_colors(&colors);
  1030. if (c1min < c2min)
  1031. dat1 = (FCELL) c1min;
  1032. else
  1033. dat1 = (FCELL) c2min;
  1034. dat2 = (FCELL) - 0.01;
  1035. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  1036. &dat2, 0, 0, 255, &colors);
  1037. dat1 = dat2;
  1038. dat2 = (FCELL) - 0.001;
  1039. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  1040. &dat2, 0, 127, 255, &colors);
  1041. dat1 = dat2;
  1042. dat2 = (FCELL) - 0.00001;
  1043. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  1044. &dat2, 0, 255, 255, &colors);
  1045. dat1 = dat2;
  1046. dat2 = (FCELL) 0.0;
  1047. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  1048. &dat2, 200, 255, 200, &colors);
  1049. dat1 = dat2;
  1050. dat2 = (FCELL) 0.00001;
  1051. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  1052. &dat2, 255, 255, 0, &colors);
  1053. dat1 = dat2;
  1054. dat2 = (FCELL) 0.001;
  1055. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  1056. &dat2, 255, 127, 0, &colors);
  1057. dat1 = dat2;
  1058. dat2 = (FCELL) 0.01;
  1059. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  1060. &dat2, 255, 0, 0, &colors);
  1061. dat1 = dat2;
  1062. if (c1max > c2max)
  1063. dat2 = (FCELL) c1max;
  1064. else
  1065. dat2 = (FCELL) c2max;
  1066. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  1067. &dat2, 255, 0, 200, &colors);
  1068. }
  1069. if (pcurv_fd >= 0) {
  1070. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  1071. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  1072. Rast_close(pcurv_fd);
  1073. Rast_write_colors(pcurv_name, G_mapset(), &colors);
  1074. if (out_type != CELL_TYPE)
  1075. Rast_round_fp_map(pcurv_name, G_mapset());
  1076. Rast_read_cats(pcurv_name, G_mapset(), &cats);
  1077. Rast_set_cats_title("profile curvature", &cats);
  1078. cat = 0;
  1079. Rast_set_c_cat(&cat, &cat, "no profile curve", &cats);
  1080. /* writing history file */
  1081. Rast_short_history(pcurv_name, "raster", &hist);
  1082. Rast_append_format_history(&hist, "profile curve map elev = %s", elev_name);
  1083. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1084. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1085. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1086. Rast_command_history(&hist);
  1087. Rast_write_history(pcurv_name, &hist);
  1088. G_message(_("Profile curve raster map <%s> complete"), pcurv_name);
  1089. }
  1090. if (tcurv_fd >= 0) {
  1091. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  1092. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  1093. Rast_close(tcurv_fd);
  1094. Rast_write_colors(tcurv_name, G_mapset(), &colors);
  1095. if (out_type != CELL_TYPE)
  1096. Rast_round_fp_map(tcurv_name, G_mapset());
  1097. Rast_read_cats(tcurv_name, G_mapset(), &cats);
  1098. Rast_set_cats_title("tangential curvature", &cats);
  1099. cat = 0;
  1100. Rast_set_c_cat(&cat, &cat, "no tangential curve", &cats);
  1101. /* writing history file */
  1102. Rast_short_history(tcurv_name, "raster", &hist);
  1103. Rast_append_format_history(&hist, "tangential curve map elev = %s", elev_name);
  1104. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1105. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1106. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1107. Rast_command_history(&hist);
  1108. Rast_write_history(tcurv_name, &hist);
  1109. G_message(_("Tangential curve raster map <%s> complete"), tcurv_name);
  1110. }
  1111. if (dx_fd >= 0) {
  1112. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  1113. Rast_put_row(dx_fd, dx_raster, data_type);
  1114. Rast_close(dx_fd);
  1115. if (out_type != CELL_TYPE)
  1116. Rast_round_fp_map(dx_name, G_mapset());
  1117. Rast_read_cats(dx_name, G_mapset(), &cats);
  1118. Rast_set_cats_title("E-W slope", &cats);
  1119. cat = 0;
  1120. Rast_set_c_cat(&cat, &cat, "no E-W slope", &cats);
  1121. /* writing history file */
  1122. Rast_short_history(dx_name, "raster", &hist);
  1123. Rast_append_format_history(&hist, "E-W slope map elev = %s", elev_name);
  1124. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1125. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1126. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1127. Rast_command_history(&hist);
  1128. Rast_write_history(dx_name, &hist);
  1129. G_message(_("E-W slope raster map <%s> complete"), dx_name);
  1130. }
  1131. if (dy_fd >= 0) {
  1132. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  1133. Rast_put_row(dy_fd, dy_raster, data_type);
  1134. Rast_close(dy_fd);
  1135. if (out_type != CELL_TYPE)
  1136. Rast_round_fp_map(dy_name, G_mapset());
  1137. Rast_read_cats(dy_name, G_mapset(), &cats);
  1138. Rast_set_cats_title("N-S slope", &cats);
  1139. cat = 0;
  1140. Rast_set_c_cat(&cat, &cat, "no N-S slope", &cats);
  1141. /* writing history file */
  1142. Rast_short_history(dy_name, "raster", &hist);
  1143. Rast_append_format_history(&hist, "N-S slope map elev = %s", elev_name);
  1144. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1145. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1146. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1147. Rast_command_history(&hist);
  1148. Rast_write_history(dy_name, &hist);
  1149. G_message(_("N-S slope raster map <%s> complete"), dy_name);
  1150. }
  1151. if (dxx_fd >= 0) {
  1152. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  1153. Rast_put_row(dxx_fd, dxx_raster, data_type);
  1154. Rast_close(dxx_fd);
  1155. if (out_type != CELL_TYPE)
  1156. Rast_round_fp_map(dxx_name, G_mapset());
  1157. Rast_read_cats(dxx_name, G_mapset(), &cats);
  1158. Rast_set_cats_title("DXX", &cats);
  1159. cat = 0;
  1160. Rast_set_c_cat(&cat, &cat, "DXX", &cats);
  1161. /* writing history file */
  1162. Rast_short_history(dxx_name, "raster", &hist);
  1163. Rast_append_format_history(&hist, "DXX map elev = %s", elev_name);
  1164. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1165. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1166. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1167. Rast_command_history(&hist);
  1168. Rast_write_history(dxx_name, &hist);
  1169. G_message(_("Dxx raster map <%s> complete"), dxx_name);
  1170. }
  1171. if (dyy_fd >= 0) {
  1172. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  1173. Rast_put_row(dyy_fd, dyy_raster, data_type);
  1174. Rast_close(dyy_fd);
  1175. if (out_type != CELL_TYPE)
  1176. Rast_round_fp_map(dyy_name, G_mapset());
  1177. Rast_read_cats(dyy_name, G_mapset(), &cats);
  1178. Rast_set_cats_title("DYY", &cats);
  1179. cat = 0;
  1180. Rast_set_c_cat(&cat, &cat, "DYY", &cats);
  1181. /* writing history file */
  1182. Rast_short_history(dyy_name, "raster", &hist);
  1183. Rast_append_format_history(&hist, "DYY map elev = %s", elev_name);
  1184. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1185. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1186. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1187. Rast_command_history(&hist);
  1188. Rast_write_history(dyy_name, &hist);
  1189. G_message(_("Dyy raster map <%s> complete"), dyy_name);
  1190. }
  1191. if (dxy_fd >= 0) {
  1192. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  1193. Rast_put_row(dxy_fd, dxy_raster, data_type);
  1194. Rast_close(dxy_fd);
  1195. if (out_type != CELL_TYPE)
  1196. Rast_round_fp_map(dxy_name, G_mapset());
  1197. Rast_read_cats(dxy_name, G_mapset(), &cats);
  1198. Rast_set_cats_title("DXY", &cats);
  1199. cat = 0;
  1200. Rast_set_c_cat(&cat, &cat, "DXY", &cats);
  1201. /* writing history file */
  1202. Rast_short_history(dxy_name, "raster", &hist);
  1203. Rast_append_format_history(&hist, "DXY map elev = %s", elev_name);
  1204. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1205. Rast_append_format_history(&hist, "min_slp_allowed = %f", min_slp_allowed);
  1206. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1207. Rast_command_history(&hist);
  1208. Rast_write_history(dxy_name, &hist);
  1209. G_message(_("Dxy raster map <%s> complete"), dxy_name);
  1210. }
  1211. exit(EXIT_SUCCESS);
  1212. }