copy.c 17 KB

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