remove_areas.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*!
  2. \file remove_areas.c
  3. \brief Vector library - clean geometry (remove small areas)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2008 by the GRASS Development Team
  6. This program is free software under the
  7. GNU General Public License (>=v2).
  8. Read the file COPYING that comes with GRASS
  9. for details.
  10. \author Radim Blazek
  11. \date 2001
  12. */
  13. #include <stdlib.h>
  14. #include <grass/gis.h>
  15. #include <grass/Vect.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Remove small areas from the map map.
  19. Centroid of the area and the longest boundary
  20. with adjacent area is removed.
  21. Map topology must be built GV_BUILD_CENTROIDS.
  22. \param Map vector map
  23. \param thresh maximum area size for removed areas
  24. \param Err vector map where removed lines and centroids are written
  25. \param msgout file pointer where messages will be written or NULL
  26. \param removed_area pointer to where total size of removed area is stored or NULL
  27. \return number of removed areas
  28. */
  29. int
  30. Vect_remove_small_areas(struct Map_info *Map, double thresh,
  31. struct Map_info *Err, FILE * msgout,
  32. double *removed_area)
  33. {
  34. int area;
  35. int nremoved = 0;
  36. struct ilist *List;
  37. struct ilist *AList;
  38. struct line_pnts *Points;
  39. struct line_cats *Cats;
  40. double size_removed = 0.0;
  41. List = Vect_new_list();
  42. AList = Vect_new_list();
  43. Points = Vect_new_line_struct();
  44. Cats = Vect_new_cats_struct();
  45. if (msgout)
  46. fprintf(msgout, "%s: %5d", _("Removed areas"), nremoved);
  47. for (area = 1; area <= Vect_get_num_areas(Map); area++) {
  48. int i, j, centroid, dissolve_neighbour;
  49. double length, size;
  50. G_debug(3, "area = %d", area);
  51. if (!Vect_area_alive(Map, area))
  52. continue;
  53. size = Vect_get_area_area(Map, area);
  54. if (size > thresh)
  55. continue;
  56. size_removed += size;
  57. /* The area is smaller than the limit -> remove */
  58. /* Remove centroid */
  59. centroid = Vect_get_area_centroid(Map, area);
  60. if (centroid > 0) {
  61. if (Err) {
  62. Vect_read_line(Map, Points, Cats, centroid);
  63. Vect_write_line(Err, GV_CENTROID, Points, Cats);
  64. }
  65. Vect_delete_line(Map, centroid);
  66. }
  67. /* Find the adjacent area with which the longest boundary is shared */
  68. Vect_get_area_boundaries(Map, area, List);
  69. /* Create a list of neighbour areas */
  70. Vect_reset_list(AList);
  71. for (i = 0; i < List->n_values; i++) {
  72. int line, left, right, neighbour;
  73. line = List->value[i];
  74. if (!Vect_line_alive(Map, abs(line))) /* Should not happen */
  75. G_fatal_error(_("Area is composed of dead boundary"));
  76. Vect_get_line_areas(Map, abs(line), &left, &right);
  77. if (line > 0)
  78. neighbour = left;
  79. else
  80. neighbour = right;
  81. G_debug(4, " line = %d left = %d right = %d neighbour = %d",
  82. line, left, right, neighbour);
  83. Vect_list_append(AList, neighbour); /* this checks for duplicity */
  84. }
  85. G_debug(3, "num neighbours = %d", AList->n_values);
  86. /* Go through the list of neghours and find that with the longest boundary */
  87. dissolve_neighbour = 0;
  88. length = -1.0;
  89. for (i = 0; i < AList->n_values; i++) {
  90. int neighbour1;
  91. double l = 0.0;
  92. neighbour1 = AList->value[i];
  93. G_debug(4, " neighbour1 = %d", neighbour1);
  94. for (j = 0; j < List->n_values; j++) {
  95. int line, left, right, neighbour2;
  96. line = List->value[j];
  97. Vect_get_line_areas(Map, abs(line), &left, &right);
  98. if (line > 0)
  99. neighbour2 = left;
  100. else
  101. neighbour2 = right;
  102. if (neighbour2 == neighbour1) {
  103. Vect_read_line(Map, Points, NULL, abs(line));
  104. l += Vect_line_length(Points);
  105. }
  106. }
  107. if (l > length) {
  108. length = l;
  109. dissolve_neighbour = neighbour1;
  110. }
  111. }
  112. G_debug(3, "dissolve_neighbour = %d", dissolve_neighbour);
  113. /* Make list of boundaries to be removed */
  114. Vect_reset_list(AList);
  115. for (i = 0; i < List->n_values; i++) {
  116. int line, left, right, neighbour;
  117. line = List->value[i];
  118. Vect_get_line_areas(Map, abs(line), &left, &right);
  119. if (line > 0)
  120. neighbour = left;
  121. else
  122. neighbour = right;
  123. G_debug(3, " neighbour = %d", neighbour);
  124. if (neighbour == dissolve_neighbour) {
  125. Vect_list_append(AList, abs(line));
  126. }
  127. }
  128. /* Remove boundaries */
  129. for (i = 0; i < AList->n_values; i++) {
  130. int line;
  131. line = AList->value[i];
  132. if (Err) {
  133. Vect_read_line(Map, Points, Cats, line);
  134. Vect_write_line(Err, GV_BOUNDARY, Points, Cats);
  135. }
  136. Vect_delete_line(Map, line);
  137. }
  138. nremoved++;
  139. if (msgout) {
  140. fprintf(msgout, "\r%s: %5d", _("Removed areas"), nremoved);
  141. fflush(stderr);
  142. }
  143. }
  144. if (msgout)
  145. fprintf(stderr, "\n");
  146. if (removed_area)
  147. *removed_area = size_removed;
  148. return (nremoved);
  149. }