map.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*!
  2. * \file map.c
  3. *
  4. * \brief Vector library - Manipulate with vector map
  5. *
  6. * Higher level functions for reading/writing/manipulating vectors.
  7. *
  8. * (C) 2001-2009 by the GRASS Development Team
  9. *
  10. * This program is free software under the GNU General Public License
  11. * (>=v2). Read the file COPYING that comes with GRASS for details.
  12. *
  13. * \author Original author CERL, probably Dave Gerdes or Mike
  14. * Higgins.
  15. * \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  16. */
  17. #include <grass/config.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <dirent.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <grass/glocale.h>
  27. #include <grass/gis.h>
  28. #include <grass/vector.h>
  29. #include <grass/dbmi.h>
  30. #include <grass/glocale.h>
  31. /*!
  32. \brief Copy all alive elements of opened vector map to another opened 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. int i, type, nlines, ret;
  41. struct line_pnts *Points;
  42. struct line_cats *Cats;
  43. Points = Vect_new_line_struct();
  44. Cats = Vect_new_cats_struct();
  45. if (Vect_level(In) < 1)
  46. G_fatal_error("Vect_copy_map_lines(): %s",
  47. _("input vector map is not open"));
  48. ret = 0;
  49. /* Note: sometimes is important to copy on level 2 (pseudotopo centroids)
  50. * and sometimes on level 1 if build take too long time */
  51. if (Vect_level(In) >= 2) {
  52. nlines = Vect_get_num_lines(In);
  53. for (i = 1; i <= nlines; i++) {
  54. if (!Vect_line_alive(In, i))
  55. continue;
  56. type = Vect_read_line(In, Points, Cats, i);
  57. if (type == -1) {
  58. G_warning(_("Unable to read vector map <%s>"),
  59. Vect_get_full_name(In));
  60. ret = 1;
  61. break;
  62. }
  63. if (type == 0)
  64. continue; /* dead line */
  65. Vect_write_line(Out, type, Points, Cats);
  66. }
  67. }
  68. else { /* Level 1 */
  69. Vect_rewind(In);
  70. while (1) {
  71. type = Vect_read_next_line(In, Points, Cats);
  72. if (type == -1) {
  73. G_warning(_("Unable to read vector map <%s>"),
  74. Vect_get_full_name(In));
  75. ret = 1;
  76. break;
  77. }
  78. else if (type == -2) { /* EOF */
  79. break;
  80. }
  81. else if (type == 0) { /* dead line */
  82. continue;
  83. }
  84. Vect_write_line(Out, type, Points, Cats);
  85. }
  86. }
  87. Vect_destroy_line_struct(Points);
  88. Vect_destroy_cats_struct(Cats);
  89. return ret;
  90. }
  91. /*
  92. \brief Copy file
  93. \param src source file
  94. \param[out] dst destination file
  95. \return 0 OK
  96. \return 1 error
  97. */
  98. static int copy_file(const char *src, const char *dst)
  99. {
  100. char buf[1024];
  101. int fd, fd2;
  102. FILE *f2;
  103. int len, len2;
  104. if ((fd = open(src, O_RDONLY)) < 0)
  105. return 1;
  106. /* if((fd2 = open(dst, O_CREAT|O_TRUNC|O_WRONLY)) < 0) */
  107. if ((f2 = fopen(dst, "w")) == NULL) {
  108. close(fd);
  109. return 1;
  110. }
  111. fd2 = fileno(f2);
  112. while ((len = read(fd, buf, 1024)) > 0) {
  113. while (len && (len2 = write(fd2, buf, len)) >= 0)
  114. len -= len2;
  115. }
  116. close(fd);
  117. /* close(fd2); */
  118. fclose(f2);
  119. if (len == -1 || len2 == -1)
  120. return 1;
  121. return 0;
  122. }
  123. /*!
  124. \brief Copy a map including attribute tables
  125. Old vector is deleted
  126. \param in input vector map name
  127. \param mapset mapset name
  128. \param out output vector map name
  129. \return -1 error
  130. \return 0 success
  131. */
  132. int
  133. Vect_copy(const char *in, const char *mapset, const char *out)
  134. {
  135. int i, n, ret, type;
  136. struct Map_info In, Out;
  137. struct field_info *Fi, *Fin;
  138. char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX];
  139. struct stat info;
  140. const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT,
  141. GV_HEAD_ELEMENT, GV_HIST_ELEMENT,
  142. GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT,
  143. NULL
  144. };
  145. const char *inmapset;
  146. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  147. dbDriver *driver;
  148. G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out);
  149. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  150. if (Vect_legal_filename(out) < 0)
  151. G_fatal_error(_("Vector map name is not SQL compliant"));
  152. inmapset = G_find_vector2(in, mapset);
  153. if (!inmapset) {
  154. G_warning(_("Unable to find vector map <%s> in <%s>"), in, mapset);
  155. return -1;
  156. }
  157. mapset = inmapset;
  158. /* remove mapset from fully qualified name, confuses G__file_name() */
  159. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  160. in = xname;
  161. }
  162. /* Delete old vector if it exists */
  163. if (G_find_vector2(out, G_mapset())) {
  164. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  165. out);
  166. ret = Vect_delete(out);
  167. if (ret != 0) {
  168. G_warning(_("Unable to delete vector map <%s>"), out);
  169. return -1;
  170. }
  171. }
  172. /* Copy the directory */
  173. G__make_mapset_element(GV_DIRECTORY);
  174. sprintf(buf, "%s/%s", GV_DIRECTORY, out);
  175. G__make_mapset_element(buf);
  176. i = 0;
  177. while (files[i]) {
  178. sprintf(buf, "%s/%s", in, files[i]);
  179. G__file_name(old_path, GV_DIRECTORY, buf, mapset);
  180. sprintf(buf, "%s/%s", out, files[i]);
  181. G__file_name(new_path, GV_DIRECTORY, buf, G_mapset());
  182. if (stat(old_path, &info) == 0) { /* file exists? */
  183. G_debug(2, "copy %s to %s", old_path, new_path);
  184. if (copy_file(old_path, new_path)) {
  185. G_warning(_("Unable to copy vector map <%s> to <%s>"),
  186. old_path, new_path);
  187. }
  188. }
  189. i++;
  190. }
  191. G__file_name(old_path, GV_DIRECTORY, in, mapset);
  192. G__file_name(new_path, GV_DIRECTORY, out, G_mapset());
  193. /* Open input */
  194. Vect_set_open_level(1);
  195. Vect_open_old_head(&In, in, mapset);
  196. if (In.format != GV_FORMAT_NATIVE) { /* Done */
  197. Vect_close(&In);
  198. return 0;
  199. }
  200. /* Open output */
  201. Vect_open_update_head(&Out, out, G_mapset());
  202. /* Copy tables */
  203. n = Vect_get_num_dblinks(&In);
  204. type = GV_1TABLE;
  205. if (n > 1)
  206. type = GV_MTABLE;
  207. for (i = 0; i < n; i++) {
  208. Fi = Vect_get_dblink(&In, i);
  209. if (Fi == NULL) {
  210. G_warning(_("Database connection not defined for layer %d"),
  211. In.dblnk->field[i].number);
  212. Vect_close(&In);
  213. Vect_close(&Out);
  214. return -1;
  215. }
  216. Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type);
  217. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  218. Fi->driver, Fi->database, Fi->table, Fin->driver,
  219. Fin->database, Fin->table);
  220. Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key,
  221. Fin->database, Fin->driver);
  222. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  223. Fin->driver, Vect_subst_var(Fin->database, &Out),
  224. Fin->table);
  225. if (ret == DB_FAILED) {
  226. G_warning(_("Unable to copy table <%s>"), Fin->table);
  227. Vect_close(&In);
  228. Vect_close(&Out);
  229. return -1;
  230. }
  231. driver =
  232. db_start_driver_open_database(Fin->driver,
  233. Vect_subst_var(Fin->database,
  234. &Out));
  235. if (driver == NULL) {
  236. G_warning(_("Unable to open database <%s> by driver <%s>"),
  237. Fin->database, Fin->driver);
  238. }
  239. else {
  240. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  241. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  242. Fi->table, Fi->key);
  243. db_close_database_shutdown_driver(driver);
  244. }
  245. }
  246. Vect_close(&In);
  247. Vect_close(&Out);
  248. return 0;
  249. }
  250. /*!
  251. \brief Rename a map.
  252. Attribute tables are created in the same database where input tables were stored.
  253. The original format (native/OGR) is used.
  254. Old map ('out') is deleted!!!
  255. \param in input vector map name
  256. \param out output vector map name
  257. \return -1 error
  258. \return 0 success
  259. */
  260. int Vect_rename(const char *in, const char *out)
  261. {
  262. int i, n, ret, type;
  263. struct Map_info Map;
  264. struct field_info *Fin, *Fout;
  265. int *fields;
  266. dbDriver *driver;
  267. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  268. G_debug(2, "Rename vector '%s' to '%s'", in, out);
  269. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  270. if (Vect_legal_filename(out) < 0)
  271. G_fatal_error(_("Vector map name is not SQL compliant"));
  272. /* Delete old vector if it exists */
  273. if (G_find_vector2(out, G_mapset())) {
  274. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  275. out);
  276. Vect_delete(out);
  277. }
  278. /* remove mapset from fully qualified name */
  279. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  280. in = xname;
  281. }
  282. /* Move the directory */
  283. ret = G_rename(GV_DIRECTORY, in, out);
  284. if (ret == 0) {
  285. G_warning(_("Vector map <%s> not found"), in);
  286. return -1;
  287. }
  288. else if (ret == -1) {
  289. G_warning(_("Unable to copy vector map <%s> to <%s>"), in, out);
  290. return -1;
  291. }
  292. /* Rename all tables if the format is native */
  293. Vect_set_open_level(1);
  294. Vect_open_update_head(&Map, out, G_mapset());
  295. if (Map.format != GV_FORMAT_NATIVE) { /* Done */
  296. Vect_close(&Map);
  297. return 0;
  298. }
  299. /* Copy tables */
  300. n = Vect_get_num_dblinks(&Map);
  301. type = GV_1TABLE;
  302. if (n > 1)
  303. type = GV_MTABLE;
  304. /* Make the list of fields */
  305. fields = (int *)G_malloc(n * sizeof(int));
  306. for (i = 0; i < n; i++) {
  307. Fin = Vect_get_dblink(&Map, i);
  308. fields[i] = Fin->number;
  309. }
  310. for (i = 0; i < n; i++) {
  311. G_debug(3, "field[%d] = %d", i, fields[i]);
  312. Fin = Vect_get_field(&Map, fields[i]);
  313. if (Fin == NULL) {
  314. G_warning(_("Database connection not defined for layer %d"),
  315. fields[i]);
  316. Vect_close(&Map);
  317. return -1;
  318. }
  319. Fout = Vect_default_field_info(&Map, Fin->number, Fin->name, type);
  320. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  321. Fin->driver, Fin->database, Fin->table, Fout->driver,
  322. Fout->database, Fout->table);
  323. /* TODO: db_rename_table instead of db_copy_table */
  324. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  325. Fout->driver, Vect_subst_var(Fout->database,
  326. &Map), Fout->table);
  327. if (ret == DB_FAILED) {
  328. G_warning(_("Unable to copy table <%s>"), Fin->table);
  329. Vect_close(&Map);
  330. return -1;
  331. }
  332. /* Change the link */
  333. Vect_map_del_dblink(&Map, Fin->number);
  334. Vect_map_add_dblink(&Map, Fout->number, Fout->name, Fout->table,
  335. Fin->key, Fout->database, Fout->driver);
  336. /* Delete old table */
  337. ret = db_delete_table(Fin->driver, Fin->database, Fin->table);
  338. if (ret == DB_FAILED) {
  339. G_warning(_("Unable to delete table <%s>"), Fin->table);
  340. Vect_close(&Map);
  341. return -1;
  342. }
  343. driver =
  344. db_start_driver_open_database(Fout->driver,
  345. Vect_subst_var(Fout->database,
  346. &Map));
  347. if (driver == NULL) {
  348. G_warning(_("Unable to open database <%s> by driver <%s>"),
  349. Fout->database, Fout->driver);
  350. }
  351. else {
  352. if (db_create_index2(driver, Fout->table, Fin->key) != DB_OK)
  353. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  354. Fout->table, Fout->key);
  355. db_close_database_shutdown_driver(driver);
  356. }
  357. }
  358. Vect_close(&Map);
  359. free(fields);
  360. return 0;
  361. }
  362. /*!
  363. \brief Delete vector map including attribute tables
  364. \param map vector map name
  365. \return -1 error
  366. \return 0 success
  367. */
  368. int Vect_delete(const char *map)
  369. {
  370. int i, n, ret;
  371. struct Map_info Map;
  372. struct field_info *Fi;
  373. char buf[GPATH_MAX];
  374. DIR *dir;
  375. struct dirent *ent;
  376. const char *tmp;
  377. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  378. G_debug(3, "Delete vector '%s'", map);
  379. /* remove mapset from fully qualified name */
  380. if (G_name_is_fully_qualified(map, xname, xmapset)) {
  381. map = xname;
  382. }
  383. if (map == NULL || strlen(map) == 0) {
  384. G_warning(_("Invalid vector map name <%s>"), map ? map : "null");
  385. return -1;
  386. }
  387. sprintf(buf, "%s/%s/%s/%s/%s/%s", G_gisdbase(), G_location(),
  388. G_mapset(), GV_DIRECTORY, map, GV_DBLN_ELEMENT);
  389. G_debug(1, "dbln file: %s", buf);
  390. if (access(buf, F_OK) == 0) {
  391. /* Open input */
  392. Vect_set_open_level(1); /* Topo not needed */
  393. ret = Vect_open_old_head(&Map, map, G_mapset());
  394. if (ret < 1) {
  395. G_warning(_("Unable to open header file for vector map <%s>"),
  396. map);
  397. return -1;
  398. }
  399. /* Delete all tables, NOT external (OGR) */
  400. if (Map.format == GV_FORMAT_NATIVE) {
  401. n = Vect_get_num_dblinks(&Map);
  402. for (i = 0; i < n; i++) {
  403. Fi = Vect_get_dblink(&Map, i);
  404. if (Fi == NULL) {
  405. G_warning(_("Database connection not defined for layer %d"),
  406. Map.dblnk->field[i].number);
  407. Vect_close(&Map);
  408. return -1;
  409. }
  410. G_debug(3, "Delete drv:db:table '%s:%s:%s'", Fi->driver,
  411. Fi->database, Fi->table);
  412. ret = db_table_exists(Fi->driver, Fi->database, Fi->table);
  413. if (ret == -1) {
  414. G_warning(_("Unable to find table <%s> linked to vector map <%s>"),
  415. Fi->table, map);
  416. Vect_close(&Map);
  417. return -1;
  418. }
  419. if (ret == 1) {
  420. ret =
  421. db_delete_table(Fi->driver, Fi->database, Fi->table);
  422. if (ret == DB_FAILED) {
  423. G_warning(_("Unable to delete table <%s>"), Fi->table);
  424. Vect_close(&Map);
  425. return -1;
  426. }
  427. }
  428. else {
  429. G_warning(_("Table <%s> linked to vector map <%s> does not exist"),
  430. Fi->table, map);
  431. }
  432. }
  433. }
  434. Vect_close(&Map);
  435. }
  436. /* Delete all files from vector/name directory */
  437. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  438. G_debug(3, "opendir '%s'", buf);
  439. dir = opendir(buf);
  440. if (dir == NULL) {
  441. G_warning(_("Unable to open directory '%s'"), buf);
  442. return -1;
  443. }
  444. while ((ent = readdir(dir))) {
  445. G_debug(3, "file = '%s'", ent->d_name);
  446. if ((strcmp(ent->d_name, ".") == 0) ||
  447. (strcmp(ent->d_name, "..") == 0))
  448. continue;
  449. sprintf(buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map,
  450. ent->d_name);
  451. G_debug(3, "delete file '%s'", buf);
  452. ret = unlink(buf);
  453. if (ret == -1) {
  454. G_warning(_("Unable to delete file '%s'"), buf);
  455. closedir(dir);
  456. return -1;
  457. }
  458. }
  459. closedir(dir);
  460. /* NFS can create .nfsxxxxxxxx files for those deleted
  461. * -> we have to move the directory to ./tmp before it is deleted */
  462. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  463. tmp = G_tempfile();
  464. G_debug(3, "rename '%s' to '%s'", buf, tmp);
  465. ret = rename(buf, tmp);
  466. if (ret == -1) {
  467. G_warning(_("Unable to rename directory '%s' to '%s'"), buf, tmp);
  468. return -1;
  469. }
  470. G_debug(3, "remove directory '%s'", tmp);
  471. /* Warning: remove() fails on Windows */
  472. ret = rmdir(tmp);
  473. if (ret == -1) {
  474. G_warning(_("Unable to remove directory '%s'"), tmp);
  475. return -1;
  476. }
  477. return 0;
  478. }
  479. /*!
  480. \brief Copy tables linked to vector map.
  481. All if field = 0, or table defined by given field if field > 0
  482. Notice, that if input map has no tables defined, it will copy
  483. nothing and return 0 (success).
  484. \param In input vector map
  485. \param[out] Out output vector map
  486. \param field layer number
  487. \return 0 on success
  488. \return -1 on error
  489. */
  490. int Vect_copy_tables(const struct Map_info *In, struct Map_info *Out, int field)
  491. {
  492. int i, n, ret, type;
  493. struct field_info *Fi, *Fin;
  494. dbDriver *driver;
  495. n = Vect_get_num_dblinks(In);
  496. G_debug(2, "Vect_copy_tables(): copying %d tables",n);
  497. type = GV_1TABLE;
  498. if (n > 1)
  499. type = GV_MTABLE;
  500. for (i = 0; i < n; i++) {
  501. Fi = Vect_get_dblink(In, i);
  502. if (Fi == NULL) {
  503. G_warning(_("Database connection not defined for layer %d"),
  504. In->dblnk->field[i].number);
  505. return -1;
  506. }
  507. if (field > 0 && Fi->number != field)
  508. continue;
  509. Fin = Vect_default_field_info(Out, Fi->number, Fi->name, type);
  510. G_debug(2, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  511. Fi->driver, Fi->database, Fi->table, Fin->driver,
  512. Fin->database, Fin->table);
  513. ret =
  514. Vect_map_add_dblink(Out, Fi->number, Fi->name, Fin->table,
  515. Fi->key, Fin->database, Fin->driver);
  516. if (ret == -1) {
  517. G_warning(_("Unable to add database link for vector map <%s>"),
  518. Out->name);
  519. return -1;
  520. }
  521. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  522. Fin->driver, Vect_subst_var(Fin->database, Out),
  523. Fin->table);
  524. if (ret == DB_FAILED) {
  525. G_warning(_("Unable to copy table <%s>"), Fin->table);
  526. return -1;
  527. }
  528. driver =
  529. db_start_driver_open_database(Fin->driver,
  530. Vect_subst_var(Fin->database, Out));
  531. if (driver == NULL) {
  532. G_warning(_("Unable to open database <%s> by driver <%s>"),
  533. Fin->database, Fin->driver);
  534. }
  535. else {
  536. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  537. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  538. Fin->table, Fin->key);
  539. db_close_database_shutdown_driver(driver);
  540. }
  541. }
  542. return 0;
  543. }
  544. /*!
  545. \brief Copy table linked to vector map based on type.
  546. \param In input vector map
  547. \param[out] Out output vector map
  548. \param field_in input layer number
  549. \param field_out output layer number
  550. \param field_name layer name
  551. \param type feature type
  552. \return 0 on success
  553. \return -1 on error
  554. */
  555. int
  556. Vect_copy_table(const struct Map_info *In, struct Map_info *Out, int field_in,
  557. int field_out, const char *field_name, int type)
  558. {
  559. return Vect_copy_table_by_cats(In, Out, field_in, field_out, field_name,
  560. type, NULL, 0);
  561. }
  562. /*!
  563. \brief Copy table linked to vector map based on category numbers.
  564. \param In input vector map
  565. \param[out] Out output vector map
  566. \param field_in input layer number
  567. \param field_out output layer number
  568. \param field_name layer name
  569. \param type feature type
  570. \param cats pointer to array of cats or NULL
  571. \param ncats number of cats in 'cats'
  572. \return 0 on success
  573. \return -1 on error
  574. */
  575. int
  576. Vect_copy_table_by_cats(const struct Map_info *In, struct Map_info *Out,
  577. int field_in, int field_out, const char *field_name,
  578. int type, int *cats, int ncats)
  579. {
  580. int ret;
  581. struct field_info *Fi, *Fin;
  582. const char *name, *key;
  583. G_debug(2, "Vect_copy_table(): field_in = %d field_out = %d", field_in,
  584. field_out);
  585. Fi = Vect_get_field(In, field_in);
  586. if (Fi == NULL) {
  587. G_warning(_("Database connection not defined for layer %d"),
  588. field_in);
  589. return -1;
  590. }
  591. if (field_name != NULL)
  592. name = field_name;
  593. else
  594. name = Fi->name;
  595. Fin = Vect_default_field_info(Out, field_out, name, type);
  596. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  597. Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database,
  598. Fin->table);
  599. ret =
  600. Vect_map_add_dblink(Out, Fin->number, Fin->name, Fin->table, Fi->key,
  601. Fin->database, Fin->driver);
  602. if (ret == -1) {
  603. G_warning(_("Unable to add database link for vector map <%s>"),
  604. Out->name);
  605. return -1;
  606. }
  607. if (cats)
  608. key = Fi->key;
  609. else
  610. key = NULL;
  611. ret = db_copy_table_by_ints(Fi->driver, Fi->database, Fi->table,
  612. Fin->driver, Vect_subst_var(Fin->database,
  613. Out), Fin->table,
  614. key, cats, ncats);
  615. if (ret == DB_FAILED) {
  616. G_warning(_("Unable to copy table <%s>"), Fin->table);
  617. return -1;
  618. }
  619. return 0;
  620. }
  621. /*!
  622. \brief Set spatial index to be realease when vector is closed.
  623. By default, the memory occupied by spatial index is not released.
  624. \param Map vector map
  625. \return
  626. */
  627. void Vect_set_release_support(struct Map_info *Map)
  628. {
  629. Map->plus.release_support = 1;
  630. }
  631. /*!
  632. \brief By default, category index is not updated if vector is
  633. changed, this function sets category index update.
  634. WARNING: currently only category for elements is updated
  635. not for areas
  636. \param Map vector map
  637. \return
  638. */
  639. void Vect_set_category_index_update(struct Map_info *Map)
  640. {
  641. Map->plus.update_cidx = 1;
  642. }