map.c 18 KB

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