copy.c 19 KB

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