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