process_left.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "distance.h"
  21. int process_left(int from_row, int to_row, int start_col, int first_zone)
  22. {
  23. register int i, col, cur_zone;
  24. register MAPTYPE *to_ptr, *from_ptr;
  25. register int ncols, incr, farthest;
  26. /* find cells to the left
  27. * stop at left edge, or when ncols is bigger than the last zone,
  28. * or when we see a 1 in the map
  29. */
  30. col = start_col;
  31. from_ptr = map + MAPINDEX(from_row, col);
  32. to_ptr = map + MAPINDEX(to_row, col);
  33. farthest = distances[ndist - 1].ncols;
  34. /* planimetric grids will look for ncols^2
  35. * and can use fact that (n+1)^2 = n^2 + 2n + 1
  36. */
  37. if (window.proj != PROJECTION_LL)
  38. incr = 1;
  39. else
  40. incr = 0;
  41. ncols = 0;
  42. while (1) {
  43. if (col == 0) { /* global wrap-around */
  44. if (!wrap_ncols)
  45. break; /* only can happen with lat-lon */
  46. col = window.cols;
  47. ncols += wrap_ncols - 1;
  48. from_ptr = map + MAPINDEX(from_row, col);
  49. to_ptr = map + MAPINDEX(to_row, col);
  50. }
  51. col--;
  52. if (incr) {
  53. ncols += incr;
  54. incr += 2;
  55. }
  56. else
  57. ncols++;
  58. if (ncols > farthest)
  59. break;
  60. if (*--from_ptr == 1)
  61. break;
  62. /* convert 1,2,3,4 to -1,0,1,2 etc. 0 becomes ndist */
  63. if ((cur_zone = *--to_ptr))
  64. cur_zone -= ZONE_INCR;
  65. else
  66. cur_zone = ndist;
  67. /* find the first zone that is closer than the current value */
  68. for (i = first_zone; i < cur_zone; i++) {
  69. if (distances[i].ncols >= ncols) {
  70. *to_ptr = (first_zone = i) + ZONE_INCR;
  71. break;
  72. }
  73. }
  74. }
  75. return 0;
  76. }