resout2d.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*-
  2. * Written by H. Mitasova, I. Kosinovsky, D. Gerdes Summer 1993
  3. * University of Illinois
  4. * US Army Construction Engineering Research Lab
  5. * Copyright 1993, H. Mitasova (University of Illinois),
  6. * I. Kosinovsky, (USA-CERL), and D.Gerdes (USA-CERL)
  7. *
  8. * modified by McCauley in August 1995
  9. * modified by Mitasova in August 1995
  10. *
  11. */
  12. #define MULT 100000
  13. #include <stdio.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/raster.h>
  17. #include <grass/bitmap.h>
  18. #include <grass/linkm.h>
  19. #include <grass/interpf.h>
  20. #include <grass/glocale.h>
  21. /* output cell maps for elevation, aspect, slope and curvatures */
  22. int IL_resample_output_2d(struct interp_params *params, double zmin, double zmax, /* min,max input z-values */
  23. double zminac, double zmaxac, /* min,max interpolated values */
  24. double c1min, double c1max, double c2min, double c2max, double gmin, double gmax, double ertot, /* total interplating func. error */
  25. char *input, /* input file name */
  26. double *dnorm, struct Cell_head *outhd, /* Region with desired resolution */
  27. struct Cell_head *winhd, /* Current region */
  28. char *smooth, int n_points)
  29. /*
  30. * Creates output files as well as history files and color tables for
  31. * them.
  32. */
  33. {
  34. FCELL *cell1; /* cell buffer */
  35. int cf1 = 0, cf2 = 0, cf3 = 0, cf4 = 0, cf5 = 0, cf6 = 0; /* cell file descriptors */
  36. int nrows, ncols; /* current region rows and columns */
  37. int i; /* loop counter */
  38. const char *mapset;
  39. float dat1, dat2;
  40. struct Colors colors, colors2;
  41. double value1, value2;
  42. struct History hist, hist1, hist2, hist3, hist4, hist5;
  43. struct _Color_Rule_ *rule;
  44. const char *maps, *type;
  45. int cond1, cond2;
  46. CELL val1, val2;
  47. cond2 = ((params->pcurv != NULL) ||
  48. (params->tcurv != NULL) || (params->mcurv != NULL));
  49. cond1 = ((params->slope != NULL) || (params->aspect != NULL) || cond2);
  50. /* change region to output cell file region */
  51. G_verbose_message(_("Temporarily changing the region to desired resolution..."));
  52. if (Rast_set_window(outhd) < 0) {
  53. G_warning(_("Unable to set region"));
  54. return -1;
  55. }
  56. mapset = G_mapset();
  57. cell1 = Rast_allocate_f_buf();
  58. if (params->elev != NULL) {
  59. cf1 = Rast_open_fp_new(params->elev);
  60. if (cf1 < 0) {
  61. G_warning(_("Unable to create raster map <%s>"),
  62. params->elev);
  63. return -1;
  64. }
  65. }
  66. if (params->slope != NULL) {
  67. cf2 = Rast_open_fp_new(params->slope);
  68. if (cf2 < 0) {
  69. G_warning(_("Unable to create raster map <%s>"),
  70. params->elev);
  71. return -1;
  72. }
  73. }
  74. if (params->aspect != NULL) {
  75. cf3 = Rast_open_fp_new(params->aspect);
  76. if (cf3 < 0) {
  77. G_warning(_("Unable to create raster map <%s>"),
  78. params->elev);
  79. return -1;
  80. }
  81. }
  82. if (params->pcurv != NULL) {
  83. cf4 = Rast_open_fp_new(params->pcurv);
  84. if (cf4 < 0) {
  85. G_warning(_("Unable to create raster map <%s>"),
  86. params->elev);
  87. return -1;
  88. }
  89. }
  90. if (params->tcurv != NULL) {
  91. cf5 = Rast_open_fp_new(params->tcurv);
  92. if (cf5 < 0) {
  93. G_warning(_("Unable to create raster map <%s>"),
  94. params->elev);
  95. return -1;
  96. }
  97. }
  98. if (params->mcurv != NULL) {
  99. cf6 = Rast_open_fp_new(params->mcurv);
  100. if (cf6 < 0) {
  101. G_warning(_("Unable to create raster map <%s>"),
  102. params->elev);
  103. return -1;
  104. }
  105. }
  106. nrows = outhd->rows;
  107. if (nrows != params->nsizr) {
  108. G_warning(_("First change your rows number(%d) to %d"),
  109. nrows, params->nsizr);
  110. return -1;
  111. }
  112. ncols = outhd->cols;
  113. if (ncols != params->nsizc) {
  114. G_warning(_("First change your columns number(%d) to %d"),
  115. ncols, params->nsizr);
  116. return -1;
  117. }
  118. if (params->elev != NULL) {
  119. fseek(params->Tmp_fd_z, 0L, 0); /* seek to the beginning */
  120. for (i = 0; i < params->nsizr; i++) {
  121. /* seek to the right row */
  122. if (fseek(params->Tmp_fd_z, (long)
  123. ((params->nsizr - 1 -
  124. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  125. G_warning(_("Unable to fseek to the right spot"));
  126. return -1;
  127. }
  128. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_z);
  129. if (Rast_put_f_raster_row(cf1, cell1) < 0) {
  130. G_warning(_("Failed writing raster map"));
  131. return -1;
  132. }
  133. }
  134. }
  135. if (params->slope != NULL) {
  136. fseek(params->Tmp_fd_dx, 0L, 0); /* seek to the beginning */
  137. for (i = 0; i < params->nsizr; i++) {
  138. /* seek to the right row */
  139. if (fseek(params->Tmp_fd_dx, (long)
  140. ((params->nsizr - 1 -
  141. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  142. G_warning(_("Unable to fseek to the right spot"));
  143. return -1;
  144. }
  145. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dx);
  146. /*
  147. * for (ii==0;ii<params->nsizc;ii++) { fprintf(stderr,"ii=%d ",ii);
  148. * fprintf(stderr,"%f ",cell1[ii]); }
  149. * fprintf(stderr,"params->nsizc=%d \n",params->nsizc);
  150. */
  151. if (Rast_put_f_raster_row(cf2, cell1) < 0) {
  152. G_warning(_("Failed writing raster map"));
  153. return -1;
  154. }
  155. }
  156. }
  157. if (params->aspect != NULL) {
  158. fseek(params->Tmp_fd_dy, 0L, 0); /* seek to the beginning */
  159. for (i = 0; i < params->nsizr; i++) {
  160. /* seek to the right row */
  161. if (fseek(params->Tmp_fd_dy, (long)
  162. ((params->nsizr - 1 -
  163. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  164. G_warning(_("Unable to fseek to the right spot"));
  165. return -1;
  166. }
  167. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_dy);
  168. if (Rast_put_f_raster_row(cf3, cell1) < 0) {
  169. G_warning(_("Failed writing raster map"));
  170. return -1;
  171. }
  172. }
  173. }
  174. if (params->pcurv != NULL) {
  175. fseek(params->Tmp_fd_xx, 0L, 0); /* seek to the beginning */
  176. for (i = 0; i < params->nsizr; i++) {
  177. /* seek to the right row */
  178. if (fseek(params->Tmp_fd_xx, (long)
  179. ((params->nsizr - 1 -
  180. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  181. G_warning(_("Unable to fseek to the right spot"));
  182. return -1;
  183. }
  184. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xx);
  185. if (Rast_put_f_raster_row(cf4, cell1) < 0) {
  186. G_warning(_("Failed writing raster map"));
  187. return -1;
  188. }
  189. }
  190. }
  191. if (params->tcurv != NULL) {
  192. fseek(params->Tmp_fd_yy, 0L, 0); /* seek to the beginning */
  193. for (i = 0; i < params->nsizr; i++) {
  194. /* seek to the right row */
  195. if (fseek(params->Tmp_fd_yy, (long)
  196. ((params->nsizr - 1 -
  197. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  198. G_warning(_("Unable to fseek to the right spot"));
  199. return -1;
  200. }
  201. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_yy);
  202. if (Rast_put_f_raster_row(cf5, cell1) < 0) {
  203. G_warning(_("Failed writing raster map"));
  204. return -1;
  205. }
  206. }
  207. }
  208. if (params->mcurv != NULL) {
  209. fseek(params->Tmp_fd_xy, 0L, 0); /* seek to the beginning */
  210. for (i = 0; i < params->nsizr; i++) {
  211. /* seek to the right row */
  212. if (fseek(params->Tmp_fd_xy, (long)
  213. ((params->nsizr - 1 -
  214. i) * params->nsizc * sizeof(FCELL)), 0) == -1) {
  215. G_warning(_("Unable to fseek to the right spot"));
  216. return -1;
  217. }
  218. fread(cell1, sizeof(FCELL), params->nsizc, params->Tmp_fd_xy);
  219. if (Rast_put_f_raster_row(cf6, cell1) < 0) {
  220. G_warning(_("Failed writing raster map"));
  221. return -1;
  222. }
  223. }
  224. }
  225. if (cf1)
  226. Rast_close(cf1);
  227. if (cf2)
  228. Rast_close(cf2);
  229. if (cf3)
  230. Rast_close(cf3);
  231. if (cf4)
  232. Rast_close(cf4);
  233. if (cf5)
  234. Rast_close(cf5);
  235. if (cf6)
  236. Rast_close(cf6);
  237. /* write colormaps and history for output cell files */
  238. /* colortable for elevations */
  239. maps = G_find_file("cell", input, "");
  240. if (params->elev != NULL) {
  241. if (maps == NULL) {
  242. G_warning(_("Raster map <%s> not found"), input);
  243. return -1;
  244. }
  245. Rast_init_colors(&colors2);
  246. /*
  247. * Rast_mark_colors_as_fp(&colors2);
  248. */
  249. if (Rast_read_colors(input, maps, &colors) >= 0) {
  250. if (colors.modular.rules) {
  251. rule = colors.modular.rules;
  252. while (rule->next)
  253. rule = rule->next;
  254. for (; rule; rule = rule->prev) {
  255. value1 = rule->low.value * params->zmult;
  256. value2 = rule->high.value * params->zmult;
  257. Rast_add_modular_d_color_rule(&value1, rule->low.red,
  258. rule->low.grn,
  259. rule->low.blu, &value2,
  260. rule->high.red,
  261. rule->high.grn,
  262. rule->high.blu,
  263. &colors2);
  264. }
  265. }
  266. if (colors.fixed.rules) {
  267. rule = colors.fixed.rules;
  268. while (rule->next)
  269. rule = rule->next;
  270. for (; rule; rule = rule->prev) {
  271. value1 = rule->low.value * params->zmult;
  272. value2 = rule->high.value * params->zmult;
  273. Rast_add_d_color_rule(&value1, rule->low.red,
  274. rule->low.grn, rule->low.blu,
  275. &value2, rule->high.red,
  276. rule->high.grn, rule->high.blu,
  277. &colors2);
  278. }
  279. }
  280. maps = NULL;
  281. maps = G_find_file("cell", params->elev, "");
  282. if (maps == NULL) {
  283. G_warning(_("Raster map <%s> not found"), params->elev);
  284. return -1;
  285. }
  286. if (Rast_write_colors(params->elev, maps, &colors2) < 0) {
  287. G_warning(_("Unable to write color file of raster map <%s>"),
  288. params->elev);
  289. return -1;
  290. }
  291. Rast_quantize_fp_map_range(params->elev, mapset,
  292. zminac - 0.5, zmaxac + 0.5,
  293. (CELL) (zminac - 0.5),
  294. (CELL) (zmaxac + 0.5));
  295. }
  296. else
  297. G_warning(_("No color table for input raster map -- will not create color table"));
  298. }
  299. /* colortable for slopes */
  300. if (cond1 & (!params->deriv)) {
  301. Rast_init_colors(&colors);
  302. val1 = 0;
  303. val2 = 2;
  304. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 0, &colors);
  305. val1 = 2;
  306. val2 = 5;
  307. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  308. val1 = 5;
  309. val2 = 10;
  310. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  311. val1 = 10;
  312. val2 = 15;
  313. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 0, 0, 255, &colors);
  314. val1 = 15;
  315. val2 = 30;
  316. Rast_add_c_color_rule(&val1, 0, 0, 255, &val2, 255, 0, 255, &colors);
  317. val1 = 30;
  318. val2 = 50;
  319. Rast_add_c_color_rule(&val1, 255, 0, 255, &val2, 255, 0, 0, &colors);
  320. val1 = 50;
  321. val2 = 90;
  322. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 0, 0, 0, &colors);
  323. if (params->slope != NULL) {
  324. maps = NULL;
  325. maps = G_find_file("cell", params->slope, "");
  326. if (maps == NULL) {
  327. G_warning(_("Raster map <%s> not found"), params->slope);
  328. return -1;
  329. }
  330. Rast_write_colors(params->slope, maps, &colors);
  331. Rast_quantize_fp_map_range(params->slope, mapset, 0., 90., 0, 90);
  332. type = "raster";
  333. Rast_short_history(params->slope, type, &hist1);
  334. if (params->elev != NULL)
  335. sprintf(hist1.edhist[0], "The elevation map is %s",
  336. params->elev);
  337. sprintf(hist1.datsrc_1, "raster map %s", input);
  338. hist1.edlinecnt = 1;
  339. Rast_write_history(params->slope, &hist1);
  340. }
  341. /* colortable for aspect */
  342. Rast_init_colors(&colors);
  343. val1 = 0;
  344. val2 = 0;
  345. Rast_add_c_color_rule(&val1, 255, 255, 255, &val2, 255, 255, 255, &colors);
  346. val1 = 1;
  347. val2 = 90;
  348. Rast_add_c_color_rule(&val1, 255, 255, 0, &val2, 0, 255, 0, &colors);
  349. val1 = 90;
  350. val2 = 180;
  351. Rast_add_c_color_rule(&val1, 0, 255, 0, &val2, 0, 255, 255, &colors);
  352. val1 = 180;
  353. val2 = 270;
  354. Rast_add_c_color_rule(&val1, 0, 255, 255, &val2, 255, 0, 0, &colors);
  355. val1 = 270;
  356. val2 = 360;
  357. Rast_add_c_color_rule(&val1, 255, 0, 0, &val2, 255, 255, 0, &colors);
  358. if (params->aspect != NULL) {
  359. maps = NULL;
  360. maps = G_find_file("cell", params->aspect, "");
  361. if (maps == NULL) {
  362. G_warning(_("Raster map <%s> not found"), params->aspect);
  363. return -1;
  364. }
  365. Rast_write_colors(params->aspect, maps, &colors);
  366. Rast_quantize_fp_map_range(params->aspect, mapset, 0., 360., 0, 360);
  367. type = "raster";
  368. Rast_short_history(params->aspect, type, &hist2);
  369. if (params->elev != NULL)
  370. sprintf(hist2.edhist[0], "The elevation map is %s",
  371. params->elev);
  372. sprintf(hist2.datsrc_1, "raster map %s", input);
  373. hist2.edlinecnt = 1;
  374. Rast_write_history(params->aspect, &hist2);
  375. }
  376. /* colortable for curvatures */
  377. if (cond2) {
  378. Rast_init_colors(&colors);
  379. dat1 = (FCELL) amin1(c1min, c2min);
  380. dat2 = (FCELL) - 0.01;
  381. Rast_add_f_color_rule(&dat1, 50, 0, 155,
  382. &dat2, 0, 0, 255, &colors);
  383. dat1 = dat2;
  384. dat2 = (FCELL) - 0.001;
  385. Rast_add_f_color_rule(&dat1, 0, 0, 255,
  386. &dat2, 0, 127, 255, &colors);
  387. dat1 = dat2;
  388. dat2 = (FCELL) - 0.00001;
  389. Rast_add_f_color_rule(&dat1, 0, 127, 255,
  390. &dat2, 0, 255, 255, &colors);
  391. dat1 = dat2;
  392. dat2 = (FCELL) 0.00;
  393. Rast_add_f_color_rule(&dat1, 0, 255, 255,
  394. &dat2, 200, 255, 200, &colors);
  395. dat1 = dat2;
  396. dat2 = (FCELL) 0.00001;
  397. Rast_add_f_color_rule(&dat1, 200, 255, 200,
  398. &dat2, 255, 255, 0, &colors);
  399. dat1 = dat2;
  400. dat2 = (FCELL) 0.001;
  401. Rast_add_f_color_rule(&dat1, 255, 255, 0,
  402. &dat2, 255, 127, 0, &colors);
  403. dat1 = dat2;
  404. dat2 = (FCELL) 0.01;
  405. Rast_add_f_color_rule(&dat1, 255, 127, 0,
  406. &dat2, 255, 0, 0, &colors);
  407. dat1 = dat2;
  408. dat2 = (FCELL) amax1(c1max, c2max);
  409. Rast_add_f_color_rule(&dat1, 255, 0, 0,
  410. &dat2, 155, 0, 20, &colors);
  411. maps = NULL;
  412. if (params->pcurv != NULL) {
  413. maps = G_find_file("cell", params->pcurv, "");
  414. if (maps == NULL) {
  415. G_warning(_("Raster map <%s> not found"), params->pcurv);
  416. return -1;
  417. }
  418. Rast_write_colors(params->pcurv, maps, &colors);
  419. fprintf(stderr, "color map written\n");
  420. Rast_quantize_fp_map_range(params->pcurv, mapset,
  421. dat1, dat2,
  422. (CELL) (dat1 * MULT),
  423. (CELL) (dat2 * MULT));
  424. type = "raster";
  425. Rast_short_history(params->pcurv, type, &hist3);
  426. if (params->elev != NULL)
  427. sprintf(hist3.edhist[0], "The elevation map is %s",
  428. params->elev);
  429. sprintf(hist3.datsrc_1, "raster map %s", input);
  430. hist3.edlinecnt = 1;
  431. Rast_write_history(params->pcurv, &hist3);
  432. }
  433. if (params->tcurv != NULL) {
  434. maps = NULL;
  435. maps = G_find_file("cell", params->tcurv, "");
  436. if (maps == NULL) {
  437. G_warning(_("Raster map <%s> not found"), params->tcurv);
  438. return -1;
  439. }
  440. Rast_write_colors(params->tcurv, maps, &colors);
  441. Rast_quantize_fp_map_range(params->tcurv, mapset,
  442. dat1, dat2, (CELL) (dat1 * MULT),
  443. (CELL) (dat2 * MULT));
  444. type = "raster";
  445. Rast_short_history(params->tcurv, type, &hist4);
  446. if (params->elev != NULL)
  447. sprintf(hist4.edhist[0], "The elevation map is %s",
  448. params->elev);
  449. sprintf(hist4.datsrc_1, "raster map %s", input);
  450. hist4.edlinecnt = 1;
  451. Rast_write_history(params->tcurv, &hist4);
  452. }
  453. if (params->mcurv != NULL) {
  454. maps = NULL;
  455. maps = G_find_file("cell", params->mcurv, "");
  456. if (maps == NULL) {
  457. G_warning(_("Raster map <%s> not found"), params->mcurv);
  458. return -1;
  459. }
  460. Rast_write_colors(params->mcurv, maps, &colors);
  461. Rast_quantize_fp_map_range(params->mcurv, mapset,
  462. dat1, dat2,
  463. (CELL) (dat1 * MULT),
  464. (CELL) (dat2 * MULT));
  465. type = "raster";
  466. Rast_short_history(params->mcurv, type, &hist5);
  467. if (params->elev != NULL)
  468. sprintf(hist5.edhist[0], "The elevation map is %s",
  469. params->elev);
  470. sprintf(hist5.datsrc_1, "raster map %s", input);
  471. hist5.edlinecnt = 1;
  472. Rast_write_history(params->mcurv, &hist5);
  473. }
  474. }
  475. }
  476. if (params->elev != NULL) {
  477. maps = G_find_file("cell", params->elev, "");
  478. if (maps == NULL) {
  479. G_warning(_("Raster map <%s> not found"), params->elev);
  480. return -1;
  481. }
  482. Rast_short_history(params->elev, "raster", &hist);
  483. if (smooth != NULL)
  484. sprintf(hist.edhist[0], "tension=%f, smoothing=%s",
  485. params->fi * 1000. / (*dnorm), smooth);
  486. else
  487. sprintf(hist.edhist[0], "tension=%f",
  488. params->fi * 1000. / (*dnorm));
  489. sprintf(hist.edhist[1], "dnorm=%f, zmult=%f", *dnorm, params->zmult);
  490. sprintf(hist.edhist[2], "KMAX=%d, KMIN=%d, errtotal=%f", params->kmax,
  491. params->kmin, sqrt(ertot / n_points));
  492. sprintf(hist.edhist[3], "zmin_data=%f, zmax_data=%f", zmin, zmax);
  493. sprintf(hist.edhist[4], "zmin_int=%f, zmax_int=%f", zminac, zmaxac);
  494. sprintf(hist.datsrc_1, "raster map %s", input);
  495. hist.edlinecnt = 5;
  496. Rast_write_history(params->elev, &hist);
  497. }
  498. /* change region to initial region */
  499. G_verbose_message(_("Changing the region back to initial..."));
  500. if (Rast_set_window(winhd) < 0) {
  501. G_warning(_("Unable to set region"));
  502. return -1;
  503. }
  504. return 1;
  505. }