copy_tab.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <grass/gis.h>
  2. #include <grass/vector.h>
  3. #include <grass/dbmi.h>
  4. #include <grass/glocale.h>
  5. void copy_tabs(const struct Map_info *In, struct Map_info *Out)
  6. {
  7. int nlines, line, i;
  8. int ttype, ntabs;
  9. int **ocats, *nocats, nfields, *fields;
  10. struct field_info *IFi, *OFi;
  11. struct line_cats *Cats;
  12. dbDriver *driver;
  13. ntabs = 0;
  14. /* Collect list of output cats */
  15. Cats = Vect_new_cats_struct();
  16. nfields = Vect_cidx_get_num_fields(Out);
  17. ocats = (int **)G_malloc(nfields * sizeof(int *));
  18. nocats = (int *)G_malloc(nfields * sizeof(int));
  19. fields = (int *)G_malloc(nfields * sizeof(int));
  20. for (i = 0; i < nfields; i++) {
  21. nocats[i] = 0;
  22. ocats[i] =
  23. (int *)G_malloc(Vect_cidx_get_num_cats_by_index(Out, i) *
  24. sizeof(int));
  25. fields[i] = Vect_cidx_get_field_number(Out, i);
  26. }
  27. nlines = Vect_get_num_lines(Out);
  28. for (line = 1; line <= nlines; line++) {
  29. Vect_read_line(Out, NULL, Cats, line);
  30. for (i = 0; i < Cats->n_cats; i++) {
  31. int f, j;
  32. f = -1;
  33. for (j = 0; j < nfields; j++) { /* find field */
  34. if (fields[j] == Cats->field[i]) {
  35. f = j;
  36. break;
  37. }
  38. }
  39. if (f >= 0) {
  40. ocats[f][nocats[f]] = Cats->cat[i];
  41. nocats[f]++;
  42. }
  43. }
  44. }
  45. /* Copy tables */
  46. G_message(_("Writing attributes..."));
  47. /* Number of output tabs */
  48. for (i = 0; i < Vect_get_num_dblinks(In); i++) {
  49. int j, f = -1;
  50. IFi = Vect_get_dblink(In, i);
  51. for (j = 0; j < nfields; j++) { /* find field */
  52. if (fields[j] == IFi->number) {
  53. f = j;
  54. break;
  55. }
  56. }
  57. if (f >= 0 && nocats[f] > 0)
  58. ntabs++;
  59. }
  60. if (ntabs > 1)
  61. ttype = GV_MTABLE;
  62. else
  63. ttype = GV_1TABLE;
  64. for (i = 0; i < nfields; i++) {
  65. int ret;
  66. if (fields[i] == 0)
  67. continue;
  68. if (nocats[i] == 0)
  69. continue;
  70. G_verbose_message(_("Writing attributes for layer %d"), fields[i]);
  71. /* Make a list of categories */
  72. IFi = Vect_get_field(In, fields[i]);
  73. if (!IFi) { /* no table */
  74. G_message(_("No attribute table for layer %d"), fields[i]);
  75. continue;
  76. }
  77. OFi = Vect_default_field_info(Out, IFi->number, NULL, ttype);
  78. ret = db_copy_table_by_ints(IFi->driver, IFi->database, IFi->table,
  79. OFi->driver,
  80. Vect_subst_var(OFi->database, Out),
  81. OFi->table, IFi->key, ocats[i],
  82. nocats[i]);
  83. if (ret == DB_FAILED) {
  84. G_warning(_("Unable to copy table <%s>"), IFi->table);
  85. }
  86. else {
  87. driver = db_start_driver_open_database(OFi->driver,
  88. Vect_subst_var(OFi->database,
  89. Out));
  90. if (!driver) {
  91. G_warning(_("Unable to open database <%s> with driver <%s>"),
  92. OFi->database, OFi->driver);
  93. }
  94. else {
  95. /* do not allow duplicate keys */
  96. if (db_create_index2(driver, OFi->table, IFi->key) != DB_OK) {
  97. G_warning(_("Unable to create index"));
  98. }
  99. if (db_grant_on_table(driver, OFi->table, DB_PRIV_SELECT,
  100. DB_GROUP | DB_PUBLIC) != DB_OK) {
  101. G_warning(_("Unable to grant privileges on table <%s>"),
  102. OFi->table);
  103. }
  104. db_close_database_shutdown_driver(driver);
  105. }
  106. Vect_map_add_dblink(Out, OFi->number, OFi->name, OFi->table,
  107. IFi->key, OFi->database, OFi->driver);
  108. }
  109. }
  110. }