main.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  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. /* convert aspect from CCW from East to CW from North
  52. * aspect for flat areas is set to -9999 */
  53. static double aspect_cw_n(double aspect)
  54. {
  55. /* aspect == 0: flat */
  56. if (aspect == 0)
  57. return -9999;
  58. /* no modulus because of fp values */
  59. aspect = (450 - aspect);
  60. if (aspect >= 360)
  61. aspect -= 360;
  62. return aspect;
  63. }
  64. int main(int argc, char *argv[])
  65. {
  66. struct Categories cats;
  67. int elevation_fd;
  68. int aspect_fd;
  69. int slope_fd;
  70. int pcurv_fd;
  71. int tcurv_fd;
  72. int dx_fd;
  73. int dy_fd;
  74. int dxx_fd;
  75. int dyy_fd;
  76. int dxy_fd;
  77. DCELL *elev_cell[3], *temp;
  78. DCELL *pc1, *pc2, *pc3;
  79. DCELL c1, c2, c3, c4, c5, c6, c7, c8, c9;
  80. DCELL tmp1, tmp2;
  81. FCELL dat1, dat2;
  82. CELL cat;
  83. void *asp_raster, *asp_ptr = NULL;
  84. void *slp_raster, *slp_ptr = NULL;
  85. void *pcurv_raster, *pcurv_ptr = NULL;
  86. void *tcurv_raster, *tcurv_ptr = NULL;
  87. void *dx_raster, *dx_ptr = NULL;
  88. void *dy_raster, *dy_ptr = NULL;
  89. void *dxx_raster, *dxx_ptr = NULL;
  90. void *dyy_raster, *dyy_ptr = NULL;
  91. void *dxy_raster, *dxy_ptr = NULL;
  92. int i;
  93. RASTER_MAP_TYPE out_type, data_type;
  94. int Wrap; /* global wraparound */
  95. struct Cell_head window, cellhd;
  96. struct History hist;
  97. struct Colors colors;
  98. const char *elev_name;
  99. const char *aspect_name;
  100. const char *slope_name;
  101. const char *pcurv_name;
  102. const char *tcurv_name;
  103. const char *dx_name;
  104. const char *dy_name;
  105. const char *dxx_name;
  106. const char *dyy_name;
  107. const char *dxy_name;
  108. char buf[300];
  109. int nrows, row;
  110. int ncols, col;
  111. double north, east, south, west, ns_med;
  112. double radians_to_degrees;
  113. double degrees_to_radians;
  114. double H, V;
  115. double dx; /* partial derivative in ew direction */
  116. double dy; /* partial derivative in ns direction */
  117. double dxx, dxy, dyy;
  118. double s3, s4, s5, s6;
  119. double pcurv, tcurv;
  120. double scik1 = 100000.;
  121. double zfactor;
  122. double factor;
  123. double aspect, min_asp = 360., max_asp = 0.;
  124. double dnorm1, dx2, dy2, grad2, grad, dxy2;
  125. double gradmin = 0.001;
  126. double c1min = 0., c1max = 0., c2min = 0., c2max = 0.;
  127. double answer[92];
  128. double degrees;
  129. double tan_ans;
  130. double key;
  131. double slp_in_perc, slp_in_deg;
  132. double min_slp = 900., max_slp = 0., min_slope;
  133. int low, hi, test = 0;
  134. int deg = 0;
  135. int perc = 0;
  136. char *slope_fmt;
  137. struct GModule *module;
  138. struct
  139. {
  140. struct Option *elevation, *slope_fmt, *slope, *aspect, *pcurv, *tcurv,
  141. *zfactor, *min_slope, *out_precision,
  142. *dx, *dy, *dxx, *dyy, *dxy;
  143. } parm;
  144. struct
  145. {
  146. struct Flag *a, *n, *e;
  147. } flag;
  148. int compute_at_edges;
  149. G_gisinit(argv[0]);
  150. module = G_define_module();
  151. G_add_keyword(_("raster"));
  152. G_add_keyword(_("terrain"));
  153. G_add_keyword(_("aspect"));
  154. G_add_keyword(_("slope"));
  155. G_add_keyword(_("curvature"));
  156. module->label = _("Generates raster maps of slope, aspect, curvatures and "
  157. "partial derivatives from an elevation raster map.");
  158. module->description = _("Aspect is calculated counterclockwise from east.");
  159. parm.elevation = G_define_standard_option(G_OPT_R_ELEV);
  160. parm.slope = G_define_standard_option(G_OPT_R_OUTPUT);
  161. parm.slope->key = "slope";
  162. parm.slope->required = NO;
  163. parm.slope->description = _("Name for output slope raster map");
  164. parm.slope->guisection = _("Outputs");
  165. parm.aspect = G_define_standard_option(G_OPT_R_OUTPUT);
  166. parm.aspect->key = "aspect";
  167. parm.aspect->required = NO;
  168. parm.aspect->description = _("Name for output aspect raster map");
  169. parm.aspect->guisection = _("Outputs");
  170. parm.slope_fmt = G_define_option();
  171. parm.slope_fmt->key = "format";
  172. parm.slope_fmt->type = TYPE_STRING;
  173. parm.slope_fmt->required = NO;
  174. parm.slope_fmt->answer = "degrees";
  175. parm.slope_fmt->options = "degrees,percent";
  176. parm.slope_fmt->description = _("Format for reporting the slope");
  177. parm.slope_fmt->guisection = _("Settings");
  178. parm.out_precision = G_define_standard_option(G_OPT_R_TYPE);
  179. parm.out_precision->key = "precision";
  180. parm.out_precision->required = NO;
  181. parm.out_precision->label =
  182. _("Type of output aspect and slope maps");
  183. parm.out_precision->answer = "FCELL";
  184. parm.out_precision->guisection = _("Settings");
  185. parm.pcurv = G_define_standard_option(G_OPT_R_OUTPUT);
  186. parm.pcurv->key = "pcurvature";
  187. parm.pcurv->required = NO;
  188. parm.pcurv->description =
  189. _("Name for output profile curvature raster map");
  190. parm.pcurv->guisection = _("Outputs");
  191. parm.tcurv = G_define_standard_option(G_OPT_R_OUTPUT);
  192. parm.tcurv->key = "tcurvature";
  193. parm.tcurv->required = NO;
  194. parm.tcurv->description =
  195. _("Name for output tangential curvature raster map");
  196. parm.tcurv->guisection = _("Outputs");
  197. parm.dx = G_define_standard_option(G_OPT_R_OUTPUT);
  198. parm.dx->key = "dx";
  199. parm.dx->required = NO;
  200. parm.dx->description =
  201. _("Name for output first order partial derivative dx (E-W slope) raster map");
  202. parm.dx->guisection = _("Outputs");
  203. parm.dy = G_define_standard_option(G_OPT_R_OUTPUT);
  204. parm.dy->key = "dy";
  205. parm.dy->required = NO;
  206. parm.dy->description =
  207. _("Name for output first order partial derivative dy (N-S slope) raster map");
  208. parm.dy->guisection = _("Outputs");
  209. parm.dxx = G_define_standard_option(G_OPT_R_OUTPUT);
  210. parm.dxx->key = "dxx";
  211. parm.dxx->required = NO;
  212. parm.dxx->description =
  213. _("Name for output second order partial derivative dxx raster map");
  214. parm.dxx->guisection = _("Outputs");
  215. parm.dyy = G_define_standard_option(G_OPT_R_OUTPUT);
  216. parm.dyy->key = "dyy";
  217. parm.dyy->required = NO;
  218. parm.dyy->description =
  219. _("Name for output second order partial derivative dyy raster map");
  220. parm.dyy->guisection = _("Outputs");
  221. parm.dxy = G_define_standard_option(G_OPT_R_OUTPUT);
  222. parm.dxy->key = "dxy";
  223. parm.dxy->required = NO;
  224. parm.dxy->description =
  225. _("Name for output second order partial derivative dxy raster map");
  226. parm.dxy->guisection = _("Outputs");
  227. parm.zfactor = G_define_option();
  228. parm.zfactor->key = "zscale";
  229. parm.zfactor->description =
  230. _("Multiplicative factor to convert elevation units to horizontal units");
  231. parm.zfactor->type = TYPE_DOUBLE;
  232. parm.zfactor->required = NO;
  233. parm.zfactor->answer = "1.0";
  234. parm.zfactor->guisection = _("Settings");
  235. parm.min_slope = G_define_option();
  236. parm.min_slope->key = "min_slope";
  237. parm.min_slope->description =
  238. _("Minimum slope value (in percent) for which aspect is computed");
  239. parm.min_slope->type = TYPE_DOUBLE;
  240. parm.min_slope->required = NO;
  241. parm.min_slope->answer = "0.0";
  242. parm.min_slope->guisection = _("Settings");
  243. flag.a = G_define_flag();
  244. flag.a->key = 'a';
  245. flag.a->description =
  246. _("Do not align the current region to the raster elevation map");
  247. flag.a->guisection = _("Settings");
  248. flag.e = G_define_flag();
  249. flag.e->key = 'e';
  250. flag.e->description =
  251. _("Compute output at edges and near NULL values");
  252. flag.e->guisection = _("Settings");
  253. flag.n = G_define_flag();
  254. flag.n->key = 'n';
  255. flag.n->label =
  256. _("Create aspect as degrees clockwise from North (azimuth), with flat = -9999");
  257. flag.n->description =
  258. _("Default: degrees counter-clockwise from East, with flat = 0");
  259. flag.n->guisection = _("Settings");
  260. if (G_parser(argc, argv))
  261. exit(EXIT_FAILURE);
  262. radians_to_degrees = 180.0 / M_PI;
  263. degrees_to_radians = M_PI / 180.0;
  264. compute_at_edges = flag.e->answer;
  265. /* INC BY ONE
  266. answer[0] = 0.0;
  267. answer[91] = 15000.0;
  268. for (i = 1; i < 91; i++)
  269. {
  270. degrees = i - .5;
  271. tan_ans = tan ( degrees / radians_to_degrees );
  272. answer[i] = tan_ans * tan_ans;
  273. }
  274. */
  275. answer[0] = 0.0;
  276. answer[90] = 15000.0;
  277. for (i = 0; i < 90; i++) {
  278. degrees = i + .5;
  279. tan_ans = tan(degrees / radians_to_degrees);
  280. answer[i] = tan_ans * tan_ans;
  281. }
  282. elev_name = parm.elevation->answer;
  283. slope_name = parm.slope->answer;
  284. aspect_name = parm.aspect->answer;
  285. pcurv_name = parm.pcurv->answer;
  286. tcurv_name = parm.tcurv->answer;
  287. dx_name = parm.dx->answer;
  288. dy_name = parm.dy->answer;
  289. dxx_name = parm.dxx->answer;
  290. dyy_name = parm.dyy->answer;
  291. dxy_name = parm.dxy->answer;
  292. G_check_input_output_name(elev_name, slope_name, G_FATAL_EXIT);
  293. G_check_input_output_name(elev_name, aspect_name, G_FATAL_EXIT);
  294. G_check_input_output_name(elev_name, pcurv_name, G_FATAL_EXIT);
  295. G_check_input_output_name(elev_name, tcurv_name, G_FATAL_EXIT);
  296. G_check_input_output_name(elev_name, dx_name, G_FATAL_EXIT);
  297. G_check_input_output_name(elev_name, dy_name, G_FATAL_EXIT);
  298. G_check_input_output_name(elev_name, dxx_name, G_FATAL_EXIT);
  299. G_check_input_output_name(elev_name, dyy_name, G_FATAL_EXIT);
  300. G_check_input_output_name(elev_name, dxy_name, G_FATAL_EXIT);
  301. if (sscanf(parm.zfactor->answer, "%lf", &zfactor) != 1 || zfactor <= 0.0) {
  302. G_fatal_error(_("%s=%s - must be a positive number"),
  303. parm.zfactor->key, parm.zfactor->answer);
  304. }
  305. if (sscanf(parm.min_slope->answer, "%lf", &min_slope) != 1 ||
  306. min_slope < 0.0) {
  307. G_fatal_error(_("%s=%s - must be a non-negative number"),
  308. parm.min_slope->key,
  309. parm.min_slope->answer);
  310. }
  311. slope_fmt = parm.slope_fmt->answer;
  312. if (strcmp(slope_fmt, "percent") == 0)
  313. perc = 1;
  314. else if (strcmp(slope_fmt, "degrees") == 0)
  315. deg = 1;
  316. if (slope_name == NULL && aspect_name == NULL
  317. && pcurv_name == NULL && tcurv_name == NULL
  318. && dx_name == NULL && dy_name == NULL
  319. && dxx_name == NULL && dyy_name == NULL && dxy_name == NULL) {
  320. G_fatal_error(_("You must specify at least one of the parameters: "
  321. "<%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s>, <%s> or <%s>"),
  322. parm.slope->key, parm.aspect->key, parm.pcurv->key,
  323. parm.tcurv->key, parm.dx->key, parm.dy->key,
  324. parm.dxx->key, parm.dyy->key, parm.dxy->key);
  325. }
  326. /* set the window from the header for the elevation file */
  327. if (!flag.a->answer) {
  328. G_get_window(&window);
  329. Rast_get_cellhd(elev_name, "", &cellhd);
  330. Rast_align_window(&window, &cellhd);
  331. Rast_set_window(&window);
  332. }
  333. if (strcmp(parm.out_precision->answer, "DCELL") == 0)
  334. out_type = DCELL_TYPE;
  335. else if (strcmp(parm.out_precision->answer, "FCELL") == 0)
  336. out_type = FCELL_TYPE;
  337. else if (strcmp(parm.out_precision->answer, "CELL") == 0)
  338. out_type = CELL_TYPE;
  339. else
  340. G_fatal_error(_("Wrong raster type: %s"), parm.out_precision->answer);
  341. data_type = out_type;
  342. /* data type is the type of data being processed,
  343. out_type is type of map being created */
  344. /* ? why not use Rast_map_type() then ? */
  345. G_get_set_window(&window);
  346. nrows = Rast_window_rows();
  347. ncols = Rast_window_cols();
  348. if (((window.west == (window.east - 360.))
  349. || (window.east == (window.west - 360.))) &&
  350. (G_projection() == PROJECTION_LL)) {
  351. Wrap = 1;
  352. ncols += 2;
  353. }
  354. else
  355. Wrap = 0;
  356. /* H = window.ew_res * 4 * 2/ zfactor; *//* horizontal (east-west) run
  357. times 4 for weighted difference */
  358. /* V = window.ns_res * 4 * 2/ zfactor; *//* vertical (north-south) run
  359. times 4 for weighted difference */
  360. /* we don't assume vertical units to be meters any more */
  361. factor = G_database_units_to_meters_factor();
  362. if (factor != 1.0 && zfactor != 1.0)
  363. G_warning(_("r.slope.aspect does not convert horizontal units "
  364. "to meters in this version, see manual page."));
  365. G_begin_distance_calculations();
  366. north = Rast_row_to_northing(0.5, &window);
  367. ns_med = Rast_row_to_northing(1.5, &window);
  368. south = Rast_row_to_northing(2.5, &window);
  369. east = Rast_col_to_easting(2.5, &window);
  370. west = Rast_col_to_easting(0.5, &window);
  371. V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
  372. H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * zfactor);
  373. /* ____________________________
  374. |c1 |c2 |c3 |
  375. | | | |
  376. | | north | |
  377. | | | |
  378. |________|________|________|
  379. |c4 |c5 |c6 |
  380. | | | |
  381. | east | ns_med | west |
  382. | | | |
  383. |________|________|________|
  384. |c7 |c8 |c9 |
  385. | | | |
  386. | | south | |
  387. | | | |
  388. |________|________|________|
  389. */
  390. /* open the elevation file for reading */
  391. elevation_fd = Rast_open_old(elev_name, "");
  392. elev_cell[0] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  393. Rast_set_d_null_value(elev_cell[0], ncols);
  394. elev_cell[1] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  395. Rast_set_d_null_value(elev_cell[1], ncols);
  396. elev_cell[2] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  397. Rast_set_d_null_value(elev_cell[2], ncols);
  398. if (slope_name != NULL) {
  399. slope_fd = Rast_open_new(slope_name, out_type);
  400. slp_raster = Rast_allocate_buf(data_type);
  401. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  402. Rast_put_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 = Rast_open_new(aspect_name, out_type);
  410. asp_raster = Rast_allocate_buf(data_type);
  411. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  412. Rast_put_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 = Rast_open_new(pcurv_name, out_type);
  420. pcurv_raster = Rast_allocate_buf(data_type);
  421. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  422. Rast_put_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 = Rast_open_new(tcurv_name, out_type);
  430. tcurv_raster = Rast_allocate_buf(data_type);
  431. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  432. Rast_put_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 = Rast_open_new(dx_name, out_type);
  440. dx_raster = Rast_allocate_buf(data_type);
  441. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  442. Rast_put_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 = Rast_open_new(dy_name, out_type);
  450. dy_raster = Rast_allocate_buf(data_type);
  451. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  452. Rast_put_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 = Rast_open_new(dxx_name, out_type);
  460. dxx_raster = Rast_allocate_buf(data_type);
  461. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  462. Rast_put_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 = Rast_open_new(dyy_name, out_type);
  470. dyy_raster = Rast_allocate_buf(data_type);
  471. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  472. Rast_put_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 = Rast_open_new(dxy_name, out_type);
  480. dxy_raster = Rast_allocate_buf(data_type);
  481. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  482. Rast_put_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. Rast_get_d_row_nomask(elevation_fd, elev_cell[1] + 1, 0);
  493. elev_cell[1][0] = elev_cell[1][Rast_window_cols() - 1];
  494. elev_cell[1][Rast_window_cols() + 1] = elev_cell[1][2];
  495. }
  496. else
  497. Rast_get_d_row_nomask(elevation_fd, elev_cell[1], 0);
  498. if (Wrap) {
  499. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, 1);
  500. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  501. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  502. }
  503. else
  504. Rast_get_d_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 = Rast_row_to_northing((row - 2 + 0.5), &window);
  510. ns_med = Rast_row_to_northing((row - 1 + 0.5), &window);
  511. south = Rast_row_to_northing((row + 0.5), &window);
  512. east = Rast_col_to_easting(2.5, &window);
  513. west = Rast_col_to_easting(0.5, &window);
  514. V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
  515. H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * 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. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, row);
  541. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  542. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  543. }
  544. else
  545. Rast_get_d_row_nomask(elevation_fd, elev_cell[2], row);
  546. pc1 = elev_cell[0];
  547. pc2 = elev_cell[1];
  548. pc3 = elev_cell[2];
  549. if (aspect_fd >= 0) {
  550. if (Wrap)
  551. asp_ptr = asp_raster;
  552. else
  553. asp_ptr =
  554. G_incr_void_ptr(asp_raster, Rast_cell_size(data_type));
  555. }
  556. if (slope_fd >= 0) {
  557. if (Wrap)
  558. slp_ptr = slp_raster;
  559. else
  560. slp_ptr =
  561. G_incr_void_ptr(slp_raster, Rast_cell_size(data_type));
  562. }
  563. if (pcurv_fd >= 0) {
  564. if (Wrap)
  565. pcurv_ptr = pcurv_raster;
  566. else
  567. pcurv_ptr =
  568. G_incr_void_ptr(pcurv_raster, Rast_cell_size(data_type));
  569. }
  570. if (tcurv_fd >= 0) {
  571. if (Wrap)
  572. tcurv_ptr = tcurv_raster;
  573. else
  574. tcurv_ptr =
  575. G_incr_void_ptr(tcurv_raster, Rast_cell_size(data_type));
  576. }
  577. if (dx_fd >= 0) {
  578. if (Wrap)
  579. dx_ptr = dx_raster;
  580. else
  581. dx_ptr = G_incr_void_ptr(dx_raster, Rast_cell_size(data_type));
  582. }
  583. if (dy_fd >= 0) {
  584. if (Wrap)
  585. dy_ptr = dy_raster;
  586. else
  587. dy_ptr = G_incr_void_ptr(dy_raster, Rast_cell_size(data_type));
  588. }
  589. if (dxx_fd >= 0) {
  590. if (Wrap)
  591. dxx_ptr = dxx_raster;
  592. else
  593. dxx_ptr =
  594. G_incr_void_ptr(dxx_raster, Rast_cell_size(data_type));
  595. }
  596. if (dyy_fd >= 0) {
  597. if (Wrap)
  598. dyy_ptr = dyy_raster;
  599. else
  600. dyy_ptr =
  601. G_incr_void_ptr(dyy_raster, Rast_cell_size(data_type));
  602. }
  603. if (dxy_fd >= 0) {
  604. if (Wrap)
  605. dxy_ptr = dxy_raster;
  606. else
  607. dxy_ptr =
  608. G_incr_void_ptr(dxy_raster, Rast_cell_size(data_type));
  609. }
  610. /*skip first cell of the row */
  611. for (col = ncols - 2; col-- > 0; pc1++, pc2++, pc3++) {
  612. c1 = *pc1;
  613. c2 = *(pc1 + 1);
  614. c3 = *(pc1 + 2);
  615. c4 = *pc2;
  616. c5 = *(pc2 + 1);
  617. c6 = *(pc2 + 2);
  618. c7 = *pc3;
  619. c8 = *(pc3 + 1);
  620. c9 = *(pc3 + 2);
  621. /* DEBUG:
  622. fprintf(stdout, "\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n",
  623. c1, c2, c3, c4, c5, c6, c7, c8, c9);
  624. */
  625. if (Rast_is_d_null_value(&c5) || (!compute_at_edges &&
  626. (Rast_is_d_null_value(&c1) || Rast_is_d_null_value(&c2) ||
  627. Rast_is_d_null_value(&c3) || Rast_is_d_null_value(&c4) ||
  628. Rast_is_d_null_value(&c6) || Rast_is_d_null_value(&c7) ||
  629. Rast_is_d_null_value(&c8) || Rast_is_d_null_value(&c9)))) {
  630. if (slope_fd > 0) {
  631. Rast_set_null_value(slp_ptr, 1, data_type);
  632. slp_ptr =
  633. G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  634. }
  635. if (aspect_fd > 0) {
  636. Rast_set_null_value(asp_ptr, 1, data_type);
  637. asp_ptr =
  638. G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  639. }
  640. if (pcurv_fd > 0) {
  641. Rast_set_null_value(pcurv_ptr, 1, data_type);
  642. pcurv_ptr =
  643. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  644. }
  645. if (tcurv_fd > 0) {
  646. Rast_set_null_value(tcurv_ptr, 1, data_type);
  647. tcurv_ptr =
  648. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  649. }
  650. if (dx_fd > 0) {
  651. Rast_set_null_value(dx_ptr, 1, data_type);
  652. dx_ptr =
  653. G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  654. }
  655. if (dy_fd > 0) {
  656. Rast_set_null_value(dy_ptr, 1, data_type);
  657. dy_ptr =
  658. G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  659. }
  660. if (dxx_fd > 0) {
  661. Rast_set_null_value(dxx_ptr, 1, data_type);
  662. dxx_ptr =
  663. G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  664. }
  665. if (dyy_fd > 0) {
  666. Rast_set_null_value(dyy_ptr, 1, data_type);
  667. dyy_ptr =
  668. G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  669. }
  670. if (dxy_fd > 0) {
  671. Rast_set_null_value(dxy_ptr, 1, data_type);
  672. dxy_ptr =
  673. G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  674. }
  675. continue;
  676. } /* no data */
  677. if (compute_at_edges) {
  678. /* same method like ComputeVal in gdaldem_lib.cpp */
  679. if (Rast_is_d_null_value(&c1))
  680. c1 = c5;
  681. if (Rast_is_d_null_value(&c2))
  682. c2 = c5;
  683. if (Rast_is_d_null_value(&c3))
  684. c3 = c5;
  685. if (Rast_is_d_null_value(&c4))
  686. c4 = c5;
  687. if (Rast_is_d_null_value(&c6))
  688. c6 = c5;
  689. if (Rast_is_d_null_value(&c7))
  690. c7 = c5;
  691. if (Rast_is_d_null_value(&c8))
  692. c8 = c5;
  693. if (Rast_is_d_null_value(&c9))
  694. c9 = c5;
  695. }
  696. dx = ((c1 + c4 + c4 + c7) - (c3 + c6 + c6 + c9)) / H;
  697. dy = ((c7 + c8 + c8 + c9) - (c1 + c2 + c2 + c3)) / V;
  698. /* compute topographic parameters */
  699. key = dx * dx + dy * dy;
  700. slp_in_perc = 100 * sqrt(key);
  701. slp_in_deg = atan(sqrt(key)) * radians_to_degrees;
  702. /* now update min and max */
  703. if (deg) {
  704. if (min_slp > slp_in_deg)
  705. min_slp = slp_in_deg;
  706. if (max_slp < slp_in_deg)
  707. max_slp = slp_in_deg;
  708. }
  709. else {
  710. if (min_slp > slp_in_perc)
  711. min_slp = slp_in_perc;
  712. if (max_slp < slp_in_perc)
  713. max_slp = slp_in_perc;
  714. }
  715. if (slp_in_perc < min_slope)
  716. slp_in_perc = 0.;
  717. if (deg && out_type == CELL_TYPE) {
  718. /* INC BY ONE
  719. low = 1;
  720. hi = 91;
  721. */
  722. low = 0;
  723. hi = 90;
  724. test = 20;
  725. while (hi >= low) {
  726. if (key >= answer[test])
  727. low = test + 1;
  728. else if (key < answer[test - 1])
  729. hi = test - 1;
  730. else
  731. break;
  732. test = (low + hi) / 2;
  733. }
  734. }
  735. else if (perc && out_type == CELL_TYPE)
  736. /* INCR_BY_ONE */
  737. /* test = slp_in_perc + 1.5; *//* All the slope categories are
  738. incremented by 1 */
  739. test = slp_in_perc + .5;
  740. if (slope_fd > 0) {
  741. if (data_type == CELL_TYPE)
  742. *((CELL *) slp_ptr) = (CELL) test;
  743. else {
  744. if (deg)
  745. Rast_set_d_value(slp_ptr,
  746. (DCELL) slp_in_deg, data_type);
  747. else
  748. Rast_set_d_value(slp_ptr,
  749. (DCELL) slp_in_perc, data_type);
  750. }
  751. slp_ptr = G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  752. } /* computing slope */
  753. if (aspect_fd > 0) {
  754. double aspect_flat = 0.;
  755. if (slp_in_perc == 0.)
  756. aspect = 0.;
  757. else if (dx == 0) {
  758. if (dy > 0)
  759. aspect = 90.;
  760. else
  761. aspect = 270.;
  762. }
  763. else {
  764. aspect = (atan2(dy, dx) / degrees_to_radians);
  765. if (aspect <= 0.)
  766. aspect = 360. + aspect;
  767. }
  768. if (flag.n->answer) {
  769. aspect_flat = -9999;
  770. aspect = aspect_cw_n(aspect);
  771. }
  772. if (out_type == CELL_TYPE) {
  773. if (aspect > 0 && aspect < 0.5)
  774. aspect = 360;
  775. *((CELL *) asp_ptr) = (CELL) (aspect + .5);
  776. }
  777. else
  778. Rast_set_d_value(asp_ptr,
  779. (DCELL) aspect, data_type);
  780. asp_ptr = G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  781. /* now update min and max */
  782. if (aspect > aspect_flat && min_asp > aspect)
  783. min_asp = aspect;
  784. if (max_asp < aspect)
  785. max_asp = aspect;
  786. } /* computing aspect */
  787. if (dx_fd > 0) {
  788. if (out_type == CELL_TYPE)
  789. *((CELL *) dx_ptr) = (CELL) (scik1 * dx);
  790. else
  791. Rast_set_d_value(dx_ptr, (DCELL) dx, data_type);
  792. dx_ptr = G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  793. }
  794. if (dy_fd > 0) {
  795. if (out_type == CELL_TYPE)
  796. *((CELL *) dy_ptr) = (CELL) (scik1 * dy);
  797. else
  798. Rast_set_d_value(dy_ptr, (DCELL) dy, data_type);
  799. dy_ptr = G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  800. }
  801. if (dxx_fd <= 0 && dxy_fd <= 0 && dyy_fd <= 0 &&
  802. pcurv_fd <= 0 && tcurv_fd <= 0)
  803. continue;
  804. /* compute second order derivatives */
  805. s4 = c1 + c3 + c7 + c9 - c5 * 8.;
  806. s5 = c4 * 4. + c6 * 4. - c8 * 2. - c2 * 2.;
  807. s6 = c8 * 4. + c2 * 4. - c4 * 2. - c6 * 2.;
  808. s3 = c7 - c9 + c3 - c1;
  809. dxx = -(s4 + s5) / ((3. / 32.) * H * H);
  810. dyy = -(s4 + s6) / ((3. / 32.) * V * V);
  811. dxy = -s3 / ((1. / 16.) * H * V);
  812. if (dxx_fd > 0) {
  813. if (out_type == CELL_TYPE)
  814. *((CELL *) dxx_ptr) = (CELL) (scik1 * dxx);
  815. else
  816. Rast_set_d_value(dxx_ptr, (DCELL) dxx, data_type);
  817. dxx_ptr = G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  818. }
  819. if (dyy_fd > 0) {
  820. if (out_type == CELL_TYPE)
  821. *((CELL *) dyy_ptr) = (CELL) (scik1 * dyy);
  822. else
  823. Rast_set_d_value(dyy_ptr, (DCELL) dyy, data_type);
  824. dyy_ptr = G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  825. }
  826. if (dxy_fd > 0) {
  827. if (out_type == CELL_TYPE)
  828. *((CELL *) dxy_ptr) = (CELL) (scik1 * dxy);
  829. else
  830. Rast_set_d_value(dxy_ptr, (DCELL) dxy, data_type);
  831. dxy_ptr = G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  832. }
  833. /* compute curvature */
  834. if (pcurv_fd <= 0 && tcurv_fd <= 0)
  835. continue;
  836. grad2 = key; /*dx2 + dy2 */
  837. grad = sqrt(grad2);
  838. if (grad <= gradmin) {
  839. pcurv = 0.;
  840. tcurv = 0.;
  841. }
  842. else {
  843. dnorm1 = sqrt(grad2 + 1.);
  844. dxy2 = 2. * dxy * dx * dy;
  845. dx2 = dx * dx;
  846. dy2 = dy * dy;
  847. pcurv = (dxx * dx2 + dxy2 + dyy * dy2) /
  848. (grad2 * dnorm1 * dnorm1 * dnorm1);
  849. tcurv = (dxx * dy2 - dxy2 + dyy * dx2) / (grad2 * dnorm1);
  850. if (c1min > pcurv)
  851. c1min = pcurv;
  852. if (c1max < pcurv)
  853. c1max = pcurv;
  854. if (c2min > tcurv)
  855. c2min = tcurv;
  856. if (c2max < tcurv)
  857. c2max = tcurv;
  858. }
  859. if (pcurv_fd > 0) {
  860. if (out_type == CELL_TYPE)
  861. *((CELL *) pcurv_ptr) = (CELL) (scik1 * pcurv);
  862. else
  863. Rast_set_d_value(pcurv_ptr, (DCELL) pcurv, data_type);
  864. pcurv_ptr =
  865. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  866. }
  867. if (tcurv_fd > 0) {
  868. if (out_type == CELL_TYPE)
  869. *((CELL *) tcurv_ptr) = (CELL) (scik1 * tcurv);
  870. else
  871. Rast_set_d_value(tcurv_ptr, (DCELL) tcurv, data_type);
  872. tcurv_ptr =
  873. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  874. }
  875. } /* column for loop */
  876. if (aspect_fd > 0)
  877. Rast_put_row(aspect_fd, asp_raster, data_type);
  878. if (slope_fd > 0)
  879. Rast_put_row(slope_fd, slp_raster, data_type);
  880. if (pcurv_fd > 0)
  881. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  882. if (tcurv_fd > 0)
  883. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  884. if (dx_fd > 0)
  885. Rast_put_row(dx_fd, dx_raster, data_type);
  886. if (dy_fd > 0)
  887. Rast_put_row(dy_fd, dy_raster, data_type);
  888. if (dxx_fd > 0)
  889. Rast_put_row(dxx_fd, dxx_raster, data_type);
  890. if (dyy_fd > 0)
  891. Rast_put_row(dyy_fd, dyy_raster, data_type);
  892. if (dxy_fd > 0)
  893. Rast_put_row(dxy_fd, dxy_raster, data_type);
  894. } /* row loop */
  895. G_percent(row, nrows, 2);
  896. Rast_close(elevation_fd);
  897. G_debug(1, "Creating support files...");
  898. G_verbose_message(_("Elevation products for mapset <%s> in <%s>"),
  899. G_mapset(), G_location());
  900. if (aspect_fd >= 0) {
  901. DCELL min, max;
  902. struct FPRange range;
  903. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  904. Rast_put_row(aspect_fd, asp_raster, data_type);
  905. Rast_close(aspect_fd);
  906. if (out_type != CELL_TYPE)
  907. Rast_quantize_fp_map_range(aspect_name, G_mapset(), 0., 360., 0,
  908. 360);
  909. Rast_read_cats(aspect_name, G_mapset(), &cats);
  910. Rast_set_cats_title
  911. ("Aspect counterclockwise in degrees from east", &cats);
  912. G_verbose_message(_("Min computed aspect %.4f, max computed aspect %.4f"),
  913. min_asp, max_asp);
  914. /* the categries quant intervals are 1.0 long, plus
  915. we are using reverse order so that the label looked up
  916. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  917. the one defile for i-1.5, i-.5 interval which is added later */
  918. if (!flag.n->answer) {
  919. for (i = ceil(max_asp); i >= 1; i--) {
  920. if (i == 360)
  921. sprintf(buf, "east");
  922. else if (i == 45)
  923. sprintf(buf, "north ccw of east");
  924. else if (i == 90)
  925. sprintf(buf, "north");
  926. else if (i == 135)
  927. sprintf(buf, "north ccw of west");
  928. else if (i == 180)
  929. sprintf(buf, "west");
  930. else if (i == 225)
  931. sprintf(buf, "south ccw of west");
  932. else if (i == 270)
  933. sprintf(buf, "south");
  934. else if (i == 315)
  935. sprintf(buf, "south ccw of east");
  936. else
  937. sprintf(buf, "%d degree%s ccw from east", i,
  938. i == 1 ? "" : "s");
  939. if (data_type == CELL_TYPE) {
  940. Rast_set_c_cat((CELL *) &i, (CELL *) &i, buf, &cats);
  941. continue;
  942. }
  943. tmp1 = (double)i - .5;
  944. tmp2 = (double)i + .5;
  945. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  946. }
  947. if (data_type == CELL_TYPE) {
  948. cat = 0;
  949. Rast_set_c_cat(&cat, &cat, "no aspect", &cats);
  950. }
  951. else {
  952. tmp1 = 0;
  953. Rast_set_d_cat(&tmp1, &tmp1, "no aspect", &cats);
  954. }
  955. }
  956. else {
  957. for (i = ceil(max_asp); i >= 1; i--) {
  958. if (i == 0 && i == 360)
  959. sprintf(buf, "north");
  960. else if (i == 45)
  961. sprintf(buf, "north-east");
  962. else if (i == 90)
  963. sprintf(buf, "east");
  964. else if (i == 135)
  965. sprintf(buf, "south-east");
  966. else if (i == 180)
  967. sprintf(buf, "south");
  968. else if (i == 225)
  969. sprintf(buf, "south-west");
  970. else if (i == 270)
  971. sprintf(buf, "west");
  972. else if (i == 315)
  973. sprintf(buf, "north-west");
  974. else
  975. sprintf(buf, "%d degree%s cw from north", i,
  976. i == 1 ? "" : "s");
  977. if (data_type == CELL_TYPE) {
  978. Rast_set_c_cat((CELL *) &i, (CELL *) &i, buf, &cats);
  979. continue;
  980. }
  981. tmp1 = (double)i - .5;
  982. tmp2 = (double)i + .5;
  983. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  984. }
  985. if (data_type == CELL_TYPE) {
  986. cat = -9999;
  987. Rast_set_c_cat(&cat, &cat, "no aspect", &cats);
  988. }
  989. else {
  990. tmp1 = -9999;
  991. Rast_set_d_cat(&tmp1, &tmp1, "no aspect", &cats);
  992. }
  993. }
  994. Rast_write_cats(aspect_name, &cats);
  995. Rast_free_cats(&cats);
  996. /* write colors for aspect file */
  997. Rast_init_colors(&colors);
  998. Rast_read_fp_range(aspect_name, G_mapset(), &range);
  999. Rast_get_fp_range_min_max(&range, &min, &max);
  1000. Rast_make_aspect_fp_colors(&colors, min, max);
  1001. Rast_write_colors(aspect_name, G_mapset(), &colors);
  1002. /* writing history file */
  1003. Rast_short_history(aspect_name, "raster", &hist);
  1004. Rast_append_format_history(&hist, "aspect map elev = %s", elev_name);
  1005. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1006. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1007. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1008. Rast_command_history(&hist);
  1009. Rast_write_history(aspect_name, &hist);
  1010. G_message(_("Aspect raster map <%s> complete"), aspect_name);
  1011. }
  1012. if (slope_fd >= 0) {
  1013. /* colortable for slopes */
  1014. CELL val1, val2;
  1015. Rast_init_colors(&colors);
  1016. val1 = 0;
  1017. val2 = 2;
  1018. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  1019. val1 = 2;
  1020. val2 = 5;
  1021. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  1022. val1 = 5;
  1023. val2 = 10;
  1024. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  1025. val1 = 10;
  1026. val2 = 15;
  1027. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  1028. val1 = 15;
  1029. val2 = 30;
  1030. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  1031. val1 = 30;
  1032. val2 = 50;
  1033. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  1034. val1 = 50;
  1035. val2 = 90;
  1036. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  1037. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  1038. Rast_put_row(slope_fd, slp_raster, data_type);
  1039. Rast_close(slope_fd);
  1040. if (out_type != CELL_TYPE) {
  1041. /* INCR_BY_ONE
  1042. if(deg)
  1043. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 1, 91);
  1044. else
  1045. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp, max_slp,
  1046. (CELL) min_slp + 1, (CELL) ceil(max_slp) + 1);
  1047. */
  1048. Rast_write_colors(slope_name, G_mapset(), &colors);
  1049. if (deg)
  1050. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 0,
  1051. 90);
  1052. else /* percent */
  1053. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp,
  1054. max_slp, (CELL) min_slp,
  1055. (CELL) ceil(max_slp));
  1056. }
  1057. Rast_read_cats(slope_name, G_mapset(), &cats);
  1058. if (deg)
  1059. Rast_set_cats_title("slope in degrees", &cats);
  1060. else if (perc)
  1061. Rast_set_cats_title("percent slope", &cats);
  1062. G_verbose_message(_("Min computed slope %.4f, max computed slope %.4f"),
  1063. min_slp, max_slp);
  1064. /* the categries quant intervals are 1.0 long, plus
  1065. we are using reverse order so that the label looked up
  1066. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  1067. the one defined for i-1.5, i-.5 interval which is added later */
  1068. for (i = ceil(max_slp); i > /* INC BY ONE >= */ 0; i--) {
  1069. if (deg)
  1070. sprintf(buf, "%d degree%s", i, i == 1 ? "" : "s");
  1071. else if (perc)
  1072. sprintf(buf, "%d percent", i);
  1073. if (data_type == CELL_TYPE) {
  1074. /* INCR_BY_ONE
  1075. Rast_set_c_cat(i+1, buf, &cats);
  1076. */
  1077. Rast_set_c_cat(&i, &i, buf, &cats);
  1078. continue;
  1079. }
  1080. /* INCR_BY_ONE
  1081. tmp1 = (DCELL) i+.5;
  1082. tmp2 = (DCELL) i+1.5;
  1083. */
  1084. tmp1 = (DCELL) i - .5;
  1085. tmp2 = (DCELL) i + .5;
  1086. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  1087. }
  1088. if (data_type == CELL_TYPE) {
  1089. cat = 0;
  1090. Rast_set_c_cat(&cat, &cat, "zero slope", &cats);
  1091. }
  1092. /* INCR_BY_ONE
  1093. Rast_set_c_cat(0, "no data", &cats);
  1094. */
  1095. else {
  1096. tmp1 = 0;
  1097. tmp2 = 0.5;
  1098. Rast_set_d_cat(&tmp1, &tmp2, "zero slope", &cats);
  1099. }
  1100. /* INCR_BY_ONE
  1101. Rast_set_d_cat (&tmp1, &tmp1, "no data", &cats);
  1102. */
  1103. Rast_write_cats(slope_name, &cats);
  1104. /* writing history file */
  1105. Rast_short_history(slope_name, "raster", &hist);
  1106. Rast_append_format_history(&hist, "slope map elev = %s", elev_name);
  1107. Rast_append_format_history(&hist, "zfactor = %.2f format = %s", zfactor,
  1108. parm.slope_fmt->answer);
  1109. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1110. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1111. Rast_command_history(&hist);
  1112. Rast_write_history(slope_name, &hist);
  1113. G_message(_("Slope raster map <%s> complete"), slope_name);
  1114. }
  1115. /* colortable for curvatures */
  1116. if (pcurv_fd >= 0 || tcurv_fd >= 0) {
  1117. Rast_init_colors(&colors);
  1118. if (c1min < c2min)
  1119. dat1 = (FCELL) c1min;
  1120. else
  1121. dat1 = (FCELL) c2min;
  1122. dat2 = (FCELL) - 0.01;
  1123. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  1124. &dat2, 0, 0, 255, &colors);
  1125. dat1 = dat2;
  1126. dat2 = (FCELL) - 0.001;
  1127. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  1128. &dat2, 0, 127, 255, &colors);
  1129. dat1 = dat2;
  1130. dat2 = (FCELL) - 0.00001;
  1131. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  1132. &dat2, 0, 255, 255, &colors);
  1133. dat1 = dat2;
  1134. dat2 = (FCELL) 0.0;
  1135. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  1136. &dat2, 200, 255, 200, &colors);
  1137. dat1 = dat2;
  1138. dat2 = (FCELL) 0.00001;
  1139. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  1140. &dat2, 255, 255, 0, &colors);
  1141. dat1 = dat2;
  1142. dat2 = (FCELL) 0.001;
  1143. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  1144. &dat2, 255, 127, 0, &colors);
  1145. dat1 = dat2;
  1146. dat2 = (FCELL) 0.01;
  1147. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  1148. &dat2, 255, 0, 0, &colors);
  1149. dat1 = dat2;
  1150. if (c1max > c2max)
  1151. dat2 = (FCELL) c1max;
  1152. else
  1153. dat2 = (FCELL) c2max;
  1154. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  1155. &dat2, 255, 0, 200, &colors);
  1156. }
  1157. if (pcurv_fd >= 0) {
  1158. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  1159. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  1160. Rast_close(pcurv_fd);
  1161. Rast_write_colors(pcurv_name, G_mapset(), &colors);
  1162. if (out_type != CELL_TYPE)
  1163. Rast_round_fp_map(pcurv_name, G_mapset());
  1164. Rast_read_cats(pcurv_name, G_mapset(), &cats);
  1165. Rast_set_cats_title("profile curvature", &cats);
  1166. cat = 0;
  1167. Rast_set_c_cat(&cat, &cat, "no profile curve", &cats);
  1168. /* writing history file */
  1169. Rast_short_history(pcurv_name, "raster", &hist);
  1170. Rast_append_format_history(&hist, "profile curve map elev = %s", elev_name);
  1171. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1172. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1173. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1174. Rast_command_history(&hist);
  1175. Rast_write_history(pcurv_name, &hist);
  1176. G_message(_("Profile curve raster map <%s> complete"), pcurv_name);
  1177. }
  1178. if (tcurv_fd >= 0) {
  1179. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  1180. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  1181. Rast_close(tcurv_fd);
  1182. Rast_write_colors(tcurv_name, G_mapset(), &colors);
  1183. if (out_type != CELL_TYPE)
  1184. Rast_round_fp_map(tcurv_name, G_mapset());
  1185. Rast_read_cats(tcurv_name, G_mapset(), &cats);
  1186. Rast_set_cats_title("tangential curvature", &cats);
  1187. cat = 0;
  1188. Rast_set_c_cat(&cat, &cat, "no tangential curve", &cats);
  1189. /* writing history file */
  1190. Rast_short_history(tcurv_name, "raster", &hist);
  1191. Rast_append_format_history(&hist, "tangential curve map elev = %s", elev_name);
  1192. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1193. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1194. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1195. Rast_command_history(&hist);
  1196. Rast_write_history(tcurv_name, &hist);
  1197. G_message(_("Tangential curve raster map <%s> complete"), tcurv_name);
  1198. }
  1199. if (dx_fd >= 0) {
  1200. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  1201. Rast_put_row(dx_fd, dx_raster, data_type);
  1202. Rast_close(dx_fd);
  1203. if (out_type != CELL_TYPE)
  1204. Rast_round_fp_map(dx_name, G_mapset());
  1205. Rast_read_cats(dx_name, G_mapset(), &cats);
  1206. Rast_set_cats_title("E-W slope", &cats);
  1207. cat = 0;
  1208. Rast_set_c_cat(&cat, &cat, "no E-W slope", &cats);
  1209. /* writing history file */
  1210. Rast_short_history(dx_name, "raster", &hist);
  1211. Rast_append_format_history(&hist, "E-W slope map elev = %s", elev_name);
  1212. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1213. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1214. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1215. Rast_command_history(&hist);
  1216. Rast_write_history(dx_name, &hist);
  1217. G_message(_("E-W slope raster map <%s> complete"), dx_name);
  1218. }
  1219. if (dy_fd >= 0) {
  1220. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  1221. Rast_put_row(dy_fd, dy_raster, data_type);
  1222. Rast_close(dy_fd);
  1223. if (out_type != CELL_TYPE)
  1224. Rast_round_fp_map(dy_name, G_mapset());
  1225. Rast_read_cats(dy_name, G_mapset(), &cats);
  1226. Rast_set_cats_title("N-S slope", &cats);
  1227. cat = 0;
  1228. Rast_set_c_cat(&cat, &cat, "no N-S slope", &cats);
  1229. /* writing history file */
  1230. Rast_short_history(dy_name, "raster", &hist);
  1231. Rast_append_format_history(&hist, "N-S slope map elev = %s", elev_name);
  1232. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1233. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1234. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1235. Rast_command_history(&hist);
  1236. Rast_write_history(dy_name, &hist);
  1237. G_message(_("N-S slope raster map <%s> complete"), dy_name);
  1238. }
  1239. if (dxx_fd >= 0) {
  1240. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  1241. Rast_put_row(dxx_fd, dxx_raster, data_type);
  1242. Rast_close(dxx_fd);
  1243. if (out_type != CELL_TYPE)
  1244. Rast_round_fp_map(dxx_name, G_mapset());
  1245. Rast_read_cats(dxx_name, G_mapset(), &cats);
  1246. Rast_set_cats_title("DXX", &cats);
  1247. cat = 0;
  1248. Rast_set_c_cat(&cat, &cat, "DXX", &cats);
  1249. /* writing history file */
  1250. Rast_short_history(dxx_name, "raster", &hist);
  1251. Rast_append_format_history(&hist, "DXX map elev = %s", elev_name);
  1252. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1253. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1254. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1255. Rast_command_history(&hist);
  1256. Rast_write_history(dxx_name, &hist);
  1257. G_message(_("Dxx raster map <%s> complete"), dxx_name);
  1258. }
  1259. if (dyy_fd >= 0) {
  1260. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  1261. Rast_put_row(dyy_fd, dyy_raster, data_type);
  1262. Rast_close(dyy_fd);
  1263. if (out_type != CELL_TYPE)
  1264. Rast_round_fp_map(dyy_name, G_mapset());
  1265. Rast_read_cats(dyy_name, G_mapset(), &cats);
  1266. Rast_set_cats_title("DYY", &cats);
  1267. cat = 0;
  1268. Rast_set_c_cat(&cat, &cat, "DYY", &cats);
  1269. /* writing history file */
  1270. Rast_short_history(dyy_name, "raster", &hist);
  1271. Rast_append_format_history(&hist, "DYY map elev = %s", elev_name);
  1272. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1273. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1274. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1275. Rast_command_history(&hist);
  1276. Rast_write_history(dyy_name, &hist);
  1277. G_message(_("Dyy raster map <%s> complete"), dyy_name);
  1278. }
  1279. if (dxy_fd >= 0) {
  1280. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  1281. Rast_put_row(dxy_fd, dxy_raster, data_type);
  1282. Rast_close(dxy_fd);
  1283. if (out_type != CELL_TYPE)
  1284. Rast_round_fp_map(dxy_name, G_mapset());
  1285. Rast_read_cats(dxy_name, G_mapset(), &cats);
  1286. Rast_set_cats_title("DXY", &cats);
  1287. cat = 0;
  1288. Rast_set_c_cat(&cat, &cat, "DXY", &cats);
  1289. /* writing history file */
  1290. Rast_short_history(dxy_name, "raster", &hist);
  1291. Rast_append_format_history(&hist, "DXY map elev = %s", elev_name);
  1292. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1293. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1294. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1295. Rast_command_history(&hist);
  1296. Rast_write_history(dxy_name, &hist);
  1297. G_message(_("Dxy raster map <%s> complete"), dxy_name);
  1298. }
  1299. exit(EXIT_SUCCESS);
  1300. }