copy.c 17 KB

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