clean_topo.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <grass/gis.h>
  5. #include <grass/vector.h>
  6. #include <grass/glocale.h>
  7. #include "sw_defs.h"
  8. #include "defs.h"
  9. int clean_topo(void)
  10. {
  11. int type, line, nlines;
  12. int area, nareas;
  13. int verbose;
  14. int err_boundaries, err_centr_out, err_centr_dupl, err_nocentr;
  15. struct bound_box box;
  16. double snap_thresh;
  17. /* cleaning part 1: count errors */
  18. G_message(_("Searching for topology errors..."));
  19. verbose = G_verbose();
  20. G_set_verbose(0);
  21. Vect_build_partial(&Out, GV_BUILD_CENTROIDS);
  22. G_set_verbose(verbose);
  23. err_boundaries = err_centr_out = err_centr_dupl = err_nocentr = 0;
  24. nlines = Vect_get_num_lines(&Out);
  25. for (line = 1; line <= nlines; line++) {
  26. if (!Vect_line_alive(&Out, line))
  27. continue;
  28. type = Vect_get_line_type(&Out, line);
  29. if (type == GV_BOUNDARY) {
  30. int left, right;
  31. Vect_get_line_areas(&Out, line, &left, &right);
  32. if (left == 0 || right == 0) {
  33. G_debug(3, "line = %d left = %d right = %d", line,
  34. left, right);
  35. err_boundaries++;
  36. }
  37. }
  38. if (type == GV_CENTROID) {
  39. area = Vect_get_centroid_area(&Out, line);
  40. if (area == 0)
  41. err_centr_out++;
  42. else if (area < 0)
  43. err_centr_dupl++;
  44. }
  45. }
  46. err_nocentr = 0;
  47. nareas = Vect_get_num_areas(&Out);
  48. for (area = 1; area <= nareas; area++) {
  49. if (!Vect_area_alive(&Out, area))
  50. continue;
  51. line = Vect_get_area_centroid(&Out, area);
  52. if (line == 0)
  53. err_nocentr++;
  54. }
  55. /* cleaning part 2: snap */
  56. /* TODO: adjust snapping treshold to ULP */
  57. Vect_get_map_box(&Out, &box);
  58. snap_thresh = fabs(box.W);
  59. if (snap_thresh < fabs(box.E))
  60. snap_thresh = fabs(box.E);
  61. if (snap_thresh < fabs(box.N))
  62. snap_thresh = fabs(box.N);
  63. if (snap_thresh < fabs(box.S))
  64. snap_thresh = fabs(box.S);
  65. snap_thresh = d_ulp(snap_thresh);
  66. if (err_nocentr || err_centr_dupl || err_centr_out) {
  67. int nmod;
  68. G_important_message(_("Cleaning output topology"));
  69. Vect_snap_lines(&Out, GV_BOUNDARY, snap_thresh, NULL);
  70. do {
  71. Vect_break_lines(&Out, GV_BOUNDARY, NULL);
  72. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  73. nmod =
  74. Vect_clean_small_angles_at_nodes(&Out, GV_BOUNDARY, NULL);
  75. } while (nmod > 0);
  76. G_message(_("Removing dangles..."));
  77. Vect_remove_dangles(&Out, GV_BOUNDARY, -1.0, NULL);
  78. G_message(_("Removing bridges..."));
  79. Vect_remove_bridges(&Out, NULL, NULL, NULL);
  80. err_boundaries = 0;
  81. nlines = Vect_get_num_lines(&Out);
  82. for (line = 1; line <= nlines; line++) {
  83. if (!Vect_line_alive(&Out, line))
  84. continue;
  85. type = Vect_get_line_type(&Out, line);
  86. if (type == GV_BOUNDARY) {
  87. int left, right;
  88. Vect_get_line_areas(&Out, line, &left, &right);
  89. if (left == 0 || right == 0) {
  90. G_debug(3, "line = %d left = %d right = %d", line,
  91. left, right);
  92. err_boundaries++;
  93. }
  94. }
  95. }
  96. }
  97. /* cleaning part 3: remove remaining incorrect boundaries */
  98. if (err_boundaries) {
  99. G_important_message(_("Removing incorrect boundaries from output"));
  100. nlines = Vect_get_num_lines(&Out);
  101. for (line = 1; line <= nlines; line++) {
  102. if (!Vect_line_alive(&Out, line))
  103. continue;
  104. type = Vect_get_line_type(&Out, line);
  105. if (type == GV_BOUNDARY) {
  106. int left, right;
  107. Vect_get_line_areas(&Out, line, &left, &right);
  108. /* &&, not ||, no typo */
  109. if (left == 0 && right == 0) {
  110. G_debug(3, "line = %d left = %d right = %d", line,
  111. left, right);
  112. Vect_delete_line(&Out, line);
  113. }
  114. }
  115. }
  116. }
  117. /* this is slow:
  118. if (in_area) {
  119. if (Type == GV_LINE)
  120. G_message(_("Merging lines ..."));
  121. else
  122. G_message(_("Merging boundaries ..."));
  123. Vect_merge_lines(&Out, Type, NULL, NULL);
  124. }
  125. */
  126. return 1;
  127. }