break_polygons.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*!
  2. \file break_polygons.c
  3. \brief Vector library - clean geometry (break polygons)
  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-2008
  12. */
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <grass/gis.h>
  16. #include <grass/Vect.h>
  17. #include <grass/glocale.h>
  18. typedef struct
  19. {
  20. double x, y;
  21. double a1, a2; /* angles */
  22. char cross; /* 0 - do not break, 1 - break */
  23. char used; /* 0 - was not used to break line, 1 - was used to break line
  24. * this is stored because points are automaticaly marked as cross, even if not used
  25. * later to break lines */
  26. } XPNT;
  27. static int fpoint;
  28. /* Function called from RTreeSearch for point found */
  29. void srch(int id, int *arg)
  30. {
  31. fpoint = id;
  32. }
  33. /*!
  34. \brief Break polygons in vector map.
  35. Breaks lines specified by type in vector map. Points at intersections may be optionally
  36. written to error map. Input map must be opened on level 2 for update at least on GV_BUILD_BASE.
  37. Function is optimized for closed polygons rigs (e.g. imported from OGR) but with clean geometry -
  38. adjacent polygons mostly have identical boundary. Function creates database of ALL points
  39. in the map, and then is looking for those where polygons should be broken.
  40. Lines may be broken only at points existing in input map!
  41. \param Map input map where polygons will be broken
  42. \param type type of line to be broken
  43. \param Err vector map where points at intersections will be written or NULL
  44. \return
  45. */
  46. void
  47. Vect_break_polygons(struct Map_info *Map, int type, struct Map_info *Err)
  48. {
  49. struct line_pnts *BPoints, *Points;
  50. struct line_cats *Cats;
  51. int i, j, k, ret, ltype, broken, last, nlines;
  52. int nbreaks;
  53. struct Node *RTree;
  54. int apoints, npoints, nallpoints, nmarks;
  55. XPNT *XPnts;
  56. struct Rect rect;
  57. double dx, dy, a1 = 0, a2 = 0;
  58. int closed, last_point, cross;
  59. RTree = RTreeNewIndex();
  60. BPoints = Vect_new_line_struct();
  61. Points = Vect_new_line_struct();
  62. Cats = Vect_new_cats_struct();
  63. nlines = Vect_get_num_lines(Map);
  64. G_debug(3, "nlines = %d", nlines);
  65. /* Go through all lines in vector, and add each point to structure of points,
  66. * if such point already exists check angles of segments and if differ mark for break */
  67. apoints = 0;
  68. nmarks = 0;
  69. npoints = 1; /* index starts from 1 ! */
  70. nallpoints = 0;
  71. XPnts = NULL;
  72. for (i = 1; i <= nlines; i++) {
  73. G_debug(3, "i = %d", i);
  74. if (!Vect_line_alive(Map, i))
  75. continue;
  76. ltype = Vect_read_line(Map, Points, Cats, i);
  77. if (!(ltype & type))
  78. continue;
  79. /* This would be confused by duplicate coordinates (angle cannot be calculated) ->
  80. * prune line first */
  81. Vect_line_prune(Points);
  82. /* If first and last point are identical it is close polygon, we dont need to register last point
  83. * and we can calculate angle for first.
  84. * If first and last point are not identical we have to mark for break both */
  85. last_point = Points->n_points - 1;
  86. if (Points->x[0] == Points->x[last_point] &&
  87. Points->y[0] == Points->y[last_point])
  88. closed = 1;
  89. else
  90. closed = 0;
  91. for (j = 0; j < Points->n_points; j++) {
  92. G_debug(3, "j = %d", j);
  93. nallpoints++;
  94. if (j == last_point && closed)
  95. continue; /* do not register last of close polygon */
  96. /* Box */
  97. rect.boundary[0] = Points->x[j];
  98. rect.boundary[3] = Points->x[j];
  99. rect.boundary[1] = Points->y[j];
  100. rect.boundary[4] = Points->y[j];
  101. rect.boundary[2] = 0;
  102. rect.boundary[5] = 0;
  103. /* Already in DB? */
  104. fpoint = -1;
  105. RTreeSearch(RTree, &rect, (void *)srch, 0);
  106. G_debug(3, "fpoint = %d", fpoint);
  107. if (Points->n_points <= 2 ||
  108. (!closed && (j == 0 || j == last_point))) {
  109. cross = 1; /* mark for cross in any case */
  110. }
  111. else { /* calculate angles */
  112. cross = 0;
  113. if (j == 0 && closed) { /* closed polygon */
  114. dx = Points->x[last_point] - Points->x[0];
  115. dy = Points->y[last_point] - Points->y[0];
  116. a1 = atan2(dy, dx);
  117. dx = Points->x[1] - Points->x[0];
  118. dy = Points->y[1] - Points->y[0];
  119. a2 = atan2(dy, dx);
  120. }
  121. else {
  122. dx = Points->x[j - 1] - Points->x[j];
  123. dy = Points->y[j - 1] - Points->y[j];
  124. a1 = atan2(dy, dx);
  125. dx = Points->x[j + 1] - Points->x[j];
  126. dy = Points->y[j + 1] - Points->y[j];
  127. a2 = atan2(dy, dx);
  128. }
  129. }
  130. if (fpoint > 0) { /* Found */
  131. if (XPnts[fpoint].cross == 1)
  132. continue; /* already marked */
  133. /* Check angles */
  134. if (cross) {
  135. XPnts[fpoint].cross = 1;
  136. nmarks++;
  137. }
  138. else {
  139. G_debug(3, "a1 = %f xa1 = %f a2 = %f xa2 = %f", a1,
  140. XPnts[fpoint].a1, a2, XPnts[fpoint].a2);
  141. if ((a1 == XPnts[fpoint].a1 && a2 == XPnts[fpoint].a2) || (a1 == XPnts[fpoint].a2 && a2 == XPnts[fpoint].a1)) { /* identical */
  142. }
  143. else {
  144. XPnts[fpoint].cross = 1;
  145. nmarks++;
  146. }
  147. }
  148. }
  149. else {
  150. /* Add to tree and to structure */
  151. RTreeInsertRect(&rect, npoints, &RTree, 0);
  152. if (npoints >= apoints) {
  153. apoints += 10000;
  154. XPnts =
  155. (XPNT *) G_realloc(XPnts,
  156. (apoints + 1) * sizeof(XPNT));
  157. }
  158. XPnts[npoints].x = Points->x[j];
  159. XPnts[npoints].y = Points->y[j];
  160. XPnts[npoints].used = 0;
  161. if (j == 0 || j == (Points->n_points - 1) ||
  162. Points->n_points < 3) {
  163. XPnts[npoints].a1 = 0;
  164. XPnts[npoints].a2 = 0;
  165. XPnts[npoints].cross = 1;
  166. nmarks++;
  167. }
  168. else {
  169. XPnts[npoints].a1 = a1;
  170. XPnts[npoints].a2 = a2;
  171. XPnts[npoints].cross = 0;
  172. }
  173. npoints++;
  174. }
  175. }
  176. }
  177. /* G_sleep (10); */
  178. nbreaks = 0;
  179. /* Second loop through lines (existing when loop is started, no need to process lines written again)
  180. * and break at points marked for break */
  181. for (i = 1; i <= nlines; i++) {
  182. int n_orig_points;
  183. G_debug(3, "i = %d", i);
  184. if (!Vect_line_alive(Map, i))
  185. continue;
  186. ltype = Vect_read_line(Map, Points, Cats, i);
  187. if (!(ltype & type))
  188. continue;
  189. if (!(ltype & GV_LINES))
  190. continue; /* Nonsense to break points */
  191. /* Duplicates would result in zero length lines -> prune line first */
  192. n_orig_points = Points->n_points;
  193. Vect_line_prune(Points);
  194. broken = 0;
  195. last = 0;
  196. G_debug(3, "n_points = %d", Points->n_points);
  197. for (j = 1; j < Points->n_points; j++) {
  198. G_debug(3, "j = %d", j);
  199. nallpoints++;
  200. /* Box */
  201. rect.boundary[0] = Points->x[j];
  202. rect.boundary[3] = Points->x[j];
  203. rect.boundary[1] = Points->y[j];
  204. rect.boundary[4] = Points->y[j];
  205. rect.boundary[2] = 0;
  206. rect.boundary[5] = 0;
  207. if (Points->n_points <= 1 ||
  208. (j == (Points->n_points - 1) && !broken))
  209. break;
  210. /* One point only or
  211. * last point and line is not broken, do nothing */
  212. RTreeSearch(RTree, &rect, (void *)srch, 0);
  213. G_debug(3, "fpoint = %d", fpoint);
  214. if (XPnts[fpoint].cross) { /* realy use to break line */
  215. XPnts[fpoint].used = 1;
  216. }
  217. /* break or write last segment of broken line */
  218. if ((j == (Points->n_points - 1) && broken) ||
  219. XPnts[fpoint].cross) {
  220. Vect_reset_line(BPoints);
  221. for (k = last; k <= j; k++) {
  222. Vect_append_point(BPoints, Points->x[k], Points->y[k],
  223. Points->z[k]);
  224. }
  225. /* Result may collapse to one point */
  226. Vect_line_prune(BPoints);
  227. if (BPoints->n_points > 1) {
  228. ret = Vect_write_line(Map, ltype, BPoints, Cats);
  229. G_debug(3,
  230. "Line %d written j = %d n_points(orig,pruned) = %d n_points(new) = %d",
  231. ret, j, Points->n_points, BPoints->n_points);
  232. }
  233. if (!broken)
  234. Vect_delete_line(Map, i); /* not yet deleted */
  235. last = j;
  236. broken = 1;
  237. nbreaks++;
  238. }
  239. }
  240. if (!broken && n_orig_points > Points->n_points) { /* was pruned before -> rewrite */
  241. if (Points->n_points > 1) {
  242. Vect_rewrite_line(Map, i, ltype, Points, Cats);
  243. G_debug(3, "Line %d pruned, npoints = %d", i,
  244. Points->n_points);
  245. }
  246. else {
  247. Vect_delete_line(Map, i);
  248. G_debug(3, "Line %d was deleted", i);
  249. }
  250. }
  251. else {
  252. G_debug(3, "Line %d was not changed", i);
  253. }
  254. }
  255. /* Write points on breaks */
  256. if (Err) {
  257. Vect_reset_cats(Cats);
  258. for (i = 1; i < npoints; i++) {
  259. if (XPnts[i].used) {
  260. Vect_reset_line(Points);
  261. Vect_append_point(Points, XPnts[i].x, XPnts[i].y, 0);
  262. Vect_write_line(Err, GV_POINT, Points, Cats);
  263. }
  264. }
  265. }
  266. G_free(XPnts);
  267. RTreeDestroyNode(RTree);
  268. }