copy.c 19 KB

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