copy.c 21 KB

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