copy.c 23 KB

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