copy.c 19 KB

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