main.c 42 KB

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