msurements.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <grass/gis.h>
  2. #include <grass/display.h>
  3. #include "local_proto.h"
  4. FILE *output;
  5. int measurements(int color1, int color2, int s_flag, int m_flag, int k_flag)
  6. {
  7. double *x, *y;
  8. int npoints, nalloc;
  9. double area;
  10. double cur_ux, cur_uy;
  11. double length;
  12. double ux, uy;
  13. int button;
  14. int cur_screen_x, cur_screen_y;
  15. int screen_x, screen_y;
  16. struct Cell_head window;
  17. double t, b, l, r;
  18. nalloc = 128;
  19. x = (double *)G_calloc(nalloc, sizeof(double));
  20. y = (double *)G_calloc(nalloc, sizeof(double));
  21. /* Use stderr for TCLTK-Output */
  22. if (s_flag)
  23. output = stderr;
  24. else
  25. output = stdout;
  26. /* Set up area/distance calculations */
  27. G_begin_polygon_area_calculations();
  28. G_begin_distance_calculations();
  29. G_get_window(&window);
  30. D_get_dst(&t, &b, &l, &r);
  31. D_do_conversions(&window, t, b, l, r);
  32. for (;;) {
  33. npoints = 0;
  34. if (!s_flag)
  35. G_clear_screen();
  36. fprintf(output, "\nButtons:\n");
  37. fprintf(output, "Left: where am i\n");
  38. fprintf(output, "Middle: set FIRST vertex\n");
  39. fprintf(output, "Right: quit this\n");
  40. screen_y = (t + b) / 2;
  41. screen_x = (l + r) / 2;
  42. do {
  43. R_get_location_with_pointer(&screen_x, &screen_y, &button);
  44. cur_uy = D_d_to_u_row((double)screen_y);
  45. cur_ux = D_d_to_u_col((double)screen_x);
  46. if (button == 1)
  47. print_en(cur_ux, cur_uy, s_flag);
  48. if (button == 3)
  49. return (0);
  50. } while (button != 2);
  51. add_point(&x, &y, &npoints, &nalloc, cur_ux, cur_uy);
  52. if (!s_flag)
  53. G_clear_screen();
  54. fprintf(output, "\nLeft: where am i\n");
  55. fprintf(output, "Middle: set NEXT vertex\n");
  56. fprintf(output, "Right: FINISH\n");
  57. R_move_abs(screen_x, screen_y);
  58. cur_screen_x = screen_x;
  59. cur_screen_y = screen_y;
  60. length = 0.0;
  61. do {
  62. D_use_color(color1);
  63. R_get_location_with_line(cur_screen_x, cur_screen_y, &screen_x,
  64. &screen_y, &button);
  65. uy = D_d_to_u_row((double)screen_y);
  66. ux = D_d_to_u_col((double)screen_x);
  67. if (button == 1) {
  68. print_en(ux, uy, s_flag);
  69. }
  70. else if (button == 2) {
  71. draw_line(screen_x, screen_y, cur_screen_x, cur_screen_y,
  72. color1, color2);
  73. add_point(&x, &y, &npoints, &nalloc, ux, uy);
  74. length += G_distance(cur_ux, cur_uy, ux, uy);
  75. print_length(length, s_flag, k_flag);
  76. cur_screen_x = screen_x;
  77. cur_screen_y = screen_y;
  78. cur_ux = ux;
  79. cur_uy = uy;
  80. }
  81. } while (button != 3);
  82. R_flush();
  83. if (!s_flag)
  84. G_clear_screen();
  85. fprintf(output, "\nButtons:\n");
  86. fprintf(output, "Left: DO ANOTHER\n");
  87. fprintf(output, "Middle:\n");
  88. fprintf(output, "Right: quit this\n");
  89. /*
  90. * 10000 is sq meters per hectare
  91. * 2589988 is sq meters per sq mile
  92. */
  93. fprintf(output, "\n");
  94. print_length(length, s_flag, k_flag);
  95. if (npoints > 3) {
  96. area = G_area_of_polygon(x, y, npoints);
  97. if (!m_flag) {
  98. fprintf(output, "AREA: %10.2f hectares\n", area / 10000);
  99. fprintf(output, "AREA: %10.4f square miles\n",
  100. area / 2589988.11);
  101. }
  102. fprintf(output, "AREA: %10.2f square meters\n", area);
  103. if (k_flag)
  104. fprintf(output, "AREA: %10.4f square kilometers\n",
  105. area / 1000000);
  106. }
  107. R_get_location_with_pointer(&screen_x, &screen_y, &button);
  108. if (button == 3)
  109. return (0);
  110. }
  111. return 0;
  112. }
  113. int print_en(double e, double n, int s_flag)
  114. {
  115. char buf[100];
  116. /* Use stderr for TCLTK-Output */
  117. if (s_flag)
  118. output = stderr;
  119. else
  120. output = stdout;
  121. G_format_easting(e, buf, G_projection());
  122. fprintf(output, "EAST: %s\n", buf);
  123. G_format_northing(n, buf, G_projection());
  124. fprintf(output, "NORTH: %s\n", buf);
  125. return 0;
  126. }
  127. int print_length(double length, int s_flag, int k_flag)
  128. {
  129. /* Use stderr for TCLTK-Output */
  130. if (s_flag)
  131. output = stderr;
  132. else
  133. output = stdout;
  134. fprintf(output, "LEN: %10.2f meters\n", length);
  135. if (k_flag)
  136. fprintf(output, "LEN: %10.4f kilometers\n", length / 1000);
  137. return 0;
  138. }
  139. int add_point(double **x, double **y,
  140. int *npoints, int *nalloc, double ux, double uy)
  141. {
  142. double *px, *py;
  143. px = *x;
  144. py = *y;
  145. if (*npoints >= *nalloc) {
  146. *nalloc *= 2;
  147. *x = px = (double *)G_realloc(px, *nalloc * sizeof(double));
  148. *y = py = (double *)G_realloc(py, *nalloc * sizeof(double));
  149. }
  150. px[*npoints] = ux;
  151. py[*npoints] = uy;
  152. *npoints += 1;
  153. return 0;
  154. }