mv_wind.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. ************************************************************
  3. * MODULE: r.le.setup/mv_wind.c *
  4. * Version 5.0beta Oct. 1, 2001 *
  5. * *
  6. * AUTHOR: W.L. Baker, University of Wyoming *
  7. * BAKERWL@UWYO.EDU *
  8. * *
  9. * PURPOSE: To set up sampling areas, which can can then *
  10. * be used to obtain data using the r.le.dist, *
  11. * r.le.patch, and r.le.pixel programs. The *
  12. * mv_wind.c code queries the user for information *
  13. * needed to setup the moving window *
  14. * *
  15. * COPYRIGHT: (C) 2001 by W.L. Baker *
  16. * *
  17. * This program is free software under the GNU General *
  18. * Public License(>=v2). Read the file COPYING that comes *
  19. * with GRASS for details *
  20. * *
  21. ************************************************************/
  22. #include <grass/display.h>
  23. #include "setup.h"
  24. #include <grass/config.h>
  25. /* SETUP THE PARAMETERS FOR THE
  26. MOVING WINDOW */
  27. void mov_wind(int t, int b, int l, int r, char *n1, char *n2, char *n3,
  28. double *mx)
  29. {
  30. int xp = 0, yp = 0, x1, y1, btn = 0, s, l0, t0, xpl, ypt,
  31. u_w = 0, u_l = 0, u_w0, u_l0, w_w = 0, w_l = 0, initl = 0,
  32. initt = 0, d, fmask, j, circle = 0;
  33. register int i;
  34. double tmp[2], tmp1, radius = 0.0;
  35. FILE *fp;
  36. /*
  37. mx[0] = cols of region/width of screen
  38. mx[1] = rows of region/height of screen
  39. t = top row of sampling frame
  40. b = bottom row of sampling frame
  41. l = left col of sampling frame
  42. r = right col of sampling frame
  43. */
  44. /* open the moving window parameter file */
  45. fp = fopen0("r.le.para/move_wind", "w");
  46. G_sleep_on_error(0);
  47. initl = l;
  48. initt = t;
  49. l = (int)((double)(l * mx[0]) + 0.5);
  50. r = (int)((double)(r * mx[0]) + 0.5);
  51. t = (int)((double)(t * mx[1]) + 0.5);
  52. b = (int)((double)(b * mx[1]) + 0.5);
  53. /* display sampling frame */
  54. R_open_driver();
  55. R_standard_color(D_translate_color("grey"));
  56. draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5),
  57. (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1);
  58. R_close_driver();
  59. /* determine whether the user will use
  60. the keyboard or mouse to setup the
  61. moving window */
  62. keyboard:
  63. fprintf(stderr, "\n\n HOW WILL YOU SPECIFY THE MOVING WINDOW?\n");
  64. fprintf(stderr,
  65. "\n Use keyboard to enter moving window dimensions 1");
  66. fprintf(stderr,
  67. "\n Use mouse to draw moving window 2\n");
  68. fprintf(stderr,
  69. "\n Which Number? ");
  70. numtrap(1, &tmp1);
  71. d = (int)(tmp1);
  72. if (d < 1 || d > 2) {
  73. fprintf(stderr, " You did not enter a 1 or 2, try again\n");
  74. goto keyboard;
  75. }
  76. if (d == 1 || d == 2) {
  77. /* return a value > 0 to fmask if there is
  78. a MASK present */
  79. fprintf(stderr,
  80. "\n If a MASK is not present (see r.mask) a beep may sound\n");
  81. fprintf(stderr,
  82. " and a WARNING may be printed that can be ignored.\n");
  83. fprintf(stderr,
  84. " If a MASK is present there will be no warning.\n");
  85. fmask = Rast_open_old("MASK", G_mapset());
  86. fprintf(stderr, "\n");
  87. /* setup the moving window using keyboard */
  88. if (d == 1) {
  89. /* if sampling using circles */
  90. fprintf(stderr, "\n Do you want to sample using rectangles");
  91. if (!G_yes
  92. ("\n (including squares) (y) or circles (n)? ", 1)) {
  93. fprintf(stderr,
  94. "\n What radius do you want for the circles? Radius");
  95. fprintf(stderr,
  96. "\n is in pixels; add 0.5 pixels, for the center");
  97. fprintf(stderr,
  98. "\n pixel, to the number of pixels outside the");
  99. fprintf(stderr,
  100. "\n center pixel. Type a real number with one");
  101. fprintf(stderr,
  102. "\n decimal place ending in .5 (e.g., 4.5): ");
  103. numtrap(1, &radius);
  104. u_w = (int)(2 * radius);
  105. u_l = (int)(2 * radius);
  106. u_w0 = u_w / mx[0];
  107. u_l0 = u_l / mx[1];
  108. }
  109. /* if sampling using rectangles/squares */
  110. else {
  111. back:
  112. fprintf(stderr,
  113. "\n Enter number of COLUMNS & ROWS for the dimensions of");
  114. fprintf(stderr,
  115. "\n the moving window (e.g., 10 10): ");
  116. numtrap(2, tmp);
  117. u_w = fabs(tmp[0]);
  118. u_l = fabs(tmp[1]);
  119. u_w0 = fabs(tmp[0]) / mx[0];
  120. u_l0 = fabs(tmp[1]) / mx[1];
  121. /* trap possible errors in dimensions */
  122. if (!u_w0 || !u_l0) {
  123. fprintf(stderr,
  124. "\n You entered a dimension as 0; enter dimensions again\n");
  125. goto back;
  126. }
  127. else if (u_w == 1 && u_l == 1) {
  128. fprintf(stderr,
  129. "\n You entered dimensions as 1 1; This will not produce");
  130. fprintf(stderr,
  131. "\n meaningful results; enter larger dimensions\n");
  132. goto back;
  133. }
  134. else if (u_w >= r || u_l >= b) {
  135. fprintf(stderr,
  136. "\n Window size you chose allows < 2 windows across each row;");
  137. fprintf(stderr,
  138. "\n please make window dimensions smaller\n");
  139. goto back;
  140. }
  141. }
  142. /* display the user-defined moving
  143. window on the map */
  144. R_open_driver();
  145. R_standard_color(D_translate_color("red"));
  146. if (radius) {
  147. draw_circle(initl, initt, initl + u_w0, initt + u_l0, 3);
  148. }
  149. else {
  150. draw_box(initl, initt, initl + u_w0, initt + u_l0, 1);
  151. }
  152. R_close_driver();
  153. /* if all is OK, then set window dimensions */
  154. fprintf(stderr,
  155. "\n Is the displayed moving window as you wanted it (y) or");
  156. if (G_yes("\n do you want to redo it? (n) ", 1)) {
  157. xp = (int)(u_w0);
  158. yp = (int)(u_l0);
  159. }
  160. else {
  161. paint_map(n1, n2, n3);
  162. R_open_driver();
  163. R_standard_color(D_translate_color("grey"));
  164. draw_box((int)(l / mx[0] + 0.5), (int)(t / mx[1] + 0.5),
  165. (int)(r / mx[0] + 0.5), (int)(b / mx[1] + 0.5), 1);
  166. R_close_driver();
  167. radius = 0.0;
  168. goto keyboard;
  169. }
  170. }
  171. /* setup the moving window using the mouse */
  172. else if (d == 2) {
  173. G_system("clear");
  174. /* if sampling using circles */
  175. fprintf(stderr,
  176. "\n\n Do you want to use a rectangular (including squares) (y)");
  177. if (!G_yes("\n or circular (n) moving window? ", 1)) {
  178. circle = 1;
  179. fprintf(stderr,
  180. "\n Draw a rectangular area to contain a circular moving window.");
  181. fprintf(stderr,
  182. "\n First select upper left corner, then lower right:\n");
  183. fprintf(stderr, " Left button: Check unit size\n");
  184. fprintf(stderr,
  185. " Middle button: Upper left corner of area here\n");
  186. fprintf(stderr,
  187. " Right button: Lower right corner of area here\n");
  188. }
  189. else {
  190. circle = 0;
  191. fprintf(stderr,
  192. "\n Draw a rectangular (or square) moving window");
  193. fprintf(stderr,
  194. "\n First select upper left corner, then lower right:\n");
  195. fprintf(stderr,
  196. " Left button: Check moving window size\n");
  197. fprintf(stderr,
  198. " Middle button: Upper left corner of window here\n");
  199. fprintf(stderr,
  200. " Right: button: Lower right corner of window here\n");
  201. }
  202. R_open_driver();
  203. while (btn != 3) {
  204. back1:
  205. R_get_location_with_box(l, t, &xp, &yp, &btn);
  206. u_w = (int)((double)(xp - l) * mx[0] + 0.5);
  207. u_l = (int)((double)(yp - t) * mx[1] + 0.5);
  208. if (btn == 1) { /** show the size and ratio **/
  209. fprintf(stderr,
  210. " Window would be %d columns wide by %d rows long\n",
  211. u_w, u_l);
  212. fprintf(stderr,
  213. " Width/length would be %5.2f and area %d pixels\n",
  214. (float)u_w / u_l, u_w * u_l);
  215. for (i = 0; i < 120; i++)
  216. fprintf(stderr, "\b");
  217. }
  218. else if (btn == 2) {
  219. R_move_abs(xp, yp);
  220. l0 = l;
  221. t0 = t;
  222. l = xp;
  223. t = yp;
  224. }
  225. else if (btn == 3) {
  226. xpl =
  227. (int)((double)((int)((double)(xp - l) * mx[0] + 0.5))
  228. / mx[0]);
  229. ypt =
  230. (int)((double)((int)((double)(yp - t) * mx[1] + 0.5))
  231. / mx[1]);
  232. if (xpl < 0 || ypt < 0) {
  233. fprintf(stderr,
  234. "\n You did not put lower right corner below and to the");
  235. fprintf(stderr,
  236. "\n of upper left corner. Please select lower right");
  237. fprintf(stderr, "\n corner again");
  238. goto back1;
  239. }
  240. else if (xpl == 0 || ypt == 0) {
  241. fprintf(stderr,
  242. "\n\n Window would have 0 rows and/or 0 columns;");
  243. fprintf(stderr, " try again\n");
  244. goto back1;
  245. }
  246. else if (xpl > 0 && ypt > 0) {
  247. R_standard_color(D_translate_color("red"));
  248. if (circle) {
  249. if (xpl > ypt)
  250. xpl = ypt;
  251. else if (ypt > xpl)
  252. ypt = xpl;
  253. u_w = (int)((double)xpl * mx[0] + 0.5);
  254. u_l = (int)((double)ypt * mx[1] + 0.5);
  255. draw_circle(initl, initt, initl + xpl,
  256. initt + ypt, 3);
  257. }
  258. else
  259. draw_box(initl, initt, initl + xpl, initt + ypt,
  260. 1);
  261. G_system("clear");
  262. if (circle) {
  263. radius = (float)u_w / 2.0;
  264. fprintf(stderr,
  265. "\n\n Circular moving window has radius = %5.2f pixels\n",
  266. radius);
  267. }
  268. else {
  269. fprintf(stderr,
  270. "\n Rectangular moving window has %d columns and %d rows",
  271. u_w, u_l);
  272. fprintf(stderr,
  273. "\n with width/length ratio of %5.2f and area of %d pixels\n",
  274. (float)u_w / u_l, u_w * u_l);
  275. }
  276. }
  277. }
  278. }
  279. R_close_driver();
  280. l = l0;
  281. t = t0;
  282. }
  283. Rast_close(fmask);
  284. }
  285. /* if neither, then exit */
  286. else
  287. exit(0);
  288. /* write the moving window parameters
  289. into the r.le.para/move_wind file */
  290. fprintf(fp, "%8d%8d u_w u_l: CELL\n", u_w, u_l);
  291. w_w = r - l;
  292. w_l = b - t;
  293. /* write the radius of circles, if a
  294. circular moving window is to be used */
  295. fprintf(fp, "%8.1f radius of circular moving window\n", radius);
  296. /* write the search area in the
  297. r.le.para/move_wind file */
  298. fprintf(fp, "%8d%8d w_w w_l\n", w_w, w_l);
  299. fprintf(fp, "%8d%8d x0, y0\n", (int)((double)(initl) * mx[0] + 0.5),
  300. (int)((double)(initt) * mx[1] + 0.5));
  301. fclose(fp);
  302. return;
  303. }