main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /****************************************************************
  2. *
  3. * MODULE: v.buffer
  4. *
  5. * AUTHOR(S): Radim Blazek
  6. * Upgraded by Rosen Matev (Google Summer of Code 2008)
  7. * OGR support by Martin Landa <landa.martin gmail.com> (2009)
  8. *
  9. * PURPOSE: Vector buffer
  10. *
  11. * COPYRIGHT: (C) 2001-2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General
  14. * Public License (>=v2). Read the file COPYING that
  15. * comes with GRASS for details.
  16. *
  17. **************************************************************/
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <math.h>
  22. #include <grass/gis.h>
  23. #include <grass/vector.h>
  24. #include <grass/dbmi.h>
  25. #include <grass/glocale.h>
  26. #define PI M_PI
  27. #ifndef MIN
  28. #define MIN(X,Y) ((X<Y)?X:Y)
  29. #endif
  30. #ifndef MAX
  31. #define MAX(X,Y) ((X>Y)?X:Y)
  32. #endif
  33. /* returns 1 if unit_tolerance is adjusted, 0 otherwise */
  34. int adjust_tolerance(double *tolerance)
  35. {
  36. double t = 0.999 * (1 - cos(PI / 8));
  37. G_debug(2, "Maximum tolerance = %f", t);
  38. if (*tolerance > t) {
  39. *tolerance = t;
  40. return 1;
  41. }
  42. return 0;
  43. }
  44. int db_CatValArray_get_value_di(dbCatValArray * cvarr, int cat, double *value)
  45. {
  46. int t;
  47. int ctype = cvarr->ctype;
  48. int ret;
  49. if (ctype == DB_C_TYPE_INT) {
  50. ret = db_CatValArray_get_value_int(cvarr, cat, &t);
  51. if (ret != DB_OK)
  52. return ret;
  53. *value = (double)t;
  54. return DB_OK;
  55. }
  56. if (ctype == DB_C_TYPE_DOUBLE) {
  57. ret = db_CatValArray_get_value_double(cvarr, cat, value);
  58. return ret;
  59. }
  60. return DB_FAILED;
  61. }
  62. struct buf_contours
  63. {
  64. int inner_count;
  65. int outer;
  66. int *inner;
  67. };
  68. struct buf_contours_pts
  69. {
  70. int inner_count;
  71. struct line_pnts *oPoints;
  72. struct line_pnts **iPoints;
  73. };
  74. int point_in_buffer(struct buf_contours *arr_bc, struct spatial_index *si,
  75. struct Map_info *Buf, double x, double y)
  76. {
  77. int i, j, ret, flag;
  78. struct bound_box bbox;
  79. static struct ilist *List = NULL;
  80. static struct line_pnts *Points = NULL;
  81. if (List == NULL)
  82. List = Vect_new_list();
  83. if (Points == NULL)
  84. Points = Vect_new_line_struct();
  85. /* select outer contours overlapping with centroid (x, y) */
  86. bbox.W = bbox.E = x;
  87. bbox.N = bbox.S = y;
  88. bbox.T = PORT_DOUBLE_MAX;
  89. bbox.B = -PORT_DOUBLE_MAX;
  90. Vect_spatial_index_select(si, &bbox, List);
  91. for (i = 0; i < List->n_values; i++) {
  92. Vect_read_line(Buf, Points, NULL, arr_bc[List->value[i]].outer);
  93. ret = Vect_point_in_poly(x, y, Points);
  94. if (ret == 0)
  95. continue;
  96. flag = 1;
  97. for (j = 0; j < arr_bc[List->value[i]].inner_count; j++) {
  98. if (arr_bc[List->value[i]].inner[j] < 1)
  99. continue;
  100. Vect_read_line(Buf, Points, NULL, arr_bc[List->value[i]].inner[j]);
  101. ret = Vect_point_in_poly(x, y, Points);
  102. if (ret != 0) { /* inside inner contour */
  103. flag = 0;
  104. break;
  105. }
  106. }
  107. if (flag) {
  108. /* (x,y) is inside outer contour and outside inner contours of arr_bc[i] */
  109. return 1;
  110. }
  111. }
  112. return 0;
  113. }
  114. int get_line_box(const struct line_pnts *Points, struct bound_box *Box)
  115. {
  116. int i;
  117. if (Points->n_points <= 0) {
  118. Box->N = 0;
  119. Box->S = 0;
  120. Box->E = 0;
  121. Box->W = 0;
  122. Box->T = 0;
  123. Box->B = 0;
  124. return 0;
  125. }
  126. Box->E = Points->x[0];
  127. Box->W = Points->x[0];
  128. Box->N = Points->y[0];
  129. Box->S = Points->y[0];
  130. Box->T = Points->z[0];
  131. Box->B = Points->z[0];
  132. for (i = 1; i < Points->n_points; i++) {
  133. if (Points->x[i] > Box->E)
  134. Box->E = Points->x[i];
  135. else if (Points->x[i] < Box->W)
  136. Box->W = Points->x[i];
  137. if (Points->y[i] > Box->N)
  138. Box->N = Points->y[i];
  139. else if (Points->y[i] < Box->S)
  140. Box->S = Points->y[i];
  141. if (Points->z[i] > Box->T)
  142. Box->T = Points->z[i];
  143. else if (Points->z[i] < Box->B)
  144. Box->B = Points->z[i];
  145. }
  146. return 1;
  147. }
  148. int main(int argc, char *argv[])
  149. {
  150. struct Map_info In, Out, Buf;
  151. struct line_pnts *Points;
  152. struct line_cats *Cats, *BCats;
  153. char bufname[GNAME_MAX];
  154. struct GModule *module;
  155. struct Option *in_opt, *out_opt, *type_opt, *dista_opt, *distb_opt,
  156. *angle_opt;
  157. struct Flag *straight_flag, *nocaps_flag;
  158. struct Option *tol_opt, *bufcol_opt, *scale_opt, *field_opt;
  159. int verbose;
  160. double da, db, dalpha, tolerance, unit_tolerance;
  161. int type;
  162. int i, ret, nareas, area, nlines, line;
  163. char *Areas, *Lines;
  164. int field;
  165. struct buf_contours *arr_bc;
  166. struct buf_contours_pts arr_bc_pts;
  167. int buffers_count = 0, line_id;
  168. struct spatial_index si;
  169. struct bound_box bbox;
  170. /* Attributes if sizecol is used */
  171. int nrec, ctype;
  172. struct field_info *Fi;
  173. dbDriver *Driver;
  174. dbCatValArray cvarr;
  175. double size_val, scale;
  176. module = G_define_module();
  177. G_add_keyword(_("vector"));
  178. G_add_keyword(_("geometry"));
  179. G_add_keyword(_("buffer"));
  180. module->description =
  181. _("Creates a buffer around vector features of given type.");
  182. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  183. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  184. field_opt->guisection = _("Selection");
  185. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  186. type_opt->options = "point,line,boundary,centroid,area";
  187. type_opt->answer = "point,line,area";
  188. type_opt->guisection = _("Selection");
  189. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  190. dista_opt = G_define_option();
  191. dista_opt->key = "distance";
  192. dista_opt->type = TYPE_DOUBLE;
  193. dista_opt->required = NO;
  194. dista_opt->description =
  195. _("Buffer distance along major axis in map units");
  196. dista_opt->guisection = _("Distance");
  197. distb_opt = G_define_option();
  198. distb_opt->key = "minordistance";
  199. distb_opt->type = TYPE_DOUBLE;
  200. distb_opt->required = NO;
  201. distb_opt->description =
  202. _("Buffer distance along minor axis in map units");
  203. distb_opt->guisection = _("Distance");
  204. angle_opt = G_define_option();
  205. angle_opt->key = "angle";
  206. angle_opt->type = TYPE_DOUBLE;
  207. angle_opt->required = NO;
  208. angle_opt->answer = "0";
  209. angle_opt->description = _("Angle of major axis in degrees");
  210. angle_opt->guisection = _("Distance");
  211. bufcol_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  212. bufcol_opt->key = "bufcolumn";
  213. bufcol_opt->description =
  214. _("Name of column to use for buffer distances");
  215. bufcol_opt->guisection = _("Distance");
  216. scale_opt = G_define_option();
  217. scale_opt->key = "scale";
  218. scale_opt->type = TYPE_DOUBLE;
  219. scale_opt->required = NO;
  220. scale_opt->answer = "1.0";
  221. scale_opt->description = _("Scaling factor for attribute column values");
  222. scale_opt->guisection = _("Distance");
  223. tol_opt = G_define_option();
  224. tol_opt->key = "tolerance";
  225. tol_opt->type = TYPE_DOUBLE;
  226. tol_opt->required = NO;
  227. tol_opt->answer = "0.01";
  228. tol_opt->description =
  229. _("Maximum distance between theoretical arc and polygon segments as multiple of buffer");
  230. tol_opt->guisection = _("Distance");
  231. straight_flag = G_define_flag();
  232. straight_flag->key = 's';
  233. straight_flag->description = _("Make outside corners straight");
  234. nocaps_flag = G_define_flag();
  235. nocaps_flag->key = 'c';
  236. nocaps_flag->description = _("Don't make caps at the ends of polylines");
  237. G_gisinit(argv[0]);
  238. if (G_parser(argc, argv))
  239. exit(EXIT_FAILURE);
  240. type = Vect_option_to_types(type_opt);
  241. if ((dista_opt->answer && bufcol_opt->answer) ||
  242. (!(dista_opt->answer || bufcol_opt->answer)))
  243. G_fatal_error(_("Select a buffer distance/minordistance/angle "
  244. "or column, but not both."));
  245. if (bufcol_opt->answer)
  246. G_warning(_("The bufcol option may contain bugs during the cleaning "
  247. "step. If you encounter problems, use the debug "
  248. "option or clean manually with v.clean tool=break; "
  249. "v.category step=0; v.extract -d type=area"));
  250. Vect_check_input_output_name(in_opt->answer, out_opt->answer, G_FATAL_EXIT);
  251. Vect_set_open_level(2); /* topology required */
  252. Vect_open_old2(&In, in_opt->answer, "", field_opt->answer);
  253. Vect_set_error_handler_io(&In, &Out);
  254. if (field_opt->answer)
  255. field = Vect_get_field_number(&In, field_opt->answer);
  256. else
  257. field = -1;
  258. if (bufcol_opt->answer && field == -1)
  259. G_fatal_error(_("The bufcol option requires a valid layer."));
  260. tolerance = atof(tol_opt->answer);
  261. if (tolerance <= 0)
  262. G_fatal_error(_("The tolerance must be > 0."));
  263. if (adjust_tolerance(&tolerance))
  264. G_warning(_("The tolerance was reset to %g"), tolerance);
  265. scale = atof(scale_opt->answer);
  266. if (scale <= 0.0)
  267. G_fatal_error("Illegal scale value");
  268. da = db = dalpha = 0;
  269. if (dista_opt->answer) {
  270. da = atof(dista_opt->answer);
  271. if (distb_opt->answer)
  272. db = atof(distb_opt->answer);
  273. else
  274. db = da;
  275. if (angle_opt->answer)
  276. dalpha = atof(angle_opt->answer);
  277. else
  278. dalpha = 0;
  279. unit_tolerance = tolerance * MIN(da, db);
  280. G_verbose_message(_("The tolerance in map units = %g"), unit_tolerance);
  281. }
  282. Vect_open_new(&Out, out_opt->answer, WITHOUT_Z);
  283. Points = Vect_new_line_struct();
  284. Cats = Vect_new_cats_struct();
  285. BCats = Vect_new_cats_struct();
  286. /* open tmp vector for buffers, needed for cleaning */
  287. sprintf(bufname, "%s_tmp_%d", out_opt->answer, getpid());
  288. if (0 > Vect_open_new(&Buf, bufname, 0)) {
  289. Vect_close(&In);
  290. Vect_close(&Out);
  291. Vect_delete(out_opt->answer);
  292. exit(EXIT_FAILURE);
  293. }
  294. Vect_build_partial(&Buf, GV_BUILD_BASE);
  295. /* check and load attribute column data */
  296. if (bufcol_opt->answer) {
  297. db_CatValArray_init(&cvarr);
  298. Fi = Vect_get_field(&In, field);
  299. if (Fi == NULL)
  300. G_fatal_error(_("Database connection not defined for layer %d"),
  301. field);
  302. Driver = db_start_driver_open_database(Fi->driver, Fi->database);
  303. if (Driver == NULL)
  304. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  305. Fi->database, Fi->driver);
  306. /* Note do not check if the column exists in the table because it may be expression */
  307. /* TODO: only select values we need instead of all in column */
  308. nrec =
  309. db_select_CatValArray(Driver, Fi->table, Fi->key,
  310. bufcol_opt->answer, NULL, &cvarr);
  311. if (nrec < 0)
  312. G_fatal_error(_("Unable to select data from table <%s>"),
  313. Fi->table);
  314. G_debug(2, "%d records selected from table", nrec);
  315. ctype = cvarr.ctype;
  316. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
  317. G_fatal_error(_("Column type not supported"));
  318. db_close_database_shutdown_driver(Driver);
  319. /* Output cats/values list */
  320. for (i = 0; i < cvarr.n_values; i++) {
  321. if (ctype == DB_C_TYPE_INT) {
  322. G_debug(4, "cat = %d val = %d", cvarr.value[i].cat,
  323. cvarr.value[i].val.i);
  324. }
  325. else if (ctype == DB_C_TYPE_DOUBLE) {
  326. G_debug(4, "cat = %d val = %f", cvarr.value[i].cat,
  327. cvarr.value[i].val.d);
  328. }
  329. }
  330. }
  331. Vect_copy_head_data(&In, &Out);
  332. Vect_hist_copy(&In, &Out);
  333. Vect_hist_command(&Out);
  334. /* Create buffers' boundaries */
  335. nlines = nareas = 0;
  336. if ((type & GV_POINTS) || (type & GV_LINES))
  337. nlines += Vect_get_num_primitives(&In, type);
  338. if (type & GV_AREA)
  339. nareas = Vect_get_num_areas(&In);
  340. if (nlines + nareas == 0) {
  341. G_warning(_("No features available for buffering. "
  342. "Check type option and features available in the input vector."));
  343. exit(EXIT_SUCCESS);
  344. }
  345. buffers_count = 1;
  346. arr_bc = G_malloc((nlines + nareas + 1) * sizeof(struct buf_contours));
  347. Vect_spatial_index_init(&si, 0);
  348. /* Lines (and Points) */
  349. if ((type & GV_POINTS) || (type & GV_LINES)) {
  350. int ltype;
  351. if (nlines > 0)
  352. G_message(_("Buffering lines..."));
  353. for (line = 1; line <= nlines; line++) {
  354. int cat;
  355. G_debug(2, "line = %d", line);
  356. G_percent(line, nlines, 2);
  357. if (!Vect_line_alive(&In, line))
  358. continue;
  359. ltype = Vect_read_line(&In, Points, Cats, line);
  360. if (!(ltype & type))
  361. continue;
  362. if (field > 0 && !Vect_cat_get(Cats, field, &cat))
  363. continue;
  364. if (bufcol_opt->answer) {
  365. ret = db_CatValArray_get_value_di(&cvarr, cat, &size_val);
  366. if (ret != DB_OK) {
  367. G_warning(_("No record for category %d in table <%s>"),
  368. cat, Fi->table);
  369. continue;
  370. }
  371. if (size_val < 0.0) {
  372. G_warning(_("Attribute is of invalid size (%.3f) for category %d"),
  373. size_val, cat);
  374. continue;
  375. }
  376. if (size_val == 0.0)
  377. continue;
  378. da = size_val * scale;
  379. db = da;
  380. dalpha = 0;
  381. unit_tolerance = tolerance * MIN(da, db);
  382. G_debug(2, " dynamic buffer size = %.2f", da);
  383. G_debug(2, _("The tolerance in map units: %g"),
  384. unit_tolerance);
  385. }
  386. Vect_line_prune(Points);
  387. if (ltype & GV_POINTS || Points->n_points == 1) {
  388. Vect_point_buffer2(Points->x[0], Points->y[0], da, db, dalpha,
  389. !(straight_flag->answer), unit_tolerance,
  390. &(arr_bc_pts.oPoints));
  391. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  392. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, Cats);
  393. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  394. /* add buffer to spatial index */
  395. Vect_get_line_box(&Buf, line_id, &bbox);
  396. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  397. arr_bc[buffers_count].outer = line_id;
  398. arr_bc[buffers_count].inner_count = 0;
  399. arr_bc[buffers_count].inner = NULL;
  400. buffers_count++;
  401. }
  402. else {
  403. Vect_line_buffer2(Points, da, db, dalpha,
  404. !(straight_flag->answer),
  405. !(nocaps_flag->answer), unit_tolerance,
  406. &(arr_bc_pts.oPoints),
  407. &(arr_bc_pts.iPoints),
  408. &(arr_bc_pts.inner_count));
  409. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  410. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, Cats);
  411. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  412. /* add buffer to spatial index */
  413. Vect_get_line_box(&Buf, line_id, &bbox);
  414. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  415. arr_bc[buffers_count].outer = line_id;
  416. arr_bc[buffers_count].inner_count = arr_bc_pts.inner_count;
  417. if (arr_bc_pts.inner_count > 0) {
  418. arr_bc[buffers_count].inner = G_malloc(arr_bc_pts.inner_count * sizeof(int));
  419. for (i = 0; i < arr_bc_pts.inner_count; i++) {
  420. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  421. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.iPoints[i], Cats);
  422. Vect_destroy_line_struct(arr_bc_pts.iPoints[i]);
  423. /* add buffer to spatial index */
  424. Vect_get_line_box(&Buf, line_id, &bbox);
  425. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  426. arr_bc[buffers_count].inner[i] = line_id;
  427. }
  428. G_free(arr_bc_pts.iPoints);
  429. }
  430. buffers_count++;
  431. }
  432. }
  433. }
  434. /* Areas */
  435. if (type & GV_AREA) {
  436. int centroid;
  437. if (nareas > 0)
  438. G_message(_("Buffering areas..."));
  439. for (area = 1; area <= nareas; area++) {
  440. int cat;
  441. G_percent(area, nareas, 2);
  442. if (!Vect_area_alive(&In, area))
  443. continue;
  444. centroid = Vect_get_area_centroid(&In, area);
  445. if (centroid == 0)
  446. continue;
  447. Vect_read_line(&In, NULL, Cats, centroid);
  448. if (field > 0 && !Vect_cat_get(Cats, field, &cat))
  449. continue;
  450. if (bufcol_opt->answer) {
  451. ret = db_CatValArray_get_value_di(&cvarr, cat, &size_val);
  452. if (ret != DB_OK) {
  453. G_warning(_("No record for category %d in table <%s>"),
  454. cat, Fi->table);
  455. continue;
  456. }
  457. if (size_val < 0.0) {
  458. G_warning(_("Attribute is of invalid size (%.3f) for category %d"),
  459. size_val, cat);
  460. continue;
  461. }
  462. if (size_val == 0.0)
  463. continue;
  464. da = size_val * scale;
  465. db = da;
  466. dalpha = 0;
  467. unit_tolerance = tolerance * MIN(da, db);
  468. G_debug(2, " dynamic buffer size = %.2f", da);
  469. G_debug(2, _("The tolerance in map units: %g"),
  470. unit_tolerance);
  471. }
  472. Vect_area_buffer2(&In, area, da, db, dalpha,
  473. !(straight_flag->answer),
  474. !(nocaps_flag->answer), unit_tolerance,
  475. &(arr_bc_pts.oPoints),
  476. &(arr_bc_pts.iPoints),
  477. &(arr_bc_pts.inner_count));
  478. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  479. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, Cats);
  480. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  481. /* add buffer to spatial index */
  482. Vect_get_line_box(&Buf, line_id, &bbox);
  483. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  484. arr_bc[buffers_count].outer = line_id;
  485. arr_bc[buffers_count].inner_count = arr_bc_pts.inner_count;
  486. if (arr_bc_pts.inner_count > 0) {
  487. arr_bc[buffers_count].inner = G_malloc(arr_bc_pts.inner_count * sizeof(int));
  488. for (i = 0; i < arr_bc_pts.inner_count; i++) {
  489. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  490. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.iPoints[i], Cats);
  491. Vect_destroy_line_struct(arr_bc_pts.iPoints[i]);
  492. /* add buffer to spatial index */
  493. Vect_get_line_box(&Buf, line_id, &bbox);
  494. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  495. arr_bc[buffers_count].inner[i] = line_id;
  496. }
  497. G_free(arr_bc_pts.iPoints);
  498. }
  499. buffers_count++;
  500. }
  501. }
  502. verbose = G_verbose();
  503. G_message(_("Cleaning buffers..."));
  504. /* Break lines */
  505. G_message(_("Building parts of topology..."));
  506. Vect_build_partial(&Out, GV_BUILD_BASE);
  507. G_message(_("Snapping boundaries..."));
  508. Vect_snap_lines(&Out, GV_BOUNDARY, 1e-7, NULL);
  509. G_message(_("Breaking polygons..."));
  510. Vect_break_polygons(&Out, GV_BOUNDARY, NULL);
  511. G_message(_("Removing duplicates..."));
  512. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  513. do {
  514. G_message(_("Breaking boundaries..."));
  515. Vect_break_lines(&Out, GV_BOUNDARY, NULL);
  516. G_message(_("Removing duplicates..."));
  517. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  518. G_message(_("Cleaning boundaries at nodes"));
  519. } while (Vect_clean_small_angles_at_nodes(&Out, GV_BOUNDARY, NULL) > 0);
  520. /* Dangles and bridges don't seem to be necessary if snapping is small enough. */
  521. /* Still needed for larger buffer distances ? */
  522. /*
  523. G_message(_("Removing dangles..."));
  524. Vect_remove_dangles(&Out, GV_BOUNDARY, -1, NULL);
  525. G_message (_("Removing bridges..."));
  526. Vect_remove_bridges(&Out, NULL);
  527. */
  528. G_message(_("Attaching islands..."));
  529. Vect_build_partial(&Out, GV_BUILD_ATTACH_ISLES);
  530. /* Calculate new centroids for all areas */
  531. nareas = Vect_get_num_areas(&Out);
  532. Areas = (char *)G_calloc(nareas + 1, sizeof(char));
  533. G_message(_("Calculating centroids for areas..."));
  534. G_percent(0, nareas, 2);
  535. for (area = 1; area <= nareas; area++) {
  536. double x, y;
  537. G_percent(area, nareas, 2);
  538. G_debug(3, "area = %d", area);
  539. if (!Vect_area_alive(&Out, area))
  540. continue;
  541. ret = Vect_get_point_in_area(&Out, area, &x, &y);
  542. if (ret < 0) {
  543. G_warning(_("Cannot calculate area centroid"));
  544. continue;
  545. }
  546. ret = point_in_buffer(arr_bc, &si, &Buf, x, y);
  547. if (ret) {
  548. G_debug(3, " -> in buffer");
  549. Areas[area] = 1;
  550. }
  551. }
  552. /* Make a list of boundaries to be deleted (both sides inside) */
  553. nlines = Vect_get_num_lines(&Out);
  554. G_debug(3, "nlines = %d", nlines);
  555. Lines = (char *)G_calloc(nlines + 1, sizeof(char));
  556. G_message(_("Generating list of boundaries to be deleted..."));
  557. for (line = 1; line <= nlines; line++) {
  558. int j, side[2], areas[2];
  559. G_percent(line, nlines, 2);
  560. G_debug(3, "line = %d", line);
  561. if (!Vect_line_alive(&Out, line))
  562. continue;
  563. Vect_get_line_areas(&Out, line, &side[0], &side[1]);
  564. for (j = 0; j < 2; j++) {
  565. if (side[j] == 0) { /* area/isle not build */
  566. areas[j] = 0;
  567. }
  568. else if (side[j] > 0) { /* area */
  569. areas[j] = side[j];
  570. }
  571. else { /* < 0 -> island */
  572. areas[j] = Vect_get_isle_area(&Out, abs(side[j]));
  573. }
  574. }
  575. G_debug(3, " areas = %d , %d -> Areas = %d, %d", areas[0], areas[1],
  576. Areas[areas[0]], Areas[areas[1]]);
  577. if (Areas[areas[0]] && Areas[areas[1]])
  578. Lines[line] = 1;
  579. }
  580. G_free(Areas);
  581. /* Delete boundaries */
  582. G_message(_("Deleting boundaries..."));
  583. for (line = 1; line <= nlines; line++) {
  584. G_percent(line, nlines, 2);
  585. if (!Vect_line_alive(&Out, line))
  586. continue;
  587. if (Lines[line]) {
  588. G_debug(3, " delete line %d", line);
  589. Vect_delete_line(&Out, line);
  590. }
  591. else {
  592. /* delete incorrect boundaries */
  593. int side[2];
  594. Vect_get_line_areas(&Out, line, &side[0], &side[1]);
  595. if (!side[0] && !side[1])
  596. Vect_delete_line(&Out, line);
  597. }
  598. }
  599. G_free(Lines);
  600. /* Create new centroids */
  601. Vect_reset_cats(Cats);
  602. Vect_cat_set(Cats, 1, 1);
  603. nareas = Vect_get_num_areas(&Out);
  604. G_message(_("Calculating centroids for areas..."));
  605. for (area = 1; area <= nareas; area++) {
  606. double x, y;
  607. G_percent(area, nareas, 2);
  608. G_debug(3, "area = %d", area);
  609. if (!Vect_area_alive(&Out, area))
  610. continue;
  611. ret = Vect_get_point_in_area(&Out, area, &x, &y);
  612. if (ret < 0) {
  613. G_warning(_("Cannot calculate area centroid"));
  614. continue;
  615. }
  616. ret = point_in_buffer(arr_bc, &si, &Buf, x, y);
  617. if (ret) {
  618. Vect_reset_line(Points);
  619. Vect_append_point(Points, x, y, 0.);
  620. Vect_write_line(&Out, GV_CENTROID, Points, Cats);
  621. }
  622. }
  623. /* free arr_bc[] */
  624. /* will only slow down the module
  625. for (i = 0; i < buffers_count; i++) {
  626. Vect_destroy_line_struct(arr_bc[i].oPoints);
  627. for (j = 0; j < arr_bc[i].inner_count; j++)
  628. Vect_destroy_line_struct(arr_bc[i].iPoints[j]);
  629. G_free(arr_bc[i].iPoints);
  630. } */
  631. Vect_spatial_index_destroy(&si);
  632. Vect_close(&Buf);
  633. Vect_delete(bufname);
  634. G_set_verbose(verbose);
  635. Vect_close(&In);
  636. Vect_build_partial(&Out, GV_BUILD_NONE);
  637. Vect_build(&Out);
  638. Vect_close(&Out);
  639. exit(EXIT_SUCCESS);
  640. }