main.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. G_get_window(&window);
  327. /* set the window from the header for the elevation file */
  328. if (!flag.a->answer) {
  329. Rast_get_cellhd(elev_name, "", &cellhd);
  330. Rast_align_window(&window, &cellhd);
  331. Rast_set_window(&window);
  332. /* probably not needed, just to make sure
  333. * G_get_window() and Rast_get_window()
  334. * return the same */
  335. G_set_window(&window);
  336. }
  337. if (strcmp(parm.out_precision->answer, "DCELL") == 0)
  338. out_type = DCELL_TYPE;
  339. else if (strcmp(parm.out_precision->answer, "FCELL") == 0)
  340. out_type = FCELL_TYPE;
  341. else if (strcmp(parm.out_precision->answer, "CELL") == 0)
  342. out_type = CELL_TYPE;
  343. else
  344. G_fatal_error(_("Wrong raster type: %s"), parm.out_precision->answer);
  345. data_type = out_type;
  346. /* data type is the type of data being processed,
  347. out_type is type of map being created */
  348. /* ? why not use Rast_map_type() then ? */
  349. nrows = Rast_window_rows();
  350. ncols = Rast_window_cols();
  351. if (((window.west == (window.east - 360.))
  352. || (window.east == (window.west - 360.))) &&
  353. (G_projection() == PROJECTION_LL)) {
  354. Wrap = 1;
  355. ncols += 2;
  356. }
  357. else
  358. Wrap = 0;
  359. /* H = window.ew_res * 4 * 2/ zfactor; *//* horizontal (east-west) run
  360. times 4 for weighted difference */
  361. /* V = window.ns_res * 4 * 2/ zfactor; *//* vertical (north-south) run
  362. times 4 for weighted difference */
  363. /* we don't assume vertical units to be meters any more */
  364. factor = G_database_units_to_meters_factor();
  365. if (factor != 1.0 && zfactor != 1.0)
  366. G_warning(_("r.slope.aspect does not convert horizontal units "
  367. "to meters in this version, see manual page."));
  368. G_begin_distance_calculations();
  369. north = Rast_row_to_northing(0.5, &window);
  370. ns_med = Rast_row_to_northing(1.5, &window);
  371. south = Rast_row_to_northing(2.5, &window);
  372. east = Rast_col_to_easting(2.5, &window);
  373. west = Rast_col_to_easting(0.5, &window);
  374. V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
  375. H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * zfactor);
  376. /* ____________________________
  377. |c1 |c2 |c3 |
  378. | | | |
  379. | | north | |
  380. | | | |
  381. |________|________|________|
  382. |c4 |c5 |c6 |
  383. | | | |
  384. | east | ns_med | west |
  385. | | | |
  386. |________|________|________|
  387. |c7 |c8 |c9 |
  388. | | | |
  389. | | south | |
  390. | | | |
  391. |________|________|________|
  392. */
  393. /* open the elevation file for reading */
  394. elevation_fd = Rast_open_old(elev_name, "");
  395. elev_cell[0] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  396. Rast_set_d_null_value(elev_cell[0], ncols);
  397. elev_cell[1] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  398. Rast_set_d_null_value(elev_cell[1], ncols);
  399. elev_cell[2] = (DCELL *) G_calloc(ncols + 1, sizeof(DCELL));
  400. Rast_set_d_null_value(elev_cell[2], ncols);
  401. if (slope_name != NULL) {
  402. slope_fd = Rast_open_new(slope_name, out_type);
  403. slp_raster = Rast_allocate_buf(data_type);
  404. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  405. Rast_put_row(slope_fd, slp_raster, data_type);
  406. }
  407. else {
  408. slp_raster = NULL;
  409. slope_fd = -1;
  410. }
  411. if (aspect_name != NULL) {
  412. aspect_fd = Rast_open_new(aspect_name, out_type);
  413. asp_raster = Rast_allocate_buf(data_type);
  414. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  415. Rast_put_row(aspect_fd, asp_raster, data_type);
  416. }
  417. else {
  418. asp_raster = NULL;
  419. aspect_fd = -1;
  420. }
  421. if (pcurv_name != NULL) {
  422. pcurv_fd = Rast_open_new(pcurv_name, out_type);
  423. pcurv_raster = Rast_allocate_buf(data_type);
  424. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  425. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  426. }
  427. else {
  428. pcurv_raster = NULL;
  429. pcurv_fd = -1;
  430. }
  431. if (tcurv_name != NULL) {
  432. tcurv_fd = Rast_open_new(tcurv_name, out_type);
  433. tcurv_raster = Rast_allocate_buf(data_type);
  434. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  435. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  436. }
  437. else {
  438. tcurv_raster = NULL;
  439. tcurv_fd = -1;
  440. }
  441. if (dx_name != NULL) {
  442. dx_fd = Rast_open_new(dx_name, out_type);
  443. dx_raster = Rast_allocate_buf(data_type);
  444. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  445. Rast_put_row(dx_fd, dx_raster, data_type);
  446. }
  447. else {
  448. dx_raster = NULL;
  449. dx_fd = -1;
  450. }
  451. if (dy_name != NULL) {
  452. dy_fd = Rast_open_new(dy_name, out_type);
  453. dy_raster = Rast_allocate_buf(data_type);
  454. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  455. Rast_put_row(dy_fd, dy_raster, data_type);
  456. }
  457. else {
  458. dy_raster = NULL;
  459. dy_fd = -1;
  460. }
  461. if (dxx_name != NULL) {
  462. dxx_fd = Rast_open_new(dxx_name, out_type);
  463. dxx_raster = Rast_allocate_buf(data_type);
  464. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  465. Rast_put_row(dxx_fd, dxx_raster, data_type);
  466. }
  467. else {
  468. dxx_raster = NULL;
  469. dxx_fd = -1;
  470. }
  471. if (dyy_name != NULL) {
  472. dyy_fd = Rast_open_new(dyy_name, out_type);
  473. dyy_raster = Rast_allocate_buf(data_type);
  474. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  475. Rast_put_row(dyy_fd, dyy_raster, data_type);
  476. }
  477. else {
  478. dyy_raster = NULL;
  479. dyy_fd = -1;
  480. }
  481. if (dxy_name != NULL) {
  482. dxy_fd = Rast_open_new(dxy_name, out_type);
  483. dxy_raster = Rast_allocate_buf(data_type);
  484. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  485. Rast_put_row(dxy_fd, dxy_raster, data_type);
  486. }
  487. else {
  488. dxy_raster = NULL;
  489. dxy_fd = -1;
  490. }
  491. if (aspect_fd < 0 && slope_fd < 0 && pcurv_fd < 0 && tcurv_fd < 0
  492. && dx_fd < 0 && dy_fd < 0 && dxx_fd < 0 && dyy_fd < 0 && dxy_fd < 0)
  493. exit(EXIT_FAILURE);
  494. if (Wrap) {
  495. Rast_get_d_row_nomask(elevation_fd, elev_cell[1] + 1, 0);
  496. elev_cell[1][0] = elev_cell[1][Rast_window_cols() - 1];
  497. elev_cell[1][Rast_window_cols() + 1] = elev_cell[1][2];
  498. }
  499. else
  500. Rast_get_d_row_nomask(elevation_fd, elev_cell[1], 0);
  501. if (Wrap) {
  502. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, 1);
  503. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  504. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  505. }
  506. else
  507. Rast_get_d_row_nomask(elevation_fd, elev_cell[2], 1);
  508. G_verbose_message(_("Percent complete..."));
  509. for (row = 2; row < nrows; row++) {
  510. /* if projection is Lat/Lon, recalculate V and H */
  511. if (G_projection() == PROJECTION_LL) {
  512. north = Rast_row_to_northing((row - 2 + 0.5), &window);
  513. ns_med = Rast_row_to_northing((row - 1 + 0.5), &window);
  514. south = Rast_row_to_northing((row + 0.5), &window);
  515. east = Rast_col_to_easting(2.5, &window);
  516. west = Rast_col_to_easting(0.5, &window);
  517. V = G_distance(east, north, east, south) * 4 / (factor * zfactor);
  518. H = G_distance(east, ns_med, west, ns_med) * 4 / (factor * zfactor);
  519. /* ____________________________
  520. |c1 |c2 |c3 |
  521. | | | |
  522. | | north | |
  523. | | | |
  524. |________|________|________|
  525. |c4 |c5 |c6 |
  526. | | | |
  527. | east | ns_med | west |
  528. | | | |
  529. |________|________|________|
  530. |c7 |c8 |c9 |
  531. | | | |
  532. | | south | |
  533. | | | |
  534. |________|________|________|
  535. */
  536. }
  537. G_percent(row, nrows, 2);
  538. temp = elev_cell[0];
  539. elev_cell[0] = elev_cell[1];
  540. elev_cell[1] = elev_cell[2];
  541. elev_cell[2] = temp;
  542. if (Wrap) {
  543. Rast_get_d_row_nomask(elevation_fd, elev_cell[2] + 1, row);
  544. elev_cell[2][0] = elev_cell[2][Rast_window_cols() - 1];
  545. elev_cell[2][Rast_window_cols() + 1] = elev_cell[2][2];
  546. }
  547. else
  548. Rast_get_d_row_nomask(elevation_fd, elev_cell[2], row);
  549. pc1 = elev_cell[0];
  550. pc2 = elev_cell[1];
  551. pc3 = elev_cell[2];
  552. if (aspect_fd >= 0) {
  553. if (Wrap)
  554. asp_ptr = asp_raster;
  555. else
  556. asp_ptr =
  557. G_incr_void_ptr(asp_raster, Rast_cell_size(data_type));
  558. }
  559. if (slope_fd >= 0) {
  560. if (Wrap)
  561. slp_ptr = slp_raster;
  562. else
  563. slp_ptr =
  564. G_incr_void_ptr(slp_raster, Rast_cell_size(data_type));
  565. }
  566. if (pcurv_fd >= 0) {
  567. if (Wrap)
  568. pcurv_ptr = pcurv_raster;
  569. else
  570. pcurv_ptr =
  571. G_incr_void_ptr(pcurv_raster, Rast_cell_size(data_type));
  572. }
  573. if (tcurv_fd >= 0) {
  574. if (Wrap)
  575. tcurv_ptr = tcurv_raster;
  576. else
  577. tcurv_ptr =
  578. G_incr_void_ptr(tcurv_raster, Rast_cell_size(data_type));
  579. }
  580. if (dx_fd >= 0) {
  581. if (Wrap)
  582. dx_ptr = dx_raster;
  583. else
  584. dx_ptr = G_incr_void_ptr(dx_raster, Rast_cell_size(data_type));
  585. }
  586. if (dy_fd >= 0) {
  587. if (Wrap)
  588. dy_ptr = dy_raster;
  589. else
  590. dy_ptr = G_incr_void_ptr(dy_raster, Rast_cell_size(data_type));
  591. }
  592. if (dxx_fd >= 0) {
  593. if (Wrap)
  594. dxx_ptr = dxx_raster;
  595. else
  596. dxx_ptr =
  597. G_incr_void_ptr(dxx_raster, Rast_cell_size(data_type));
  598. }
  599. if (dyy_fd >= 0) {
  600. if (Wrap)
  601. dyy_ptr = dyy_raster;
  602. else
  603. dyy_ptr =
  604. G_incr_void_ptr(dyy_raster, Rast_cell_size(data_type));
  605. }
  606. if (dxy_fd >= 0) {
  607. if (Wrap)
  608. dxy_ptr = dxy_raster;
  609. else
  610. dxy_ptr =
  611. G_incr_void_ptr(dxy_raster, Rast_cell_size(data_type));
  612. }
  613. /*skip first cell of the row */
  614. for (col = ncols - 2; col-- > 0; pc1++, pc2++, pc3++) {
  615. c1 = *pc1;
  616. c2 = *(pc1 + 1);
  617. c3 = *(pc1 + 2);
  618. c4 = *pc2;
  619. c5 = *(pc2 + 1);
  620. c6 = *(pc2 + 2);
  621. c7 = *pc3;
  622. c8 = *(pc3 + 1);
  623. c9 = *(pc3 + 2);
  624. /* DEBUG:
  625. fprintf(stdout, "\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n%.0f %.0f %.0f\n",
  626. c1, c2, c3, c4, c5, c6, c7, c8, c9);
  627. */
  628. if (Rast_is_d_null_value(&c5) || (!compute_at_edges &&
  629. (Rast_is_d_null_value(&c1) || Rast_is_d_null_value(&c2) ||
  630. Rast_is_d_null_value(&c3) || Rast_is_d_null_value(&c4) ||
  631. Rast_is_d_null_value(&c6) || Rast_is_d_null_value(&c7) ||
  632. Rast_is_d_null_value(&c8) || Rast_is_d_null_value(&c9)))) {
  633. if (slope_fd > 0) {
  634. Rast_set_null_value(slp_ptr, 1, data_type);
  635. slp_ptr =
  636. G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  637. }
  638. if (aspect_fd > 0) {
  639. Rast_set_null_value(asp_ptr, 1, data_type);
  640. asp_ptr =
  641. G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  642. }
  643. if (pcurv_fd > 0) {
  644. Rast_set_null_value(pcurv_ptr, 1, data_type);
  645. pcurv_ptr =
  646. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  647. }
  648. if (tcurv_fd > 0) {
  649. Rast_set_null_value(tcurv_ptr, 1, data_type);
  650. tcurv_ptr =
  651. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  652. }
  653. if (dx_fd > 0) {
  654. Rast_set_null_value(dx_ptr, 1, data_type);
  655. dx_ptr =
  656. G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  657. }
  658. if (dy_fd > 0) {
  659. Rast_set_null_value(dy_ptr, 1, data_type);
  660. dy_ptr =
  661. G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  662. }
  663. if (dxx_fd > 0) {
  664. Rast_set_null_value(dxx_ptr, 1, data_type);
  665. dxx_ptr =
  666. G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  667. }
  668. if (dyy_fd > 0) {
  669. Rast_set_null_value(dyy_ptr, 1, data_type);
  670. dyy_ptr =
  671. G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  672. }
  673. if (dxy_fd > 0) {
  674. Rast_set_null_value(dxy_ptr, 1, data_type);
  675. dxy_ptr =
  676. G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  677. }
  678. continue;
  679. } /* no data */
  680. if (compute_at_edges) {
  681. /* same method like ComputeVal in gdaldem_lib.cpp */
  682. if (Rast_is_d_null_value(&c1))
  683. c1 = c5;
  684. if (Rast_is_d_null_value(&c2))
  685. c2 = c5;
  686. if (Rast_is_d_null_value(&c3))
  687. c3 = c5;
  688. if (Rast_is_d_null_value(&c4))
  689. c4 = c5;
  690. if (Rast_is_d_null_value(&c6))
  691. c6 = c5;
  692. if (Rast_is_d_null_value(&c7))
  693. c7 = c5;
  694. if (Rast_is_d_null_value(&c8))
  695. c8 = c5;
  696. if (Rast_is_d_null_value(&c9))
  697. c9 = c5;
  698. }
  699. dx = ((c1 + c4 + c4 + c7) - (c3 + c6 + c6 + c9)) / H;
  700. dy = ((c7 + c8 + c8 + c9) - (c1 + c2 + c2 + c3)) / V;
  701. /* compute topographic parameters */
  702. key = dx * dx + dy * dy;
  703. slp_in_perc = 100 * sqrt(key);
  704. slp_in_deg = atan(sqrt(key)) * radians_to_degrees;
  705. /* now update min and max */
  706. if (deg) {
  707. if (min_slp > slp_in_deg)
  708. min_slp = slp_in_deg;
  709. if (max_slp < slp_in_deg)
  710. max_slp = slp_in_deg;
  711. }
  712. else {
  713. if (min_slp > slp_in_perc)
  714. min_slp = slp_in_perc;
  715. if (max_slp < slp_in_perc)
  716. max_slp = slp_in_perc;
  717. }
  718. if (slp_in_perc < min_slope)
  719. slp_in_perc = 0.;
  720. if (deg && out_type == CELL_TYPE) {
  721. /* INC BY ONE
  722. low = 1;
  723. hi = 91;
  724. */
  725. low = 0;
  726. hi = 90;
  727. test = 20;
  728. while (hi >= low) {
  729. if (key >= answer[test])
  730. low = test + 1;
  731. else if (key < answer[test - 1])
  732. hi = test - 1;
  733. else
  734. break;
  735. test = (low + hi) / 2;
  736. }
  737. }
  738. else if (perc && out_type == CELL_TYPE)
  739. /* INCR_BY_ONE */
  740. /* test = slp_in_perc + 1.5; *//* All the slope categories are
  741. incremented by 1 */
  742. test = slp_in_perc + .5;
  743. if (slope_fd > 0) {
  744. if (data_type == CELL_TYPE)
  745. *((CELL *) slp_ptr) = (CELL) test;
  746. else {
  747. if (deg)
  748. Rast_set_d_value(slp_ptr,
  749. (DCELL) slp_in_deg, data_type);
  750. else
  751. Rast_set_d_value(slp_ptr,
  752. (DCELL) slp_in_perc, data_type);
  753. }
  754. slp_ptr = G_incr_void_ptr(slp_ptr, Rast_cell_size(data_type));
  755. } /* computing slope */
  756. if (aspect_fd > 0) {
  757. double aspect_flat = 0.;
  758. if (slp_in_perc == 0.)
  759. aspect = 0.;
  760. else if (dx == 0) {
  761. if (dy > 0)
  762. aspect = 90.;
  763. else
  764. aspect = 270.;
  765. }
  766. else {
  767. aspect = (atan2(dy, dx) / degrees_to_radians);
  768. if (aspect <= 0.)
  769. aspect = 360. + aspect;
  770. }
  771. if (flag.n->answer) {
  772. aspect_flat = -9999;
  773. aspect = aspect_cw_n(aspect);
  774. }
  775. if (out_type == CELL_TYPE) {
  776. if (aspect > 0 && aspect < 0.5)
  777. aspect = 360;
  778. *((CELL *) asp_ptr) = (CELL) (aspect + .5);
  779. }
  780. else
  781. Rast_set_d_value(asp_ptr,
  782. (DCELL) aspect, data_type);
  783. asp_ptr = G_incr_void_ptr(asp_ptr, Rast_cell_size(data_type));
  784. /* now update min and max */
  785. if (aspect > aspect_flat && min_asp > aspect)
  786. min_asp = aspect;
  787. if (max_asp < aspect)
  788. max_asp = aspect;
  789. } /* computing aspect */
  790. if (dx_fd > 0) {
  791. if (out_type == CELL_TYPE)
  792. *((CELL *) dx_ptr) = (CELL) (scik1 * dx);
  793. else
  794. Rast_set_d_value(dx_ptr, (DCELL) dx, data_type);
  795. dx_ptr = G_incr_void_ptr(dx_ptr, Rast_cell_size(data_type));
  796. }
  797. if (dy_fd > 0) {
  798. if (out_type == CELL_TYPE)
  799. *((CELL *) dy_ptr) = (CELL) (scik1 * dy);
  800. else
  801. Rast_set_d_value(dy_ptr, (DCELL) dy, data_type);
  802. dy_ptr = G_incr_void_ptr(dy_ptr, Rast_cell_size(data_type));
  803. }
  804. if (dxx_fd <= 0 && dxy_fd <= 0 && dyy_fd <= 0 &&
  805. pcurv_fd <= 0 && tcurv_fd <= 0)
  806. continue;
  807. /* compute second order derivatives */
  808. s4 = c1 + c3 + c7 + c9 - c5 * 8.;
  809. s5 = c4 * 4. + c6 * 4. - c8 * 2. - c2 * 2.;
  810. s6 = c8 * 4. + c2 * 4. - c4 * 2. - c6 * 2.;
  811. s3 = c7 - c9 + c3 - c1;
  812. dxx = -(s4 + s5) / ((3. / 32.) * H * H);
  813. dyy = -(s4 + s6) / ((3. / 32.) * V * V);
  814. dxy = -s3 / ((1. / 16.) * H * V);
  815. if (dxx_fd > 0) {
  816. if (out_type == CELL_TYPE)
  817. *((CELL *) dxx_ptr) = (CELL) (scik1 * dxx);
  818. else
  819. Rast_set_d_value(dxx_ptr, (DCELL) dxx, data_type);
  820. dxx_ptr = G_incr_void_ptr(dxx_ptr, Rast_cell_size(data_type));
  821. }
  822. if (dyy_fd > 0) {
  823. if (out_type == CELL_TYPE)
  824. *((CELL *) dyy_ptr) = (CELL) (scik1 * dyy);
  825. else
  826. Rast_set_d_value(dyy_ptr, (DCELL) dyy, data_type);
  827. dyy_ptr = G_incr_void_ptr(dyy_ptr, Rast_cell_size(data_type));
  828. }
  829. if (dxy_fd > 0) {
  830. if (out_type == CELL_TYPE)
  831. *((CELL *) dxy_ptr) = (CELL) (scik1 * dxy);
  832. else
  833. Rast_set_d_value(dxy_ptr, (DCELL) dxy, data_type);
  834. dxy_ptr = G_incr_void_ptr(dxy_ptr, Rast_cell_size(data_type));
  835. }
  836. /* compute curvature */
  837. if (pcurv_fd <= 0 && tcurv_fd <= 0)
  838. continue;
  839. grad2 = key; /*dx2 + dy2 */
  840. grad = sqrt(grad2);
  841. if (grad <= gradmin) {
  842. pcurv = 0.;
  843. tcurv = 0.;
  844. }
  845. else {
  846. dnorm1 = sqrt(grad2 + 1.);
  847. dxy2 = 2. * dxy * dx * dy;
  848. dx2 = dx * dx;
  849. dy2 = dy * dy;
  850. pcurv = (dxx * dx2 + dxy2 + dyy * dy2) /
  851. (grad2 * dnorm1 * dnorm1 * dnorm1);
  852. tcurv = (dxx * dy2 - dxy2 + dyy * dx2) / (grad2 * dnorm1);
  853. if (c1min > pcurv)
  854. c1min = pcurv;
  855. if (c1max < pcurv)
  856. c1max = pcurv;
  857. if (c2min > tcurv)
  858. c2min = tcurv;
  859. if (c2max < tcurv)
  860. c2max = tcurv;
  861. }
  862. if (pcurv_fd > 0) {
  863. if (out_type == CELL_TYPE)
  864. *((CELL *) pcurv_ptr) = (CELL) (scik1 * pcurv);
  865. else
  866. Rast_set_d_value(pcurv_ptr, (DCELL) pcurv, data_type);
  867. pcurv_ptr =
  868. G_incr_void_ptr(pcurv_ptr, Rast_cell_size(data_type));
  869. }
  870. if (tcurv_fd > 0) {
  871. if (out_type == CELL_TYPE)
  872. *((CELL *) tcurv_ptr) = (CELL) (scik1 * tcurv);
  873. else
  874. Rast_set_d_value(tcurv_ptr, (DCELL) tcurv, data_type);
  875. tcurv_ptr =
  876. G_incr_void_ptr(tcurv_ptr, Rast_cell_size(data_type));
  877. }
  878. } /* column for loop */
  879. if (aspect_fd > 0)
  880. Rast_put_row(aspect_fd, asp_raster, data_type);
  881. if (slope_fd > 0)
  882. Rast_put_row(slope_fd, slp_raster, data_type);
  883. if (pcurv_fd > 0)
  884. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  885. if (tcurv_fd > 0)
  886. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  887. if (dx_fd > 0)
  888. Rast_put_row(dx_fd, dx_raster, data_type);
  889. if (dy_fd > 0)
  890. Rast_put_row(dy_fd, dy_raster, data_type);
  891. if (dxx_fd > 0)
  892. Rast_put_row(dxx_fd, dxx_raster, data_type);
  893. if (dyy_fd > 0)
  894. Rast_put_row(dyy_fd, dyy_raster, data_type);
  895. if (dxy_fd > 0)
  896. Rast_put_row(dxy_fd, dxy_raster, data_type);
  897. } /* row loop */
  898. G_percent(row, nrows, 2);
  899. Rast_close(elevation_fd);
  900. G_debug(1, "Creating support files...");
  901. G_verbose_message(_("Elevation products for mapset <%s> in <%s>"),
  902. G_mapset(), G_location());
  903. if (aspect_fd >= 0) {
  904. DCELL min, max;
  905. struct FPRange range;
  906. Rast_set_null_value(asp_raster, Rast_window_cols(), data_type);
  907. Rast_put_row(aspect_fd, asp_raster, data_type);
  908. Rast_close(aspect_fd);
  909. if (out_type != CELL_TYPE)
  910. Rast_quantize_fp_map_range(aspect_name, G_mapset(), 0., 360., 0,
  911. 360);
  912. Rast_read_cats(aspect_name, G_mapset(), &cats);
  913. Rast_set_cats_title
  914. ("Aspect counterclockwise in degrees from east", &cats);
  915. G_verbose_message(_("Min computed aspect %.4f, max computed aspect %.4f"),
  916. min_asp, max_asp);
  917. /* the categries quant intervals are 1.0 long, plus
  918. we are using reverse order so that the label looked up
  919. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  920. the one defile for i-1.5, i-.5 interval which is added later */
  921. if (!flag.n->answer) {
  922. for (i = ceil(max_asp); i >= 1; i--) {
  923. if (i == 360)
  924. sprintf(buf, "east");
  925. else if (i == 45)
  926. sprintf(buf, "north ccw of east");
  927. else if (i == 90)
  928. sprintf(buf, "north");
  929. else if (i == 135)
  930. sprintf(buf, "north ccw of west");
  931. else if (i == 180)
  932. sprintf(buf, "west");
  933. else if (i == 225)
  934. sprintf(buf, "south ccw of west");
  935. else if (i == 270)
  936. sprintf(buf, "south");
  937. else if (i == 315)
  938. sprintf(buf, "south ccw of east");
  939. else
  940. sprintf(buf, "%d degree%s ccw from east", i,
  941. i == 1 ? "" : "s");
  942. if (data_type == CELL_TYPE) {
  943. Rast_set_c_cat((CELL *) &i, (CELL *) &i, buf, &cats);
  944. continue;
  945. }
  946. tmp1 = (double)i - .5;
  947. tmp2 = (double)i + .5;
  948. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  949. }
  950. if (data_type == CELL_TYPE) {
  951. cat = 0;
  952. Rast_set_c_cat(&cat, &cat, "no aspect", &cats);
  953. }
  954. else {
  955. tmp1 = 0;
  956. Rast_set_d_cat(&tmp1, &tmp1, "no aspect", &cats);
  957. }
  958. }
  959. else {
  960. for (i = ceil(max_asp); i >= 1; i--) {
  961. if (i == 0 && i == 360)
  962. sprintf(buf, "north");
  963. else if (i == 45)
  964. sprintf(buf, "north-east");
  965. else if (i == 90)
  966. sprintf(buf, "east");
  967. else if (i == 135)
  968. sprintf(buf, "south-east");
  969. else if (i == 180)
  970. sprintf(buf, "south");
  971. else if (i == 225)
  972. sprintf(buf, "south-west");
  973. else if (i == 270)
  974. sprintf(buf, "west");
  975. else if (i == 315)
  976. sprintf(buf, "north-west");
  977. else
  978. sprintf(buf, "%d degree%s cw from north", i,
  979. i == 1 ? "" : "s");
  980. if (data_type == CELL_TYPE) {
  981. Rast_set_c_cat((CELL *) &i, (CELL *) &i, buf, &cats);
  982. continue;
  983. }
  984. tmp1 = (double)i - .5;
  985. tmp2 = (double)i + .5;
  986. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  987. }
  988. if (data_type == CELL_TYPE) {
  989. cat = -9999;
  990. Rast_set_c_cat(&cat, &cat, "no aspect", &cats);
  991. }
  992. else {
  993. tmp1 = -9999;
  994. Rast_set_d_cat(&tmp1, &tmp1, "no aspect", &cats);
  995. }
  996. }
  997. Rast_write_cats(aspect_name, &cats);
  998. Rast_free_cats(&cats);
  999. /* write colors for aspect file */
  1000. Rast_init_colors(&colors);
  1001. Rast_read_fp_range(aspect_name, G_mapset(), &range);
  1002. Rast_get_fp_range_min_max(&range, &min, &max);
  1003. Rast_make_aspect_fp_colors(&colors, min, max);
  1004. Rast_write_colors(aspect_name, G_mapset(), &colors);
  1005. /* writing history file */
  1006. Rast_short_history(aspect_name, "raster", &hist);
  1007. Rast_append_format_history(&hist, "aspect map elev = %s", elev_name);
  1008. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1009. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1010. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1011. Rast_command_history(&hist);
  1012. Rast_write_history(aspect_name, &hist);
  1013. G_message(_("Aspect raster map <%s> complete"), aspect_name);
  1014. }
  1015. if (slope_fd >= 0) {
  1016. /* colortable for slopes */
  1017. CELL val1, val2;
  1018. Rast_init_colors(&colors);
  1019. val1 = 0;
  1020. val2 = 2;
  1021. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  1022. val1 = 2;
  1023. val2 = 5;
  1024. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  1025. val1 = 5;
  1026. val2 = 10;
  1027. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  1028. val1 = 10;
  1029. val2 = 15;
  1030. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  1031. val1 = 15;
  1032. val2 = 30;
  1033. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  1034. val1 = 30;
  1035. val2 = 50;
  1036. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  1037. val1 = 50;
  1038. val2 = 90;
  1039. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  1040. Rast_set_null_value(slp_raster, Rast_window_cols(), data_type);
  1041. Rast_put_row(slope_fd, slp_raster, data_type);
  1042. Rast_close(slope_fd);
  1043. if (out_type != CELL_TYPE) {
  1044. /* INCR_BY_ONE
  1045. if(deg)
  1046. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 1, 91);
  1047. else
  1048. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp, max_slp,
  1049. (CELL) min_slp + 1, (CELL) ceil(max_slp) + 1);
  1050. */
  1051. Rast_write_colors(slope_name, G_mapset(), &colors);
  1052. if (deg)
  1053. Rast_quantize_fp_map_range(slope_name, G_mapset(), 0., 90., 0,
  1054. 90);
  1055. else /* percent */
  1056. Rast_quantize_fp_map_range(slope_name, G_mapset(), min_slp,
  1057. max_slp, (CELL) min_slp,
  1058. (CELL) ceil(max_slp));
  1059. }
  1060. Rast_read_cats(slope_name, G_mapset(), &cats);
  1061. if (deg)
  1062. Rast_set_cats_title("slope in degrees", &cats);
  1063. else if (perc)
  1064. Rast_set_cats_title("percent slope", &cats);
  1065. G_verbose_message(_("Min computed slope %.4f, max computed slope %.4f"),
  1066. min_slp, max_slp);
  1067. /* the categries quant intervals are 1.0 long, plus
  1068. we are using reverse order so that the label looked up
  1069. for i-.5 is not the one defined for i-.5, i+.5 interval, but
  1070. the one defined for i-1.5, i-.5 interval which is added later */
  1071. for (i = ceil(max_slp); i > /* INC BY ONE >= */ 0; i--) {
  1072. if (deg)
  1073. sprintf(buf, "%d degree%s", i, i == 1 ? "" : "s");
  1074. else if (perc)
  1075. sprintf(buf, "%d percent", i);
  1076. if (data_type == CELL_TYPE) {
  1077. /* INCR_BY_ONE
  1078. Rast_set_c_cat(i+1, buf, &cats);
  1079. */
  1080. Rast_set_c_cat(&i, &i, buf, &cats);
  1081. continue;
  1082. }
  1083. /* INCR_BY_ONE
  1084. tmp1 = (DCELL) i+.5;
  1085. tmp2 = (DCELL) i+1.5;
  1086. */
  1087. tmp1 = (DCELL) i - .5;
  1088. tmp2 = (DCELL) i + .5;
  1089. Rast_set_d_cat(&tmp1, &tmp2, buf, &cats);
  1090. }
  1091. if (data_type == CELL_TYPE) {
  1092. cat = 0;
  1093. Rast_set_c_cat(&cat, &cat, "zero slope", &cats);
  1094. }
  1095. /* INCR_BY_ONE
  1096. Rast_set_c_cat(0, "no data", &cats);
  1097. */
  1098. else {
  1099. tmp1 = 0;
  1100. tmp2 = 0.5;
  1101. Rast_set_d_cat(&tmp1, &tmp2, "zero slope", &cats);
  1102. }
  1103. /* INCR_BY_ONE
  1104. Rast_set_d_cat (&tmp1, &tmp1, "no data", &cats);
  1105. */
  1106. Rast_write_cats(slope_name, &cats);
  1107. /* writing history file */
  1108. Rast_short_history(slope_name, "raster", &hist);
  1109. Rast_append_format_history(&hist, "slope map elev = %s", elev_name);
  1110. Rast_append_format_history(&hist, "zfactor = %.2f format = %s", zfactor,
  1111. parm.slope_fmt->answer);
  1112. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1113. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1114. Rast_command_history(&hist);
  1115. Rast_write_history(slope_name, &hist);
  1116. G_message(_("Slope raster map <%s> complete"), slope_name);
  1117. }
  1118. /* colortable for curvatures */
  1119. if (pcurv_fd >= 0 || tcurv_fd >= 0) {
  1120. Rast_init_colors(&colors);
  1121. if (c1min < c2min)
  1122. dat1 = (FCELL) c1min;
  1123. else
  1124. dat1 = (FCELL) c2min;
  1125. dat2 = (FCELL) - 0.01;
  1126. Rast_add_f_color_rule(&dat1, 127, 0, 255,
  1127. &dat2, 0, 0, 255, &colors);
  1128. dat1 = dat2;
  1129. dat2 = (FCELL) - 0.001;
  1130. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  1131. &dat2, 0, 127, 255, &colors);
  1132. dat1 = dat2;
  1133. dat2 = (FCELL) - 0.00001;
  1134. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  1135. &dat2, 0, 255, 255, &colors);
  1136. dat1 = dat2;
  1137. dat2 = (FCELL) 0.0;
  1138. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  1139. &dat2, 200, 255, 200, &colors);
  1140. dat1 = dat2;
  1141. dat2 = (FCELL) 0.00001;
  1142. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  1143. &dat2, 255, 255, 0, &colors);
  1144. dat1 = dat2;
  1145. dat2 = (FCELL) 0.001;
  1146. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  1147. &dat2, 255, 127, 0, &colors);
  1148. dat1 = dat2;
  1149. dat2 = (FCELL) 0.01;
  1150. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  1151. &dat2, 255, 0, 0, &colors);
  1152. dat1 = dat2;
  1153. if (c1max > c2max)
  1154. dat2 = (FCELL) c1max;
  1155. else
  1156. dat2 = (FCELL) c2max;
  1157. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  1158. &dat2, 255, 0, 200, &colors);
  1159. }
  1160. if (pcurv_fd >= 0) {
  1161. Rast_set_null_value(pcurv_raster, Rast_window_cols(), data_type);
  1162. Rast_put_row(pcurv_fd, pcurv_raster, data_type);
  1163. Rast_close(pcurv_fd);
  1164. Rast_write_colors(pcurv_name, G_mapset(), &colors);
  1165. if (out_type != CELL_TYPE)
  1166. Rast_round_fp_map(pcurv_name, G_mapset());
  1167. Rast_read_cats(pcurv_name, G_mapset(), &cats);
  1168. Rast_set_cats_title("profile curvature", &cats);
  1169. cat = 0;
  1170. Rast_set_c_cat(&cat, &cat, "no profile curve", &cats);
  1171. /* writing history file */
  1172. Rast_short_history(pcurv_name, "raster", &hist);
  1173. Rast_append_format_history(&hist, "profile curve map elev = %s", elev_name);
  1174. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1175. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1176. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1177. Rast_command_history(&hist);
  1178. Rast_write_history(pcurv_name, &hist);
  1179. G_message(_("Profile curve raster map <%s> complete"), pcurv_name);
  1180. }
  1181. if (tcurv_fd >= 0) {
  1182. Rast_set_null_value(tcurv_raster, Rast_window_cols(), data_type);
  1183. Rast_put_row(tcurv_fd, tcurv_raster, data_type);
  1184. Rast_close(tcurv_fd);
  1185. Rast_write_colors(tcurv_name, G_mapset(), &colors);
  1186. if (out_type != CELL_TYPE)
  1187. Rast_round_fp_map(tcurv_name, G_mapset());
  1188. Rast_read_cats(tcurv_name, G_mapset(), &cats);
  1189. Rast_set_cats_title("tangential curvature", &cats);
  1190. cat = 0;
  1191. Rast_set_c_cat(&cat, &cat, "no tangential curve", &cats);
  1192. /* writing history file */
  1193. Rast_short_history(tcurv_name, "raster", &hist);
  1194. Rast_append_format_history(&hist, "tangential curve map elev = %s", elev_name);
  1195. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1196. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1197. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1198. Rast_command_history(&hist);
  1199. Rast_write_history(tcurv_name, &hist);
  1200. G_message(_("Tangential curve raster map <%s> complete"), tcurv_name);
  1201. }
  1202. if (dx_fd >= 0) {
  1203. Rast_set_null_value(dx_raster, Rast_window_cols(), data_type);
  1204. Rast_put_row(dx_fd, dx_raster, data_type);
  1205. Rast_close(dx_fd);
  1206. if (out_type != CELL_TYPE)
  1207. Rast_round_fp_map(dx_name, G_mapset());
  1208. Rast_read_cats(dx_name, G_mapset(), &cats);
  1209. Rast_set_cats_title("E-W slope", &cats);
  1210. cat = 0;
  1211. Rast_set_c_cat(&cat, &cat, "no E-W slope", &cats);
  1212. /* writing history file */
  1213. Rast_short_history(dx_name, "raster", &hist);
  1214. Rast_append_format_history(&hist, "E-W slope map elev = %s", elev_name);
  1215. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1216. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1217. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1218. Rast_command_history(&hist);
  1219. Rast_write_history(dx_name, &hist);
  1220. G_message(_("E-W slope raster map <%s> complete"), dx_name);
  1221. }
  1222. if (dy_fd >= 0) {
  1223. Rast_set_null_value(dy_raster, Rast_window_cols(), data_type);
  1224. Rast_put_row(dy_fd, dy_raster, data_type);
  1225. Rast_close(dy_fd);
  1226. if (out_type != CELL_TYPE)
  1227. Rast_round_fp_map(dy_name, G_mapset());
  1228. Rast_read_cats(dy_name, G_mapset(), &cats);
  1229. Rast_set_cats_title("N-S slope", &cats);
  1230. cat = 0;
  1231. Rast_set_c_cat(&cat, &cat, "no N-S slope", &cats);
  1232. /* writing history file */
  1233. Rast_short_history(dy_name, "raster", &hist);
  1234. Rast_append_format_history(&hist, "N-S slope map elev = %s", elev_name);
  1235. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1236. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1237. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1238. Rast_command_history(&hist);
  1239. Rast_write_history(dy_name, &hist);
  1240. G_message(_("N-S slope raster map <%s> complete"), dy_name);
  1241. }
  1242. if (dxx_fd >= 0) {
  1243. Rast_set_null_value(dxx_raster, Rast_window_cols(), data_type);
  1244. Rast_put_row(dxx_fd, dxx_raster, data_type);
  1245. Rast_close(dxx_fd);
  1246. if (out_type != CELL_TYPE)
  1247. Rast_round_fp_map(dxx_name, G_mapset());
  1248. Rast_read_cats(dxx_name, G_mapset(), &cats);
  1249. Rast_set_cats_title("DXX", &cats);
  1250. cat = 0;
  1251. Rast_set_c_cat(&cat, &cat, "DXX", &cats);
  1252. /* writing history file */
  1253. Rast_short_history(dxx_name, "raster", &hist);
  1254. Rast_append_format_history(&hist, "DXX map elev = %s", elev_name);
  1255. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1256. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1257. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1258. Rast_command_history(&hist);
  1259. Rast_write_history(dxx_name, &hist);
  1260. G_message(_("Dxx raster map <%s> complete"), dxx_name);
  1261. }
  1262. if (dyy_fd >= 0) {
  1263. Rast_set_null_value(dyy_raster, Rast_window_cols(), data_type);
  1264. Rast_put_row(dyy_fd, dyy_raster, data_type);
  1265. Rast_close(dyy_fd);
  1266. if (out_type != CELL_TYPE)
  1267. Rast_round_fp_map(dyy_name, G_mapset());
  1268. Rast_read_cats(dyy_name, G_mapset(), &cats);
  1269. Rast_set_cats_title("DYY", &cats);
  1270. cat = 0;
  1271. Rast_set_c_cat(&cat, &cat, "DYY", &cats);
  1272. /* writing history file */
  1273. Rast_short_history(dyy_name, "raster", &hist);
  1274. Rast_append_format_history(&hist, "DYY map elev = %s", elev_name);
  1275. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1276. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1277. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1278. Rast_command_history(&hist);
  1279. Rast_write_history(dyy_name, &hist);
  1280. G_message(_("Dyy raster map <%s> complete"), dyy_name);
  1281. }
  1282. if (dxy_fd >= 0) {
  1283. Rast_set_null_value(dxy_raster, Rast_window_cols(), data_type);
  1284. Rast_put_row(dxy_fd, dxy_raster, data_type);
  1285. Rast_close(dxy_fd);
  1286. if (out_type != CELL_TYPE)
  1287. Rast_round_fp_map(dxy_name, G_mapset());
  1288. Rast_read_cats(dxy_name, G_mapset(), &cats);
  1289. Rast_set_cats_title("DXY", &cats);
  1290. cat = 0;
  1291. Rast_set_c_cat(&cat, &cat, "DXY", &cats);
  1292. /* writing history file */
  1293. Rast_short_history(dxy_name, "raster", &hist);
  1294. Rast_append_format_history(&hist, "DXY map elev = %s", elev_name);
  1295. Rast_append_format_history(&hist, "zfactor = %.2f", zfactor);
  1296. Rast_append_format_history(&hist, "min_slope = %f", min_slope);
  1297. Rast_format_history(&hist, HIST_DATSRC_1, "raster elevation file %s", elev_name);
  1298. Rast_command_history(&hist);
  1299. Rast_write_history(dxy_name, &hist);
  1300. G_message(_("Dxy raster map <%s> complete"), dxy_name);
  1301. }
  1302. exit(EXIT_SUCCESS);
  1303. }