main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  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. static int cmp_int(const void *a, const void *b)
  155. {
  156. int ai = *(int *)a;
  157. int bi = *(int *)b;
  158. return (ai < bi ? -1 : (ai > bi));
  159. }
  160. /* parse filter options */
  161. /* return cat list or NULL */
  162. struct cat_list *parse_filter_options(struct Map_info *Map, int layer,
  163. char *where, char *catstr)
  164. {
  165. struct cat_list *list = NULL;
  166. if (where) {
  167. struct field_info *Fi = NULL;
  168. dbDriver *driver = NULL;
  169. int ncats, *cats = NULL;
  170. int i, j;
  171. if (layer < 1)
  172. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", "where");
  173. if (catstr)
  174. G_warning(_("'where' and 'cats' parameters were supplied, cat will be ignored"));
  175. Fi = Vect_get_field(Map, layer);
  176. if (!Fi) {
  177. G_fatal_error(_("Database connection not defined for layer %d"),
  178. layer);
  179. }
  180. G_verbose_message(_("Loading categories from table <%s>..."), Fi->table);
  181. driver = db_start_driver_open_database(Fi->driver, Fi->database);
  182. if (driver == NULL)
  183. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  184. Fi->database, Fi->driver);
  185. ncats = db_select_int(driver, Fi->table, Fi->key, where,
  186. &cats);
  187. if (ncats == -1)
  188. G_fatal_error(_("Unable select records from table <%s>"),
  189. Fi->table);
  190. G_verbose_message(_("%d categories loaded"), ncats);
  191. db_close_database_shutdown_driver(driver);
  192. /* sort */
  193. qsort(cats, ncats, sizeof(int), cmp_int);
  194. /* remove duplicates */
  195. j = 1;
  196. for (i = 1; i < ncats; i++) {
  197. if (cats[i] != cats[j - 1]) {
  198. cats[j] = cats[i];
  199. j++;
  200. }
  201. }
  202. ncats = j;
  203. /* convert to cat list */
  204. list = Vect_new_cat_list();
  205. Vect_array_to_cat_list(cats, ncats, list);
  206. if (cats)
  207. G_free(cats);
  208. }
  209. else if (catstr) {
  210. if (layer < 1)
  211. G_fatal_error(_("'%s' must be > 0 for '%s'"), "layer", GV_KEY_COLUMN);
  212. list = Vect_new_cat_list();
  213. if (Vect_str_to_cat_list(catstr, list) > 0) {
  214. G_warning(_("Problem loading category values"));
  215. }
  216. }
  217. if (list) {
  218. if (list->n_ranges < 1) {
  219. Vect_destroy_cat_list(list);
  220. list = NULL;
  221. }
  222. }
  223. return list;
  224. }
  225. int main(int argc, char *argv[])
  226. {
  227. struct Map_info In, Out, Buf;
  228. struct line_pnts *Points;
  229. struct line_cats *Cats, *BCats, *CCats;
  230. char bufname[GNAME_MAX];
  231. struct GModule *module;
  232. struct Option *in_opt, *out_opt, *type_opt, *dista_opt, *distb_opt,
  233. *angle_opt;
  234. struct Flag *straight_flag, *nocaps_flag, *cats_flag;
  235. struct Option *tol_opt, *bufcol_opt, *scale_opt, *field_opt,
  236. *where_opt, *cats_opt;
  237. struct cat_list *cat_list = NULL;
  238. int verbose;
  239. double da, db, dalpha, tolerance, unit_tolerance;
  240. int type;
  241. int i, ret, nareas, area, nlines, line;
  242. char *Areas, *Lines;
  243. int field;
  244. struct buf_contours *arr_bc;
  245. int arr_bc_alloc;
  246. struct buf_contours_pts arr_bc_pts;
  247. int line_id, buffers_count = 0;
  248. struct spatial_index si;
  249. struct bound_box bbox;
  250. /* Attributes if sizecol is used */
  251. int nrec, ctype;
  252. struct field_info *Fi = NULL;
  253. dbDriver *Driver;
  254. dbCatValArray cvarr;
  255. double size_val, scale;
  256. #ifdef HAVE_GEOS
  257. /* TODO: use GEOSBufferParams * */
  258. #endif
  259. module = G_define_module();
  260. G_add_keyword(_("vector"));
  261. G_add_keyword(_("buffer"));
  262. G_add_keyword(_("geometry"));
  263. module->description =
  264. _("Creates a buffer around vector features of given type.");
  265. in_opt = G_define_standard_option(G_OPT_V_INPUT);
  266. field_opt = G_define_standard_option(G_OPT_V_FIELD_ALL);
  267. field_opt->guisection = _("Selection");
  268. cats_opt = G_define_standard_option(G_OPT_V_CATS);
  269. cats_opt->guisection = _("Selection");
  270. where_opt = G_define_standard_option(G_OPT_DB_WHERE);
  271. where_opt->guisection = _("Selection");
  272. type_opt = G_define_standard_option(G_OPT_V_TYPE);
  273. type_opt->options = "point,line,boundary,centroid,area";
  274. type_opt->answer = "point,line,area";
  275. type_opt->guisection = _("Selection");
  276. out_opt = G_define_standard_option(G_OPT_V_OUTPUT);
  277. dista_opt = G_define_option();
  278. dista_opt->key = "distance";
  279. dista_opt->type = TYPE_DOUBLE;
  280. dista_opt->required = NO;
  281. dista_opt->description =
  282. _("Buffer distance along major axis in map units");
  283. dista_opt->guisection = _("Distance");
  284. distb_opt = G_define_option();
  285. distb_opt->key = "minordistance";
  286. distb_opt->type = TYPE_DOUBLE;
  287. distb_opt->required = NO;
  288. distb_opt->description =
  289. _("Buffer distance along minor axis in map units");
  290. distb_opt->guisection = _("Distance");
  291. angle_opt = G_define_option();
  292. angle_opt->key = "angle";
  293. angle_opt->type = TYPE_DOUBLE;
  294. angle_opt->required = NO;
  295. angle_opt->answer = "0";
  296. angle_opt->description = _("Angle of major axis in degrees");
  297. angle_opt->guisection = _("Distance");
  298. bufcol_opt = G_define_standard_option(G_OPT_DB_COLUMN);
  299. bufcol_opt->key = "bufcolumn";
  300. bufcol_opt->description =
  301. _("Name of column to use for buffer distances");
  302. bufcol_opt->guisection = _("Distance");
  303. scale_opt = G_define_option();
  304. scale_opt->key = "scale";
  305. scale_opt->type = TYPE_DOUBLE;
  306. scale_opt->required = NO;
  307. scale_opt->answer = "1.0";
  308. scale_opt->description = _("Scaling factor for attribute column values");
  309. scale_opt->guisection = _("Distance");
  310. tol_opt = G_define_option();
  311. tol_opt->key = "tolerance";
  312. tol_opt->type = TYPE_DOUBLE;
  313. tol_opt->required = NO;
  314. tol_opt->answer = "0.01";
  315. tol_opt->description =
  316. _("Maximum distance between theoretical arc and polygon segments as multiple of buffer");
  317. tol_opt->guisection = _("Distance");
  318. straight_flag = G_define_flag();
  319. straight_flag->key = 's';
  320. straight_flag->description = _("Make outside corners straight");
  321. nocaps_flag = G_define_flag();
  322. nocaps_flag->key = 'c';
  323. nocaps_flag->description = _("Don't make caps at the ends of polylines");
  324. cats_flag = G_define_flag();
  325. cats_flag->key = 't';
  326. cats_flag->description = _("Transfer categories and attributes");
  327. G_gisinit(argv[0]);
  328. if (G_parser(argc, argv))
  329. exit(EXIT_FAILURE);
  330. type = Vect_option_to_types(type_opt);
  331. if ((dista_opt->answer && bufcol_opt->answer) ||
  332. (!(dista_opt->answer || bufcol_opt->answer)))
  333. G_fatal_error(_("Select a buffer distance/minordistance/angle "
  334. "or column, but not both."));
  335. Vect_check_input_output_name(in_opt->answer, out_opt->answer, G_FATAL_EXIT);
  336. Vect_set_open_level(2); /* topology required */
  337. Vect_open_old2(&In, in_opt->answer, "", field_opt->answer);
  338. Vect_set_error_handler_io(&In, &Out);
  339. if (field_opt->answer)
  340. field = Vect_get_field_number(&In, field_opt->answer);
  341. else
  342. field = -1;
  343. cat_list = parse_filter_options(&In, field, where_opt->answer,
  344. cats_opt->answer);
  345. if (bufcol_opt->answer && field == -1)
  346. G_fatal_error(_("The bufcol option requires a valid layer."));
  347. tolerance = atof(tol_opt->answer);
  348. if (tolerance <= 0)
  349. G_fatal_error(_("The tolerance must be > 0."));
  350. if (adjust_tolerance(&tolerance))
  351. G_warning(_("The tolerance was reset to %g"), tolerance);
  352. scale = atof(scale_opt->answer);
  353. if (scale <= 0.0)
  354. G_fatal_error("Illegal scale value");
  355. da = db = dalpha = unit_tolerance = 0;
  356. if (dista_opt->answer) {
  357. da = atof(dista_opt->answer);
  358. if (distb_opt->answer)
  359. db = atof(distb_opt->answer);
  360. else
  361. db = da;
  362. if (angle_opt->answer)
  363. dalpha = atof(angle_opt->answer);
  364. else
  365. dalpha = 0;
  366. unit_tolerance = fabs(tolerance * MIN(da, db));
  367. G_verbose_message(_("The tolerance in map units = %g"), unit_tolerance);
  368. }
  369. Vect_open_new(&Out, out_opt->answer, WITHOUT_Z);
  370. Points = Vect_new_line_struct();
  371. Cats = Vect_new_cats_struct();
  372. BCats = Vect_new_cats_struct();
  373. CCats = Vect_new_cats_struct();
  374. /* open tmp vector for buffers, needed for cleaning */
  375. sprintf(bufname, "%s_tmp_%d", out_opt->answer, getpid());
  376. if (0 > Vect_open_new(&Buf, bufname, 0)) {
  377. Vect_close(&In);
  378. Vect_close(&Out);
  379. Vect_delete(out_opt->answer);
  380. exit(EXIT_FAILURE);
  381. }
  382. Vect_build_partial(&Buf, GV_BUILD_BASE);
  383. /* check and load attribute column data */
  384. if (bufcol_opt->answer) {
  385. db_CatValArray_init(&cvarr);
  386. Fi = Vect_get_field(&In, field);
  387. if (Fi == NULL)
  388. G_fatal_error(_("Database connection not defined for layer %d"),
  389. field);
  390. Driver = db_start_driver_open_database(Fi->driver, Fi->database);
  391. if (Driver == NULL)
  392. G_fatal_error(_("Unable to open database <%s> by driver <%s>"),
  393. Fi->database, Fi->driver);
  394. /* Note do not check if the column exists in the table because it may be expression */
  395. /* TODO: only select values we need instead of all in column */
  396. nrec =
  397. db_select_CatValArray(Driver, Fi->table, Fi->key,
  398. bufcol_opt->answer, NULL, &cvarr);
  399. if (nrec < 0)
  400. G_fatal_error(_("Unable to select data from table <%s>"),
  401. Fi->table);
  402. G_debug(2, "%d records selected from table", nrec);
  403. ctype = cvarr.ctype;
  404. if (ctype != DB_C_TYPE_INT && ctype != DB_C_TYPE_DOUBLE)
  405. G_fatal_error(_("Column type not supported"));
  406. db_close_database_shutdown_driver(Driver);
  407. /* Output cats/values list */
  408. for (i = 0; i < cvarr.n_values; i++) {
  409. if (ctype == DB_C_TYPE_INT) {
  410. G_debug(4, "cat = %d val = %d", cvarr.value[i].cat,
  411. cvarr.value[i].val.i);
  412. }
  413. else if (ctype == DB_C_TYPE_DOUBLE) {
  414. G_debug(4, "cat = %d val = %f", cvarr.value[i].cat,
  415. cvarr.value[i].val.d);
  416. }
  417. }
  418. }
  419. Vect_copy_head_data(&In, &Out);
  420. Vect_hist_copy(&In, &Out);
  421. Vect_hist_command(&Out);
  422. /* Create buffers' boundaries */
  423. nlines = nareas = 0;
  424. if ((type & GV_POINTS) || (type & GV_LINES))
  425. nlines = Vect_get_num_primitives(&In, type);
  426. if (type & GV_AREA)
  427. nareas = Vect_get_num_areas(&In);
  428. if (nlines + nareas == 0) {
  429. G_warning(_("No features available for buffering. "
  430. "Check type option and features available in the input vector."));
  431. exit(EXIT_SUCCESS);
  432. }
  433. /* init arr_bc */
  434. buffers_count = 1;
  435. arr_bc_alloc = nlines + nareas + 1;
  436. arr_bc = G_calloc(arr_bc_alloc, sizeof(struct buf_contours));
  437. Vect_spatial_index_init(&si, 0);
  438. #ifdef HAVE_GEOS
  439. initGEOS(G_message, G_fatal_error);
  440. #else
  441. if (da < 0. || db < 0.) {
  442. G_warning(_("Negative distances for internal buffers are not supported "
  443. "and converted to positive values."));
  444. da = fabs(da);
  445. db = fabs(db);
  446. }
  447. #endif
  448. /* Areas */
  449. if (nareas > 0) {
  450. int centroid;
  451. G_message(_("Buffering areas..."));
  452. for (area = 1; area <= nareas; area++) {
  453. int cat;
  454. G_percent(area, nareas, 2);
  455. if (!Vect_area_alive(&In, area))
  456. continue;
  457. centroid = Vect_get_area_centroid(&In, area);
  458. if (centroid == 0)
  459. continue;
  460. Vect_read_line(&In, NULL, Cats, centroid);
  461. if (cat_list) {
  462. int found = 0;
  463. for (i = 0; i < Cats->n_cats; i++) {
  464. if (Cats->field[i] == field &&
  465. Vect_cat_in_cat_list(Cats->cat[i], cat_list)) {
  466. found = 1;
  467. break;
  468. }
  469. }
  470. if (!found)
  471. continue;
  472. }
  473. else if (field > 0 && !Vect_cat_get(Cats, field, &cat))
  474. continue;
  475. Vect_reset_cats(CCats);
  476. for (i = 0; i < Cats->n_cats; i++) {
  477. if (field < 0 || Cats->field[i] == field) {
  478. Vect_cat_set(CCats, Cats->field[i], Cats->cat[i]);
  479. }
  480. }
  481. if (bufcol_opt->answer) {
  482. ret = db_CatValArray_get_value_di(&cvarr, cat, &size_val);
  483. if (ret != DB_OK) {
  484. G_warning(_("No record for category %d in table <%s>"),
  485. cat, Fi->table);
  486. continue;
  487. }
  488. if (size_val < 0.0) {
  489. G_warning(_("Attribute is of invalid size (%.3f) for category %d"),
  490. size_val, cat);
  491. continue;
  492. }
  493. if (size_val == 0.0)
  494. continue;
  495. da = size_val * scale;
  496. db = da;
  497. dalpha = 0;
  498. unit_tolerance = fabs(tolerance * MIN(da, db));
  499. G_debug(2, " dynamic buffer size = %.2f", da);
  500. G_debug(2, _("The tolerance in map units: %g"),
  501. unit_tolerance);
  502. }
  503. #ifdef HAVE_GEOS
  504. geos_buffer(&In, &Out, &Buf, area, GV_AREA, da,
  505. &si, CCats, &arr_bc, &buffers_count, &arr_bc_alloc);
  506. #else
  507. if (da < 0. || db < 0.) {
  508. G_warning(_("Negative distances for internal buffers are not supported "
  509. "and converted to positive values."));
  510. da = fabs(da);
  511. db = fabs(db);
  512. }
  513. Vect_area_buffer2(&In, area, da, db, dalpha,
  514. !(straight_flag->answer),
  515. !(nocaps_flag->answer), unit_tolerance,
  516. &(arr_bc_pts.oPoints),
  517. &(arr_bc_pts.iPoints),
  518. &(arr_bc_pts.inner_count));
  519. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  520. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, CCats);
  521. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  522. /* add buffer to spatial index */
  523. Vect_get_line_box(&Buf, line_id, &bbox);
  524. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  525. arr_bc[buffers_count].outer = line_id;
  526. arr_bc[buffers_count].inner_count = arr_bc_pts.inner_count;
  527. if (arr_bc_pts.inner_count > 0) {
  528. arr_bc[buffers_count].inner = G_malloc(arr_bc_pts.inner_count * sizeof(int));
  529. for (i = 0; i < arr_bc_pts.inner_count; i++) {
  530. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  531. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  532. Vect_destroy_line_struct(arr_bc_pts.iPoints[i]);
  533. arr_bc[buffers_count].inner[i] = line_id;
  534. }
  535. G_free(arr_bc_pts.iPoints);
  536. }
  537. buffers_count++;
  538. #endif
  539. }
  540. }
  541. /* Lines (and Points) */
  542. if (nlines > 0) {
  543. int ltype;
  544. G_message(_("Buffering lines..."));
  545. if (da < 0 || db < 0) {
  546. G_warning(_("Negative distances are only supported for areas"));
  547. da = fabs(da);
  548. db = fabs(db);
  549. }
  550. nlines = Vect_get_num_lines(&In);
  551. for (line = 1; line <= nlines; line++) {
  552. int cat;
  553. G_debug(2, "line = %d", line);
  554. G_percent(line, nlines, 2);
  555. if (!Vect_line_alive(&In, line))
  556. continue;
  557. ltype = Vect_read_line(&In, Points, Cats, line);
  558. if (!(ltype & type))
  559. continue;
  560. if (cat_list) {
  561. int found = 0;
  562. for (i = 0; i < Cats->n_cats; i++) {
  563. if (Cats->field[i] == field &&
  564. Vect_cat_in_cat_list(Cats->cat[i], cat_list)) {
  565. found = 1;
  566. break;
  567. }
  568. }
  569. if (!found)
  570. continue;
  571. }
  572. else if (field > 0 && !Vect_cat_get(Cats, field, &cat))
  573. continue;
  574. Vect_reset_cats(CCats);
  575. for (i = 0; i < Cats->n_cats; i++) {
  576. if (field < 0 || Cats->field[i] == field) {
  577. Vect_cat_set(CCats, Cats->field[i], Cats->cat[i]);
  578. }
  579. }
  580. if (bufcol_opt->answer) {
  581. ret = db_CatValArray_get_value_di(&cvarr, cat, &size_val);
  582. if (ret != DB_OK) {
  583. G_warning(_("No record for category %d in table <%s>"),
  584. cat, Fi->table);
  585. continue;
  586. }
  587. if (size_val < 0.0) {
  588. G_warning(_("Attribute is of invalid size (%.3f) for category %d"),
  589. size_val, cat);
  590. continue;
  591. }
  592. if (size_val == 0.0)
  593. continue;
  594. da = size_val * scale;
  595. if (da < 0) {
  596. G_warning(_("Negative distances are only supported for areas"));
  597. da = fabs(da);
  598. }
  599. db = da;
  600. dalpha = 0;
  601. unit_tolerance = tolerance * MIN(da, db);
  602. G_debug(2, " dynamic buffer size = %.2f", da);
  603. G_debug(2, _("The tolerance in map units: %g"),
  604. unit_tolerance);
  605. }
  606. Vect_line_prune(Points);
  607. if (ltype & GV_POINTS || Points->n_points == 1) {
  608. Vect_point_buffer2(Points->x[0], Points->y[0], da, db, dalpha,
  609. !(straight_flag->answer), unit_tolerance,
  610. &(arr_bc_pts.oPoints));
  611. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  612. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, CCats);
  613. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  614. /* add buffer to spatial index */
  615. Vect_get_line_box(&Buf, line_id, &bbox);
  616. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  617. arr_bc[buffers_count].outer = line_id;
  618. arr_bc[buffers_count].inner_count = 0;
  619. arr_bc[buffers_count].inner = NULL;
  620. buffers_count++;
  621. }
  622. else {
  623. #ifdef HAVE_GEOS
  624. geos_buffer(&In, &Out, &Buf, line, type, da,
  625. &si, CCats, &arr_bc, &buffers_count, &arr_bc_alloc);
  626. #else
  627. Vect_line_buffer2(Points, da, db, dalpha,
  628. !(straight_flag->answer),
  629. !(nocaps_flag->answer), unit_tolerance,
  630. &(arr_bc_pts.oPoints),
  631. &(arr_bc_pts.iPoints),
  632. &(arr_bc_pts.inner_count));
  633. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.oPoints, BCats);
  634. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.oPoints, CCats);
  635. Vect_destroy_line_struct(arr_bc_pts.oPoints);
  636. /* add buffer to spatial index */
  637. Vect_get_line_box(&Buf, line_id, &bbox);
  638. Vect_spatial_index_add_item(&si, buffers_count, &bbox);
  639. arr_bc[buffers_count].outer = line_id;
  640. arr_bc[buffers_count].inner_count = arr_bc_pts.inner_count;
  641. if (arr_bc_pts.inner_count > 0) {
  642. arr_bc[buffers_count].inner = G_malloc(arr_bc_pts.inner_count * sizeof(int));
  643. for (i = 0; i < arr_bc_pts.inner_count; i++) {
  644. Vect_write_line(&Out, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  645. line_id = Vect_write_line(&Buf, GV_BOUNDARY, arr_bc_pts.iPoints[i], BCats);
  646. Vect_destroy_line_struct(arr_bc_pts.iPoints[i]);
  647. arr_bc[buffers_count].inner[i] = line_id;
  648. }
  649. G_free(arr_bc_pts.iPoints);
  650. }
  651. buffers_count++;
  652. #endif
  653. }
  654. }
  655. }
  656. #ifdef HAVE_GEOS
  657. finishGEOS();
  658. #endif
  659. verbose = G_verbose();
  660. G_message(_("Cleaning buffers..."));
  661. /* Break lines */
  662. G_message(_("Building parts of topology..."));
  663. Vect_build_partial(&Out, GV_BUILD_BASE);
  664. /* Warning: snapping must be done, otherwise colinear boundaries are not broken and
  665. * topology cannot be built (the same angle). But snapping distance must be very, very
  666. * small, otherwise counterclockwise boundaries can appear in areas outside the buffer.
  667. * I have done some tests on real data (projected) and threshold 1e-8 was not enough,
  668. * Snapping threshold 1e-7 seems to work. Don't increase until we find example
  669. * where it is not sufficient. RB */
  670. /* TODO: look at snapping threshold better, calculate some theoretical value to avoid
  671. * the same angles of lines at nodes, don't forget about LongLat data, probably
  672. * calculate different threshold for each map, depending on map's bounding box
  673. * and/or distance and tolerance */
  674. G_message(_("Snapping boundaries..."));
  675. Vect_snap_lines(&Out, GV_BOUNDARY, 1e-7, NULL);
  676. G_message(_("Breaking polygons..."));
  677. Vect_break_polygons(&Out, GV_BOUNDARY, NULL);
  678. G_message(_("Removing duplicates..."));
  679. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  680. do {
  681. G_message(_("Breaking boundaries..."));
  682. Vect_break_lines(&Out, GV_BOUNDARY, NULL);
  683. G_message(_("Removing duplicates..."));
  684. Vect_remove_duplicates(&Out, GV_BOUNDARY, NULL);
  685. G_message(_("Cleaning boundaries at nodes"));
  686. } while (Vect_clean_small_angles_at_nodes(&Out, GV_BOUNDARY, NULL) > 0);
  687. /* Dangles and bridges don't seem to be necessary if snapping is small enough. */
  688. /* Still needed for larger buffer distances ? */
  689. /*
  690. G_message(_("Removing dangles..."));
  691. Vect_remove_dangles(&Out, GV_BOUNDARY, -1, NULL);
  692. G_message (_("Removing bridges..."));
  693. Vect_remove_bridges(&Out, NULL);
  694. */
  695. G_message(_("Attaching islands..."));
  696. Vect_build_partial(&Out, GV_BUILD_ATTACH_ISLES);
  697. if (!cats_flag->answer) {
  698. /* Calculate new centroids for all areas */
  699. nareas = Vect_get_num_areas(&Out);
  700. Areas = (char *)G_calloc(nareas + 1, sizeof(char));
  701. G_message(_("Calculating centroids for all areas..."));
  702. G_percent(0, nareas, 2);
  703. for (area = 1; area <= nareas; area++) {
  704. double x, y;
  705. G_percent(area, nareas, 2);
  706. G_debug(3, "area = %d", area);
  707. if (!Vect_area_alive(&Out, area))
  708. continue;
  709. ret = Vect_get_point_in_area(&Out, area, &x, &y);
  710. if (ret < 0) {
  711. G_warning(_("Cannot calculate area centroid"));
  712. continue;
  713. }
  714. ret = point_in_buffer(arr_bc, &si, &Buf, x, y);
  715. if (ret) {
  716. G_debug(3, " -> in buffer");
  717. Areas[area] = 1;
  718. }
  719. }
  720. /* Make a list of boundaries to be deleted (both sides inside) */
  721. nlines = Vect_get_num_lines(&Out);
  722. G_debug(3, "nlines = %d", nlines);
  723. Lines = (char *)G_calloc(nlines + 1, sizeof(char));
  724. G_message(_("Generating list of boundaries to be deleted..."));
  725. for (line = 1; line <= nlines; line++) {
  726. int j, side[2], areas[2];
  727. G_percent(line, nlines, 2);
  728. G_debug(3, "line = %d", line);
  729. if (!Vect_line_alive(&Out, line))
  730. continue;
  731. Vect_get_line_areas(&Out, line, &side[0], &side[1]);
  732. for (j = 0; j < 2; j++) {
  733. if (side[j] == 0) { /* area/isle not build */
  734. areas[j] = 0;
  735. }
  736. else if (side[j] > 0) { /* area */
  737. areas[j] = side[j];
  738. }
  739. else { /* < 0 -> island */
  740. areas[j] = Vect_get_isle_area(&Out, abs(side[j]));
  741. }
  742. }
  743. G_debug(3, " areas = %d , %d -> Areas = %d, %d", areas[0], areas[1],
  744. Areas[areas[0]], Areas[areas[1]]);
  745. if (Areas[areas[0]] && Areas[areas[1]])
  746. Lines[line] = 1;
  747. }
  748. G_free(Areas);
  749. /* Delete boundaries */
  750. G_message(_("Deleting boundaries..."));
  751. for (line = 1; line <= nlines; line++) {
  752. G_percent(line, nlines, 2);
  753. if (!Vect_line_alive(&Out, line))
  754. continue;
  755. if (Lines[line]) {
  756. G_debug(3, " delete line %d", line);
  757. Vect_delete_line(&Out, line);
  758. }
  759. else {
  760. /* delete incorrect boundaries */
  761. int side[2];
  762. Vect_get_line_areas(&Out, line, &side[0], &side[1]);
  763. if (!side[0] && !side[1])
  764. Vect_delete_line(&Out, line);
  765. }
  766. }
  767. G_free(Lines);
  768. }
  769. /* Create new centroids */
  770. Vect_reset_cats(Cats);
  771. Vect_cat_set(Cats, 1, 1);
  772. nareas = Vect_get_num_areas(&Out);
  773. G_message(_("Calculating centroids for areas..."));
  774. for (area = 1; area <= nareas; area++) {
  775. double x, y;
  776. G_percent(area, nareas, 2);
  777. G_debug(3, "area = %d", area);
  778. if (!Vect_area_alive(&Out, area))
  779. continue;
  780. ret = Vect_get_point_in_area(&Out, area, &x, &y);
  781. if (ret < 0) {
  782. G_warning(_("Cannot calculate area centroid"));
  783. continue;
  784. }
  785. if (cats_flag->answer)
  786. ret = buffer_cats(arr_bc, &si, &Buf, x, y, Cats);
  787. else
  788. ret = point_in_buffer(arr_bc, &si, &Buf, x, y);
  789. if (ret) {
  790. Vect_reset_line(Points);
  791. Vect_append_point(Points, x, y, 0.);
  792. Vect_write_line(&Out, GV_CENTROID, Points, Cats);
  793. }
  794. }
  795. Vect_spatial_index_destroy(&si);
  796. Vect_close(&Buf);
  797. Vect_delete(bufname);
  798. G_set_verbose(verbose);
  799. if (cats_flag->answer)
  800. Vect_copy_tables(&In, &Out, field);
  801. Vect_close(&In);
  802. Vect_build_partial(&Out, GV_BUILD_NONE);
  803. Vect_build(&Out);
  804. Vect_close(&Out);
  805. exit(EXIT_SUCCESS);
  806. }