copy.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. /*!
  2. \file lib/vector/Vlib/copy.c
  3. \brief Vector library - Copy vector features and attribute tables linked to the map
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2012-2013 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 Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. \author Update to GRASS 7 by Martin Landa <landa.martin gmail.com> (OGR/PostGIS topology support)
  11. */
  12. #include <grass/vector.h>
  13. #include <grass/glocale.h>
  14. #include "local_proto.h"
  15. /*!
  16. \brief Copy topological elements
  17. - simple features (None)
  18. - native topo (GRASS)
  19. - PostGIS Topo
  20. */
  21. #define TOPO_NONE -1
  22. #define TOPO_NATIVE 1
  23. #define TOPO_POSTGIS 2
  24. #ifdef HAVE_POSTGRES
  25. #include "pg_local_proto.h"
  26. #endif
  27. static int copy_lines_1(struct Map_info *, int, struct Map_info *);
  28. static int copy_lines_2(struct Map_info *, int, int, struct Map_info *);
  29. #if 0
  30. static int copy_nodes(const struct Map_info *, struct Map_info *);
  31. #endif
  32. static int copy_line_nodes(const struct Map_info *, int, int, struct line_pnts *,
  33. struct Map_info *);
  34. static int is_isle(const struct Map_info *, int);
  35. /*!
  36. \brief Copy all alive vector features from input vector map to
  37. output vector map
  38. \param In input vector map
  39. \param[out] Out output vector map
  40. \return 0 on success
  41. \return 1 on error
  42. */
  43. int Vect_copy_map_lines(struct Map_info *In, struct Map_info *Out)
  44. {
  45. return Vect_copy_map_lines_field(In, -1, Out);
  46. }
  47. /*!
  48. \brief Copy all alive vector features from given layer from input
  49. vector map to output vector map
  50. Note: Try to copy on level 2 otherwise level 1 is used.
  51. \param In input vector map
  52. \param field layer number (-1 for all layers)
  53. \param[out] Out output vector map
  54. \return 0 on success
  55. \return 1 on error
  56. */
  57. int Vect_copy_map_lines_field(struct Map_info *In, int field,
  58. struct Map_info *Out)
  59. {
  60. int ret, format, topo;
  61. if (Vect_level(In) < 1)
  62. G_fatal_error(_("Unable to copy features. Input vector map <%s> is not open"),
  63. Vect_get_full_name(In));
  64. format = Out->format; /* do not use Vect_maptype(), we need native
  65. format for temporary maps here */
  66. topo = TOPO_NONE;
  67. if (format == GV_FORMAT_NATIVE) {
  68. topo = TOPO_NATIVE;
  69. }
  70. else if (format == GV_FORMAT_POSTGIS && Out->fInfo.pg.toposchema_name) {
  71. int type;
  72. topo = TOPO_POSTGIS;
  73. /* get type of first feature from input vector map */
  74. Vect_rewind(In);
  75. Vect_set_constraint_type(In, GV_POINT | GV_LINES);
  76. type = Vect_read_next_line(In, NULL, NULL);
  77. if (!(type & (GV_POINTS | GV_LINES)))
  78. G_fatal_error(_("Unsupported feature type %d"), type);
  79. /* create feature table with given feature type */
  80. if (0 > Vect_write_line(Out, type, NULL, NULL)) {
  81. G_warning(_("Unable to create PostGIS layer <%s>"),
  82. Vect_get_finfo_layer_name(Out));
  83. return 1;
  84. }
  85. }
  86. /* Note: sometimes is important to copy on level 2 (pseudotopo
  87. centroids) and sometimes on level 1 if build take too long time
  88. */
  89. ret = 0;
  90. if (Vect_level(In) >= 2) {
  91. /* -> copy features on level 2 */
  92. #if 0
  93. if (topo == TOPO_POSTGIS) {
  94. /* PostGIS topology - copy also nodes */
  95. copy_nodes(In, Out);
  96. }
  97. #endif
  98. /* copy features */
  99. ret += copy_lines_2(In, field, topo, Out);
  100. if (topo == TOPO_NONE) {
  101. /* copy areas - external formats and simple features access only */
  102. ret += Vect__copy_areas(In, field, Out);
  103. }
  104. }
  105. else {
  106. /* -> copy features on level 1 */
  107. if (topo == TOPO_NONE)
  108. G_warning(_("Vector map <%s> not open on topological level. "
  109. "Areas will be skipped!"), Vect_get_full_name(In));
  110. ret += copy_lines_1(In, field, Out);
  111. }
  112. return ret > 0 ? 1 : 0;
  113. }
  114. /*!
  115. \brief Copy vector features on level 1
  116. \param In input vector map
  117. \param field layer number (-1 for all layers)
  118. \param Out output vector map
  119. \return 0 on success
  120. \return 1 on error
  121. */
  122. int copy_lines_1(struct Map_info *In, int field, struct Map_info *Out)
  123. {
  124. int ret, type;
  125. struct line_pnts *Points;
  126. struct line_cats *Cats;
  127. Points = Vect_new_line_struct();
  128. Cats = Vect_new_cats_struct();
  129. ret = 0;
  130. Vect_rewind(In);
  131. while (TRUE) {
  132. type = Vect_read_next_line(In, Points, Cats);
  133. if (type == -1) {
  134. G_warning(_("Unable to read vector map <%s>"),
  135. Vect_get_full_name(In));
  136. ret = 1;
  137. break;
  138. }
  139. else if (type == -2) { /* EOF */
  140. break; /* free allocated space and return */
  141. }
  142. else if (type == 0) { /* dead line */
  143. continue;
  144. }
  145. /* don't skip boundaries if field != -1 */
  146. if (field != -1 && !(type & GV_BOUNDARY) &&
  147. Vect_cat_get(Cats, field, NULL) == 0)
  148. continue; /* different layer */
  149. Vect_write_line(Out, type, Points, Cats);
  150. }
  151. Vect_destroy_line_struct(Points);
  152. Vect_destroy_cats_struct(Cats);
  153. return ret;
  154. }
  155. /*!
  156. \brief Copy vector features on level 2
  157. \param In input vector map
  158. \param field layer number (-1 for all layers)
  159. \param topo topo access (none, native, postgis)
  160. \param Out output vector map
  161. \return 0 on success
  162. \return 1 on error
  163. */
  164. int copy_lines_2(struct Map_info *In, int field, int topo, struct Map_info *Out)
  165. {
  166. int i, type, nlines, nskipped;
  167. int ret, left, rite, centroid, with_z;
  168. struct line_pnts *Points, *CPoints, *NPoints;
  169. struct line_cats *Cats, *CCats;
  170. Points = Vect_new_line_struct();
  171. CPoints = Vect_new_line_struct();
  172. NPoints = Vect_new_line_struct();
  173. Cats = Vect_new_cats_struct();
  174. CCats = Vect_new_cats_struct();
  175. with_z = Vect_is_3d(In);
  176. ret = 0;
  177. nlines = Vect_get_num_lines(In);
  178. if (topo == TOPO_NONE) {
  179. const char *ftype;
  180. ftype = Vect_get_finfo_geometry_type(Out);
  181. G_debug(2, "feature type: %s", ftype ? ftype : "?");
  182. if (!ftype)
  183. G_message(_("Copying features..."));
  184. else
  185. G_message(_("Copying features (%s)..."), ftype);
  186. }
  187. else
  188. G_message(_("Copying features..."));
  189. Vect_append_point(NPoints, 0., 0., 0.);
  190. nskipped = 0;
  191. for (i = 1; i <= nlines; i++) {
  192. if (!Vect_line_alive(In, i))
  193. continue;
  194. G_percent(i, nlines, 2);
  195. type = Vect_read_line(In, Points, Cats, i);
  196. if (type == -1) {
  197. G_warning(_("Unable to read vector map <%s>"),
  198. Vect_get_full_name(In));
  199. ret = 1;
  200. break; /* free allocated space and return */
  201. }
  202. if (type == 0)
  203. continue; /* dead line */
  204. if (topo == TOPO_NONE && (type == GV_CENTROID || type == GV_BOUNDARY)) {
  205. /* OGR/PostGIS layers (simple features): centroids are
  206. stored in topo polygon defined by areas (topo required)
  207. */
  208. continue;
  209. }
  210. /* don't skips boundaries if field != -1 */
  211. if (field != -1) {
  212. if (type & GV_BOUNDARY) {
  213. if (Vect_cat_get(Cats, field, NULL) == 0) {
  214. int skip_bndry = TRUE;
  215. Vect_get_line_areas(In, i, &left, &rite);
  216. if (left < 0)
  217. left = Vect_get_isle_area(In, abs(left));
  218. if (left > 0) {
  219. if ((centroid =
  220. Vect_get_area_centroid(In, left)) > 0) {
  221. Vect_read_line(In, CPoints, CCats, centroid);
  222. if (Vect_cat_get(CCats, field, NULL) != 0)
  223. skip_bndry = FALSE;
  224. }
  225. }
  226. if (skip_bndry) {
  227. if (rite < 0)
  228. rite = Vect_get_isle_area(In, abs(rite));
  229. if (rite > 0) {
  230. if ((centroid =
  231. Vect_get_area_centroid(In, rite)) > 0) {
  232. Vect_read_line(In, CPoints, CCats,
  233. centroid);
  234. if (Vect_cat_get(CCats, field, NULL) != 0)
  235. skip_bndry = FALSE;
  236. }
  237. }
  238. }
  239. if (skip_bndry)
  240. continue;
  241. }
  242. }
  243. else if (Vect_cat_get(Cats, field, NULL) == 0) {
  244. nskipped++;
  245. continue; /* different layer */
  246. }
  247. }
  248. /* copy also nodes connected to the line (PostGIS Topology
  249. * mode only) */
  250. if (topo == TOPO_POSTGIS && (type & GV_LINES)) {
  251. int n1, n2;
  252. struct P_line *Line;
  253. struct Format_info_offset *offset;
  254. offset = &(Out->fInfo.pg.offset);
  255. n1 = n2 = -1;
  256. Line = In->plus.Line[i];
  257. if (Line) {
  258. if (type == GV_LINE) {
  259. struct P_topo_l *topo = (struct P_topo_l *)Line->topo;
  260. n1 = topo->N1;
  261. n2 = topo->N2;
  262. }
  263. else if (type == GV_BOUNDARY) {
  264. struct P_topo_b *topo = (struct P_topo_b *)Line->topo;
  265. n1 = topo->N1;
  266. n2 = topo->N2;
  267. }
  268. }
  269. if (n1 > 0 && (n1 > offset->array_num || offset->array[n1-1] == 0))
  270. copy_line_nodes(In, n1, with_z, NPoints, Out);
  271. if (n2 > 0 && (n2 > offset->array_num || offset->array[n2-1] == 0))
  272. copy_line_nodes(In, n2, with_z, NPoints, Out);
  273. }
  274. if (-1 == Vect_write_line(Out, type, Points, Cats)) {
  275. G_warning(_("Writing new feature failed"));
  276. return 1;
  277. }
  278. }
  279. if (nskipped > 0)
  280. G_important_message(_("%d features without category or from different layer skipped"), nskipped);
  281. Vect_destroy_line_struct(Points);
  282. Vect_destroy_line_struct(CPoints);
  283. Vect_destroy_line_struct(NPoints);
  284. Vect_destroy_cats_struct(Cats);
  285. Vect_destroy_cats_struct(CCats);
  286. return ret;
  287. }
  288. #if 0
  289. /*!
  290. \brief Copy nodes as points (PostGIS Topology only)
  291. \param In input vector map
  292. \param Out output vector map
  293. \return 0 on success
  294. \return 1 on error
  295. */
  296. int copy_nodes(const struct Map_info *In, struct Map_info *Out)
  297. {
  298. int nnodes, node, with_z;
  299. struct line_pnts *Points;
  300. Points = Vect_new_line_struct();
  301. with_z = Vect_is_3d(In);
  302. nnodes = Vect_get_num_nodes(In);
  303. G_message(_("Exporting nodes..."));
  304. Vect_append_point(Points, 0., 0., 0.);
  305. for (node = 1; node <= nnodes; node++) {
  306. G_debug(3, "Exporting GRASS node %d", node);
  307. G_percent(node, nnodes, 5);
  308. copy_line_nodes(In, node, with_z, Points, Out);
  309. }
  310. Vect_destroy_line_struct(Points);
  311. return 0;
  312. }
  313. #endif
  314. int copy_line_nodes(const struct Map_info *In, int node, int with_z,
  315. struct line_pnts *Points, struct Map_info *Out)
  316. {
  317. double x, y, z;
  318. Vect_get_node_coor(In, node, &x, &y, &z);
  319. Points->x[0] = x;
  320. Points->y[0] = y;
  321. if (with_z)
  322. Points->z[0] = z;
  323. #ifdef HAVE_POSTGRES
  324. if (-1 == V2__write_node_pg(Out, Points)) {
  325. G_warning(_("Writing node %d failed"), node);
  326. return 1;
  327. }
  328. #else
  329. G_fatal_error(_("GRASS is not compiled with PostgreSQL support"));
  330. return 1;
  331. #endif
  332. return 0;
  333. }
  334. /*!
  335. \brief Check if area is part of an isle
  336. Check for areas that are part of isles which in turn are inside
  337. another area.
  338. \param Map pointer to Map_info struct
  339. \param area area id
  340. \return TRUE if area forms an isle otherwise FALSE
  341. */
  342. int is_isle(const struct Map_info *Map, int area)
  343. {
  344. int i, line, left, right, isle, is_isle;
  345. struct ilist *List;
  346. List = Vect_new_list();
  347. Vect_get_area_boundaries(Map, area, List);
  348. is_isle = FALSE;
  349. /* do we need to check all boundaries ? no */
  350. for (i = 0; i < List->n_values && !is_isle; i++) {
  351. line = List->value[i];
  352. if (1 != Vect_get_line_areas(Map, abs(line), &left, &right))
  353. continue;
  354. isle = line > 0 ? left : right;
  355. if (isle < 0 && Vect_get_isle_area(Map, abs(isle)) > 0) {
  356. is_isle = TRUE;
  357. break;
  358. }
  359. }
  360. G_debug(3, "is_isle(): area %d skip? -> %s", area, is_isle ? "yes" : "no");
  361. Vect_destroy_list(List);
  362. return is_isle;
  363. }
  364. /*!
  365. \brief Copy areas as polygons (OGR/PostGIS simple features access only)
  366. \param In input vector map
  367. \param field layer number (-1 for all layers)
  368. \param Out output vector map
  369. \return 0 on success
  370. \return 1 on error
  371. */
  372. int Vect__copy_areas(const struct Map_info *In, int field, struct Map_info *Out)
  373. {
  374. int i, area, nareas, cat, isle, nisles, nparts_alloc, nskipped;
  375. struct line_pnts **Points;
  376. struct line_cats *Cats;
  377. /* allocate points & cats */
  378. Points = (struct line_pnts **) G_malloc(sizeof(struct line_pnts *));
  379. Points[0] = Vect_new_line_struct();
  380. nparts_alloc = 1;
  381. Cats = Vect_new_cats_struct();
  382. /* copy areas */
  383. nskipped = 0;
  384. nareas = Vect_get_num_areas(In);
  385. if (nareas > 0)
  386. G_message(_("Exporting areas..."));
  387. for (area = 1; area <= nareas; area++) {
  388. G_debug(3, "area = %d", area);
  389. G_percent(area, nareas, 3);
  390. /* get category */
  391. Vect_reset_cats(Cats);
  392. if (field > 0) {
  393. cat = Vect_get_area_cat(In, area, field);
  394. /* skip area without category in given layer
  395. if (cat == -1) {
  396. nskipped++;
  397. continue;
  398. }
  399. */
  400. if (cat > 0)
  401. Vect_cat_set(Cats, field, cat);
  402. }
  403. /* skip isles */
  404. if (Vect_get_area_centroid(In, area) == 0) {
  405. /* no centroid - check if area forms an isle */
  406. /* this check does not make sense because the area is also
  407. * not exported if it is part of an isle inside another
  408. * area: the isle gets exported as an inner ring */
  409. if (!is_isle(In, area))
  410. G_warning(_("No centroid defined for area %d. "
  411. "Area not exported."),
  412. area);
  413. continue;
  414. }
  415. /* get outer ring (area) */
  416. Vect_get_area_points(In, area, Points[0]);
  417. /* get inner rings (isles) */
  418. nisles = Vect_get_area_num_isles(In, area);
  419. if (nisles + 1 > nparts_alloc) {
  420. /* reallocate space for isles */
  421. Points = (struct line_pnts **) G_realloc(Points,
  422. (nisles + 1) *
  423. sizeof(struct line_pnts *));
  424. for (i = nparts_alloc; i < nisles + 1; i++)
  425. Points[i] = Vect_new_line_struct();
  426. nparts_alloc = nisles + 1;
  427. }
  428. G_debug(3, "\tcat=%d, nisles=%d", cat, nisles);
  429. for (i = 0; i < nisles; i++) {
  430. isle = Vect_get_area_isle(In, area, i);
  431. Vect_get_isle_points(In, isle, Points[i + 1]);
  432. }
  433. if (In != Out) {
  434. if (0 > V2__write_area_sfa(Out, (const struct line_pnts **) Points,
  435. nisles + 1, Cats)) {
  436. G_warning(_("Writing area %d failed"), area);
  437. return -1;
  438. }
  439. }
  440. #ifdef HAVE_POSTGRES
  441. else { /* building simple features geometry from topogeometry data */
  442. if (0 > V2__update_area_pg(Out, (const struct line_pnts **) Points,
  443. nisles + 1, cat)) {
  444. G_warning(_("Writing area %d failed"), area);
  445. return -1;
  446. }
  447. }
  448. #endif
  449. }
  450. if (nskipped > 0)
  451. G_important_message(_("%d areas without category or from different layer skipped"), nskipped);
  452. /* free allocated space for isles */
  453. for (i = 0; i < nparts_alloc; i++)
  454. Vect_destroy_line_struct(Points[i]);
  455. Vect_destroy_cats_struct(Cats);
  456. return 0;
  457. }
  458. /*!
  459. \brief Copy attribute tables linked to vector map.
  460. Copy all attribute tables linked to the vector map if
  461. <em>field</em> is 0, or selected attribute table defined by given
  462. field if <em>field</em> > 0.
  463. Notice, that if input vector map has no tables defined, it will
  464. copy nothing and return 0 (success).
  465. \param In input vector map
  466. \param[out] Out output vector map
  467. \param field layer number (0 for all tables linked to the vector map)
  468. \return 0 on success
  469. \return -1 on error
  470. */
  471. int Vect_copy_tables(const struct Map_info *In, struct Map_info *Out,
  472. int field)
  473. {
  474. int i, n, ret, type;
  475. struct field_info *Fi, *Fin;
  476. dbDriver *driver;
  477. n = Vect_get_num_dblinks(In);
  478. G_debug(2, "Vect_copy_tables(): copying %d tables", n);
  479. type = GV_1TABLE;
  480. if (n > 1)
  481. type = GV_MTABLE;
  482. for (i = 0; i < n; i++) {
  483. Fi = Vect_get_dblink(In, i);
  484. if (Fi == NULL) {
  485. G_warning(_("Database connection not defined for layer %d"),
  486. In->dblnk->field[i].number);
  487. return -1;
  488. }
  489. if (field > 0 && Fi->number != field)
  490. continue;
  491. Fin = Vect_default_field_info(Out, Fi->number, Fi->name, type);
  492. G_debug(2, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  493. Fi->driver, Fi->database, Fi->table, Fin->driver,
  494. Fin->database, Fin->table);
  495. ret =
  496. Vect_map_add_dblink(Out, Fi->number, Fi->name, Fin->table,
  497. Fi->key, Fin->database, Fin->driver);
  498. if (ret == -1) {
  499. G_warning(_("Unable to add database link for vector map <%s>"),
  500. Out->name);
  501. return -1;
  502. }
  503. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  504. Fin->driver, Vect_subst_var(Fin->database, Out),
  505. Fin->table);
  506. if (ret == DB_FAILED) {
  507. G_warning(_("Unable to copy table <%s>"), Fin->table);
  508. return -1;
  509. }
  510. driver =
  511. db_start_driver_open_database(Fin->driver,
  512. Vect_subst_var(Fin->database, Out));
  513. if (driver == NULL) {
  514. G_warning(_("Unable to open database <%s> by driver <%s>"),
  515. Fin->database, Fin->driver);
  516. }
  517. else {
  518. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  519. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  520. Fin->table, Fin->key);
  521. db_close_database_shutdown_driver(driver);
  522. }
  523. }
  524. return 0;
  525. }
  526. /*!
  527. \brief Copy attribute table linked to vector map based on type.
  528. \param In input vector map
  529. \param[out] Out output vector map
  530. \param field_in input layer number
  531. \param field_out output layer number
  532. \param field_name layer name (can be NULL)
  533. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  534. \return 0 on success
  535. \return -1 on error
  536. */
  537. int Vect_copy_table(const struct Map_info *In, struct Map_info *Out, int field_in,
  538. int field_out, const char *field_name, int type)
  539. {
  540. return Vect_copy_table_by_cats(In, Out, field_in, field_out, field_name,
  541. type, NULL, 0);
  542. }
  543. /*!
  544. \brief Copy attribute table linked to vector map based on category
  545. list.
  546. If <em>cat_list</em> is NULL, then Vect_copy_table() is called.
  547. \param In input vector map
  548. \param[out] Out output vector map
  549. \param field_in input layer number
  550. \param field_out output layer number
  551. \param field_name layer name (can be NULL)
  552. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  553. \param cat_list pointer to cat_list struct (can be NULL)
  554. \return 0 on success
  555. \return -1 on error
  556. */
  557. int Vect_copy_table_by_cat_list(const struct Map_info *In, struct Map_info *Out,
  558. int field_in, int field_out, const char *field_name,
  559. int type, const struct cat_list *cat_list)
  560. {
  561. int *cats;
  562. int ncats, ret;
  563. if (cat_list) {
  564. if (Vect_cat_list_to_array(cat_list, &cats, &ncats) != 0)
  565. return -1;
  566. ret = Vect_copy_table_by_cats(In, Out, field_in, field_out, field_name,
  567. type, cats, ncats);
  568. G_free(cats);
  569. }
  570. else {
  571. ret = Vect_copy_table(In, Out, field_in, field_out, field_name,
  572. type);
  573. }
  574. return ret;
  575. }
  576. /*!
  577. \brief Copy attribute table linked to vector map based on category
  578. numbers.
  579. \param In input vector map
  580. \param[out] Out output vector map
  581. \param field_in input layer number
  582. \param field_out output layer number
  583. \param field_name layer name (can be NULL)
  584. \param type how many tables are linked to map: GV_1TABLE / GV_MTABLE
  585. \param cats pointer to array of cats or NULL
  586. \param ncats number of cats in 'cats'
  587. \return 0 on success
  588. \return -1 on error
  589. */
  590. int Vect_copy_table_by_cats(const struct Map_info *In, struct Map_info *Out,
  591. int field_in, int field_out, const char *field_name,
  592. int type, int *cats, int ncats)
  593. {
  594. int ret;
  595. struct field_info *Fi, *Fin;
  596. const char *name, *key;
  597. G_debug(2, "Vect_copy_table(): field_in = %d field_out = %d", field_in,
  598. field_out);
  599. Fi = Vect_get_field(In, field_in);
  600. if (Fi == NULL) {
  601. G_warning(_("Database connection not defined for layer %d"),
  602. field_in);
  603. return -1;
  604. }
  605. if (field_name != NULL)
  606. name = field_name;
  607. else
  608. name = Fi->name;
  609. Fin = Vect_default_field_info(Out, field_out, name, type);
  610. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  611. Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database,
  612. Fin->table);
  613. ret =
  614. Vect_map_add_dblink(Out, Fin->number, Fin->name, Fin->table, Fi->key,
  615. Fin->database, Fin->driver);
  616. if (ret == -1) {
  617. G_warning(_("Unable to add database link for vector map <%s>"),
  618. Out->name);
  619. return -1;
  620. }
  621. if (cats)
  622. key = Fi->key;
  623. else
  624. key = NULL;
  625. ret = db_copy_table_by_ints(Fi->driver, Fi->database, Fi->table,
  626. Fin->driver, Vect_subst_var(Fin->database,
  627. Out), Fin->table,
  628. key, cats, ncats);
  629. if (ret == DB_FAILED) {
  630. G_warning(_("Unable to copy table <%s>"), Fin->table);
  631. return -1;
  632. }
  633. return 0;
  634. }