map.c 21 KB

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