map.c 20 KB

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