break_polygons.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. /*!
  2. \file lib/vector/Vlib/break_polygons.c
  3. \brief Vector library - clean geometry (break polygons)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Radim Blazek
  9. \author Update for GRASS 7 Markus Metz
  10. */
  11. #include <stdlib.h>
  12. #include <sys/stat.h>
  13. #include <fcntl.h>
  14. #include <unistd.h>
  15. #include <math.h>
  16. #include <grass/vector.h>
  17. #include <grass/glocale.h>
  18. /* TODO: 3D support
  19. *
  20. * atan2() gives angle from x-axis
  21. * this is unambiguous only in 2D, not in 3D
  22. *
  23. * one possibility would be to store unit vectors of length 1
  24. * in struct XPNT
  25. * double a1[3], a2[3];
  26. *
  27. * length = sqrt(dx * dx + dy * dy + dz * dz);
  28. * dx /= length; dy /= length; dz /=length;
  29. * a1[0] = dx; a1[1] = dy; a1[2] = dz;
  30. *
  31. * get second dx, dy, dz
  32. * length = sqrt(dx * dx + dy * dy + dz * dz);
  33. * dx /= length; dy /= length; dz /=length;
  34. * a2[0] = dx; a2[1] = dy; a2[2] = dz;
  35. *
  36. * equal angles
  37. * if (a1[0] == a2[0] && a1[1] == a2[1] && a1[2] == a2[2])
  38. *
  39. * disadvantage: increased memory consumption
  40. *
  41. * new function Vect_break_faces() ?
  42. *
  43. */
  44. typedef struct
  45. {
  46. double x, y; /* coords */
  47. double a1, a2; /* angles */
  48. char cross; /* 0 - do not break, 1 - break */
  49. char used; /* 0 - was not used to break line, 1 - was used to break line
  50. * this is stored because points are automatically marked as cross, even if not used
  51. * later to break lines */
  52. } XPNT;
  53. typedef struct
  54. {
  55. double a1, a2; /* angles */
  56. char cross; /* 0 - do not break, 1 - break */
  57. char used; /* 0 - was not used to break line, 1 - was used to break line
  58. * this is stored because points are automatically marked as cross, even if not used
  59. * later to break lines */
  60. } XPNT2;
  61. static int fpoint;
  62. /* Function called from RTreeSearch for point found */
  63. static int srch(int id, const struct RTree_Rect *rect, void *arg)
  64. {
  65. fpoint = id;
  66. return 0; /* stop searching */
  67. }
  68. /* function used by binary tree to compare items */
  69. static int compare_xpnts(const void *Xpnta, const void *Xpntb)
  70. {
  71. XPNT *a, *b;
  72. a = (XPNT *)Xpnta;
  73. b = (XPNT *)Xpntb;
  74. if (a->x > b->x)
  75. return 1;
  76. else if (a->x < b->x)
  77. return -1;
  78. else {
  79. if (a->y > b->y)
  80. return 1;
  81. else if (a->y < b->y)
  82. return -1;
  83. else
  84. return 0;
  85. }
  86. G_warning(_("Break polygons: Bug in binary tree!"));
  87. return 1;
  88. }
  89. /* break polygons using a file-based search index */
  90. void Vect_break_polygons_file(struct Map_info *Map, int type, struct Map_info *Err)
  91. {
  92. struct line_pnts *BPoints, *Points;
  93. struct line_cats *Cats, *ErrCats;
  94. int i, j, k, ret, ltype, broken, last, nlines;
  95. int nbreaks;
  96. struct RTree *RTree;
  97. int npoints, nallpoints, nmarks;
  98. XPNT2 XPnt;
  99. double dx, dy, a1 = 0, a2 = 0;
  100. int closed, last_point;
  101. char cross;
  102. int fd, xpntfd;
  103. char *filename;
  104. static struct RTree_Rect rect;
  105. static int rect_init = 0;
  106. if (!rect_init) {
  107. rect.boundary = G_malloc(6 * sizeof(RectReal));
  108. rect_init = 6;
  109. }
  110. G_debug(1, "File-based version of Vect_break_polygons()");
  111. filename = G_tempfile();
  112. fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
  113. RTree = RTreeCreateTree(fd, 0, 2);
  114. remove(filename);
  115. filename = G_tempfile();
  116. xpntfd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
  117. remove(filename);
  118. BPoints = Vect_new_line_struct();
  119. Points = Vect_new_line_struct();
  120. Cats = Vect_new_cats_struct();
  121. ErrCats = Vect_new_cats_struct();
  122. nlines = Vect_get_num_lines(Map);
  123. G_debug(3, "nlines = %d", nlines);
  124. /* Go through all lines in vector, and add each point to structure of points,
  125. * if such point already exists check angles of segments and if differ mark for break */
  126. nmarks = 0;
  127. npoints = 1; /* index starts from 1 ! */
  128. nallpoints = 0;
  129. XPnt.used = 0;
  130. G_message(_("Breaking polygons (pass 1: select break points)..."));
  131. for (i = 1; i <= nlines; i++) {
  132. G_percent(i, nlines, 1);
  133. G_debug(3, "i = %d", i);
  134. if (!Vect_line_alive(Map, i))
  135. continue;
  136. ltype = Vect_read_line(Map, Points, Cats, i);
  137. if (!(ltype & type))
  138. continue;
  139. /* This would be confused by duplicate coordinates (angle cannot be calculated) ->
  140. * prune line first */
  141. Vect_line_prune(Points);
  142. /* If first and last point are identical it is close polygon, we don't need to register last point
  143. * and we can calculate angle for first.
  144. * If first and last point are not identical we have to mark for break both */
  145. last_point = Points->n_points - 1;
  146. if (Points->x[0] == Points->x[last_point] &&
  147. Points->y[0] == Points->y[last_point])
  148. closed = 1;
  149. else
  150. closed = 0;
  151. for (j = 0; j < Points->n_points; j++) {
  152. G_debug(3, "j = %d", j);
  153. nallpoints++;
  154. if (j == last_point && closed)
  155. continue; /* do not register last of close polygon */
  156. /* Box */
  157. rect.boundary[0] = Points->x[j];
  158. rect.boundary[3] = Points->x[j];
  159. rect.boundary[1] = Points->y[j];
  160. rect.boundary[4] = Points->y[j];
  161. rect.boundary[2] = 0;
  162. rect.boundary[5] = 0;
  163. /* Already in DB? */
  164. fpoint = -1;
  165. RTreeSearch(RTree, &rect, srch, NULL);
  166. G_debug(3, "fpoint = %d", fpoint);
  167. if (Points->n_points <= 2 ||
  168. (!closed && (j == 0 || j == last_point))) {
  169. cross = 1; /* mark for cross in any case */
  170. }
  171. else { /* calculate angles */
  172. cross = 0;
  173. if (j == 0 && closed) { /* closed polygon */
  174. dx = Points->x[last_point] - Points->x[0];
  175. dy = Points->y[last_point] - Points->y[0];
  176. a1 = atan2(dy, dx);
  177. dx = Points->x[1] - Points->x[0];
  178. dy = Points->y[1] - Points->y[0];
  179. a2 = atan2(dy, dx);
  180. }
  181. else {
  182. dx = Points->x[j - 1] - Points->x[j];
  183. dy = Points->y[j - 1] - Points->y[j];
  184. a1 = atan2(dy, dx);
  185. dx = Points->x[j + 1] - Points->x[j];
  186. dy = Points->y[j + 1] - Points->y[j];
  187. a2 = atan2(dy, dx);
  188. }
  189. }
  190. if (fpoint > 0) { /* Found */
  191. /* read point */
  192. lseek(xpntfd, (off_t) (fpoint - 1) * sizeof(XPNT2), SEEK_SET);
  193. read(xpntfd, &XPnt, sizeof(XPNT2));
  194. if (XPnt.cross == 1)
  195. continue; /* already marked */
  196. /* Check angles */
  197. if (cross) {
  198. XPnt.cross = 1;
  199. nmarks++;
  200. /* write point */
  201. lseek(xpntfd, (off_t) (fpoint - 1) * sizeof(XPNT2), SEEK_SET);
  202. write(xpntfd, &XPnt, sizeof(XPNT2));
  203. }
  204. else {
  205. G_debug(3, "a1 = %f xa1 = %f a2 = %f xa2 = %f", a1,
  206. XPnt.a1, a2, XPnt.a2);
  207. if ((a1 == XPnt.a1 && a2 == XPnt.a2) ||
  208. (a1 == XPnt.a2 && a2 == XPnt.a1)) { /* identical */
  209. }
  210. else {
  211. XPnt.cross = 1;
  212. nmarks++;
  213. /* write point */
  214. lseek(xpntfd, (off_t) (fpoint - 1) * sizeof(XPNT2), SEEK_SET);
  215. write(xpntfd, &XPnt, sizeof(XPNT2));
  216. }
  217. }
  218. }
  219. else {
  220. /* Add to tree and to structure */
  221. RTreeInsertRect(&rect, npoints, RTree);
  222. if (j == 0 || j == (Points->n_points - 1) ||
  223. Points->n_points < 3) {
  224. XPnt.a1 = 0;
  225. XPnt.a2 = 0;
  226. XPnt.cross = 1;
  227. nmarks++;
  228. }
  229. else {
  230. XPnt.a1 = a1;
  231. XPnt.a2 = a2;
  232. XPnt.cross = 0;
  233. }
  234. /* write point */
  235. lseek(xpntfd, (off_t) (npoints - 1) * sizeof(XPNT2), SEEK_SET);
  236. write(xpntfd, &XPnt, sizeof(XPNT2));
  237. npoints++;
  238. }
  239. }
  240. }
  241. nbreaks = 0;
  242. /* Second loop through lines (existing when loop is started, no need to process lines written again)
  243. * and break at points marked for break */
  244. G_message(_("Breaking polygons (pass 2: break at selected points)..."));
  245. for (i = 1; i <= nlines; i++) {
  246. int n_orig_points;
  247. G_percent(i, nlines, 1);
  248. G_debug(3, "i = %d", i);
  249. if (!Vect_line_alive(Map, i))
  250. continue;
  251. ltype = Vect_read_line(Map, Points, Cats, i);
  252. if (!(ltype & type))
  253. continue;
  254. if (!(ltype & GV_LINES))
  255. continue; /* Nonsense to break points */
  256. /* Duplicates would result in zero length lines -> prune line first */
  257. n_orig_points = Points->n_points;
  258. Vect_line_prune(Points);
  259. broken = 0;
  260. last = 0;
  261. G_debug(3, "n_points = %d", Points->n_points);
  262. for (j = 1; j < Points->n_points; j++) {
  263. G_debug(3, "j = %d", j);
  264. nallpoints++;
  265. /* Box */
  266. rect.boundary[0] = Points->x[j];
  267. rect.boundary[3] = Points->x[j];
  268. rect.boundary[1] = Points->y[j];
  269. rect.boundary[4] = Points->y[j];
  270. rect.boundary[2] = 0;
  271. rect.boundary[5] = 0;
  272. if (Points->n_points <= 1 ||
  273. (j == (Points->n_points - 1) && !broken))
  274. break;
  275. /* One point only or
  276. * last point and line is not broken, do nothing */
  277. RTreeSearch(RTree, &rect, srch, NULL);
  278. G_debug(3, "fpoint = %d", fpoint);
  279. /* read point */
  280. lseek(xpntfd, (off_t) (fpoint - 1) * sizeof(XPNT2), SEEK_SET);
  281. read(xpntfd, &XPnt, sizeof(XPNT2));
  282. /* break or write last segment of broken line */
  283. if ((j == (Points->n_points - 1) && broken) ||
  284. XPnt.cross) {
  285. Vect_reset_line(BPoints);
  286. for (k = last; k <= j; k++) {
  287. Vect_append_point(BPoints, Points->x[k], Points->y[k],
  288. Points->z[k]);
  289. }
  290. /* Result may collapse to one point */
  291. Vect_line_prune(BPoints);
  292. if (BPoints->n_points > 1) {
  293. ret = Vect_write_line(Map, ltype, BPoints, Cats);
  294. G_debug(3,
  295. "Line %d written j = %d n_points(orig,pruned) = %d n_points(new) = %d",
  296. ret, j, Points->n_points, BPoints->n_points);
  297. }
  298. if (!broken)
  299. Vect_delete_line(Map, i); /* not yet deleted */
  300. /* Write points on breaks */
  301. if (Err) {
  302. if (XPnt.cross && !XPnt.used) {
  303. Vect_reset_line(BPoints);
  304. Vect_append_point(BPoints, Points->x[j], Points->y[j], 0);
  305. Vect_write_line(Err, GV_POINT, BPoints, ErrCats);
  306. }
  307. if (!XPnt.used) {
  308. XPnt.used = 1;
  309. /* write point */
  310. lseek(xpntfd, (off_t) (fpoint - 1) * sizeof(XPNT2), SEEK_SET);
  311. write(xpntfd, &XPnt, sizeof(XPNT2));
  312. }
  313. }
  314. last = j;
  315. broken = 1;
  316. nbreaks++;
  317. }
  318. }
  319. if (!broken && n_orig_points > Points->n_points) { /* was pruned before -> rewrite */
  320. if (Points->n_points > 1) {
  321. Vect_rewrite_line(Map, i, ltype, Points, Cats);
  322. G_debug(3, "Line %d pruned, npoints = %d", i,
  323. Points->n_points);
  324. }
  325. else {
  326. Vect_delete_line(Map, i);
  327. G_debug(3, "Line %d was deleted", i);
  328. }
  329. }
  330. else {
  331. G_debug(3, "Line %d was not changed", i);
  332. }
  333. }
  334. close(RTree->fd);
  335. RTreeDestroyTree(RTree);
  336. close(xpntfd);
  337. Vect_destroy_line_struct(Points);
  338. Vect_destroy_line_struct(BPoints);
  339. Vect_destroy_cats_struct(Cats);
  340. Vect_destroy_cats_struct(ErrCats);
  341. G_verbose_message(_("Breaks: %d"), nbreaks);
  342. }
  343. /* break polygons using a memory-based search index */
  344. void Vect_break_polygons_mem(struct Map_info *Map, int type, struct Map_info *Err)
  345. {
  346. struct line_pnts *BPoints, *Points;
  347. struct line_cats *Cats, *ErrCats;
  348. int i, j, k, ret, ltype, broken, last, nlines;
  349. int nbreaks;
  350. struct RB_TREE *RBTree;
  351. int npoints, nallpoints, nmarks;
  352. XPNT *XPnt_found, XPnt_search;
  353. double dx, dy, a1 = 0, a2 = 0;
  354. int closed, last_point, cross;
  355. G_debug(1, "Memory-based version of Vect_break_polygons()");
  356. RBTree = rbtree_create(compare_xpnts, sizeof(XPNT));
  357. BPoints = Vect_new_line_struct();
  358. Points = Vect_new_line_struct();
  359. Cats = Vect_new_cats_struct();
  360. ErrCats = Vect_new_cats_struct();
  361. nlines = Vect_get_num_lines(Map);
  362. G_debug(3, "nlines = %d", nlines);
  363. /* Go through all lines in vector, and add each point to structure of points,
  364. * if such point already exists check angles of segments and if differ mark for break */
  365. nmarks = 0;
  366. npoints = 0;
  367. nallpoints = 0;
  368. XPnt_search.used = 0;
  369. G_message(_("Breaking polygons (pass 1: select break points)..."));
  370. for (i = 1; i <= nlines; i++) {
  371. G_percent(i, nlines, 1);
  372. G_debug(3, "i = %d", i);
  373. if (!Vect_line_alive(Map, i))
  374. continue;
  375. ltype = Vect_read_line(Map, Points, Cats, i);
  376. if (!(ltype & type))
  377. continue;
  378. /* This would be confused by duplicate coordinates (angle cannot be calculated) ->
  379. * prune line first */
  380. Vect_line_prune(Points);
  381. /* If first and last point are identical it is close polygon, we don't need to register last point
  382. * and we can calculate angle for first.
  383. * If first and last point are not identical we have to mark for break both */
  384. last_point = Points->n_points - 1;
  385. if (Points->x[0] == Points->x[last_point] &&
  386. Points->y[0] == Points->y[last_point])
  387. closed = 1;
  388. else
  389. closed = 0;
  390. for (j = 0; j < Points->n_points; j++) {
  391. G_debug(3, "j = %d", j);
  392. nallpoints++;
  393. if (j == last_point && closed)
  394. continue; /* do not register last of close polygon */
  395. XPnt_search.x = Points->x[j];
  396. XPnt_search.y = Points->y[j];
  397. /* Already in DB? */
  398. XPnt_found = rbtree_find(RBTree, &XPnt_search);
  399. if (Points->n_points <= 2 ||
  400. (!closed && (j == 0 || j == last_point))) {
  401. cross = 1; /* mark for cross in any case */
  402. }
  403. else { /* calculate angles */
  404. cross = 0;
  405. if (j == 0 && closed) { /* closed polygon */
  406. dx = Points->x[last_point] - Points->x[0];
  407. dy = Points->y[last_point] - Points->y[0];
  408. a1 = atan2(dy, dx);
  409. dx = Points->x[1] - Points->x[0];
  410. dy = Points->y[1] - Points->y[0];
  411. a2 = atan2(dy, dx);
  412. }
  413. else {
  414. dx = Points->x[j - 1] - Points->x[j];
  415. dy = Points->y[j - 1] - Points->y[j];
  416. a1 = atan2(dy, dx);
  417. dx = Points->x[j + 1] - Points->x[j];
  418. dy = Points->y[j + 1] - Points->y[j];
  419. a2 = atan2(dy, dx);
  420. }
  421. }
  422. if (XPnt_found) { /* found */
  423. if (XPnt_found->cross == 1)
  424. continue; /* already marked */
  425. /* check angles */
  426. if (cross) {
  427. XPnt_found->cross = 1;
  428. nmarks++;
  429. }
  430. else {
  431. G_debug(3, "a1 = %f xa1 = %f a2 = %f xa2 = %f", a1,
  432. XPnt_found->a1, a2, XPnt_found->a2);
  433. if ((a1 == XPnt_found->a1 && a2 == XPnt_found->a2) ||
  434. (a1 == XPnt_found->a2 && a2 == XPnt_found->a1)) { /* identical */
  435. }
  436. else {
  437. XPnt_found->cross = 1;
  438. nmarks++;
  439. }
  440. }
  441. }
  442. else {
  443. if (j == 0 || j == (Points->n_points - 1) ||
  444. Points->n_points < 3) {
  445. XPnt_search.a1 = 0;
  446. XPnt_search.a2 = 0;
  447. XPnt_search.cross = 1;
  448. nmarks++;
  449. }
  450. else {
  451. XPnt_search.a1 = a1;
  452. XPnt_search.a2 = a2;
  453. XPnt_search.cross = 0;
  454. }
  455. /* Add to tree */
  456. rbtree_insert(RBTree, &XPnt_search);
  457. npoints++;
  458. }
  459. }
  460. }
  461. nbreaks = 0;
  462. nallpoints = 0;
  463. G_debug(2, "Break polygons: unique vertices: %ld", (long int)RBTree->count);
  464. /* uncomment to check if search tree is healthy */
  465. /* if (rbtree_debug(RBTree, RBTree->root) == 0)
  466. G_warning("Break polygons: RBTree not ok"); */
  467. /* Second loop through lines (existing when loop is started, no need to process lines written again)
  468. * and break at points marked for break */
  469. G_message(_("Breaking polygons (pass 2: break at selected points)..."));
  470. for (i = 1; i <= nlines; i++) {
  471. int n_orig_points;
  472. G_percent(i, nlines, 1);
  473. G_debug(3, "i = %d", i);
  474. if (!Vect_line_alive(Map, i))
  475. continue;
  476. ltype = Vect_read_line(Map, Points, Cats, i);
  477. if (!(ltype & type))
  478. continue;
  479. if (!(ltype & GV_LINES))
  480. continue; /* Nonsense to break points */
  481. /* Duplicates would result in zero length lines -> prune line first */
  482. n_orig_points = Points->n_points;
  483. Vect_line_prune(Points);
  484. broken = 0;
  485. last = 0;
  486. G_debug(3, "n_points = %d", Points->n_points);
  487. for (j = 1; j < Points->n_points; j++) {
  488. G_debug(3, "j = %d", j);
  489. nallpoints++;
  490. if (Points->n_points <= 1 ||
  491. (j == (Points->n_points - 1) && !broken))
  492. break;
  493. /* One point only or
  494. * last point and line is not broken, do nothing */
  495. XPnt_search.x = Points->x[j];
  496. XPnt_search.y = Points->y[j];
  497. XPnt_found = rbtree_find(RBTree, &XPnt_search);
  498. /* all points must be in the search tree, without duplicates */
  499. if (XPnt_found == NULL)
  500. G_fatal_error(_("Point not in search tree!"));
  501. /* break or write last segment of broken line */
  502. if ((j == (Points->n_points - 1) && broken) ||
  503. XPnt_found->cross) {
  504. Vect_reset_line(BPoints);
  505. for (k = last; k <= j; k++) {
  506. Vect_append_point(BPoints, Points->x[k], Points->y[k],
  507. Points->z[k]);
  508. }
  509. /* Result may collapse to one point */
  510. Vect_line_prune(BPoints);
  511. if (BPoints->n_points > 1) {
  512. ret = Vect_write_line(Map, ltype, BPoints, Cats);
  513. G_debug(3,
  514. "Line %d written j = %d n_points(orig,pruned) = %d n_points(new) = %d",
  515. ret, j, Points->n_points, BPoints->n_points);
  516. }
  517. if (!broken)
  518. Vect_delete_line(Map, i); /* not yet deleted */
  519. /* Write points on breaks */
  520. if (Err) {
  521. if (XPnt_found->cross && !XPnt_found->used) {
  522. Vect_reset_line(BPoints);
  523. Vect_append_point(BPoints, Points->x[j], Points->y[j], 0);
  524. Vect_write_line(Err, GV_POINT, BPoints, ErrCats);
  525. }
  526. XPnt_found->used = 1;
  527. }
  528. last = j;
  529. broken = 1;
  530. nbreaks++;
  531. }
  532. }
  533. if (!broken && n_orig_points > Points->n_points) { /* was pruned before -> rewrite */
  534. if (Points->n_points > 1) {
  535. Vect_rewrite_line(Map, i, ltype, Points, Cats);
  536. G_debug(3, "Line %d pruned, npoints = %d", i,
  537. Points->n_points);
  538. }
  539. else {
  540. Vect_delete_line(Map, i);
  541. G_debug(3, "Line %d was deleted", i);
  542. }
  543. }
  544. else {
  545. G_debug(3, "Line %d was not changed", i);
  546. }
  547. }
  548. rbtree_destroy(RBTree);
  549. Vect_destroy_line_struct(Points);
  550. Vect_destroy_line_struct(BPoints);
  551. Vect_destroy_cats_struct(Cats);
  552. G_verbose_message(_("Breaks: %d"), nbreaks);
  553. }
  554. /*!
  555. \brief Break polygons in vector map
  556. Breaks lines specified by type in vector map. Points at
  557. intersections may be optionally written to error map. Input vector
  558. map must be opened on level 2 for update at least on GV_BUILD_BASE.
  559. Function is optimized for closed polygons rings (e.g. imported from
  560. OGR) but with clean geometry - adjacent polygons mostly have
  561. identical boundary. Function creates database of ALL points in the
  562. vector map, and then is looking for those where polygons should be
  563. broken. Lines may be broken only at points existing in input
  564. vector map!
  565. \param Map input map where polygons will be broken
  566. \param type type of line to be broken (GV_LINE or GV_BOUNDARY)
  567. \param Err vector map where points at intersections will be written or NULL
  568. */
  569. void Vect_break_polygons(struct Map_info *Map, int type, struct Map_info *Err)
  570. {
  571. if (getenv("GRASS_VECTOR_LOWMEM"))
  572. Vect_break_polygons_file(Map, type, Err);
  573. else
  574. Vect_break_polygons_mem(Map, type, Err);
  575. }