main.c 42 KB

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