copy.c 24 KB

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