main.c 24 KB

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