copy.c 16 KB

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