map.c 21 KB

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