find_dist.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /****************************************************************************
  2. *
  3. * MODULE: r.buffer
  4. *
  5. * AUTHOR(S): Michael Shapiro - CERL
  6. *
  7. * PURPOSE: This program creates distance zones from non-zero
  8. * cells in a grid layer. Distances are specified in
  9. * meters (on the command-line). Window does not have to
  10. * have square cells. Works both for planimetric
  11. * (UTM, State Plane) and lat-long.
  12. *
  13. * COPYRIGHT: (C) 2005 by the GRASS Development Team
  14. *
  15. * This program is free software under the GNU General Public
  16. * License (>=v2). Read the file COPYING that comes with GRASS
  17. * for details.
  18. *
  19. ****************************************************************************/
  20. #include <grass/gis.h>
  21. #include "distance.h"
  22. #include "local_proto.h"
  23. static int cur_row;
  24. int begin_distance(int row)
  25. {
  26. cur_row = row;
  27. if (window.proj == PROJECTION_LL)
  28. G_set_geodesic_distance_lat1(window.north -
  29. (row + .5) * window.ns_res);
  30. return 0;
  31. }
  32. /* Determine number of columns for each distance zone
  33. * (-1 means doesn't occur)
  34. * Returns: first zone to occur (-1 if none)
  35. * Note: modifies distances structure (global)
  36. */
  37. int find_distances(int row)
  38. {
  39. int i;
  40. double dist;
  41. double scale;
  42. double ns_dist;
  43. if (window.proj == PROJECTION_LL)
  44. G_set_geodesic_distance_lat2(window.north -
  45. (row + .5) * window.ns_res);
  46. /* if at same row, distance is constant across each cell */
  47. if (row == cur_row) {
  48. if (window.proj == PROJECTION_LL) {
  49. scale = 1.0 / G_geodesic_distance_lon_to_lon(0.0, window.ew_res);
  50. for (i = 0; i < ndist; i++) {
  51. distances[i].prev_ncols = (int)(scale * distances[i].dist);
  52. distances[i].ncols = find_ll_distance_ncols(i);
  53. }
  54. }
  55. else { /* note - see parse_dist.c for more info */
  56. for (i = 0; i < ndist; i++)
  57. distances[i].ncols = distances[i].prev_ncols
  58. = (int)distances[i].dist;
  59. }
  60. }
  61. else {
  62. if (window.proj == PROJECTION_LL)
  63. for (i = 0; i < ndist; i++)
  64. distances[i].ncols = find_ll_distance_ncols(i);
  65. else {
  66. i = cur_row - row;
  67. ns_dist = (i * i) * ns_to_ew_squared;
  68. for (i = 0; i < ndist; i++) {
  69. dist = distances[i].dist - ns_dist;
  70. if (dist < 0.0)
  71. distances[i].ncols = -1;
  72. else
  73. distances[i].ncols = (int)dist;
  74. }
  75. }
  76. }
  77. for (i = 0; i < ndist; i++)
  78. if (distances[i].ncols >= 0)
  79. return i;
  80. return -1;
  81. }
  82. int reset_distances(void)
  83. {
  84. register int i;
  85. for (i = 0; i < ndist; i++)
  86. distances[i].ncols = distances[i].prev_ncols;
  87. return 0;
  88. }
  89. int find_ll_distance_ncols(int i)
  90. {
  91. int col;
  92. double lon;
  93. register double d;
  94. register double dist;
  95. /* use the ncols from previous distances as a starting point
  96. * to figure the new ncols
  97. */
  98. col = distances[i].ncols - 1;
  99. if (col < 0)
  100. col = 0;
  101. dist = distances[i].dist;
  102. lon = window.ew_res * col;
  103. d = G_geodesic_distance_lon_to_lon(0.0, lon);
  104. if (d > dist) { /*backup */
  105. while (d > dist) {
  106. if (--col < 0)
  107. break;
  108. lon -= window.ew_res;
  109. d = G_geodesic_distance_lon_to_lon(0.0, lon);
  110. }
  111. return col;
  112. }
  113. if (d == dist)
  114. return col;
  115. while (d < dist && lon < 180.00 && col <= window.cols) {
  116. col++;
  117. lon += window.ew_res;
  118. d = G_geodesic_distance_lon_to_lon(0.0, lon);
  119. }
  120. return col - 1;
  121. }