map.c 20 KB

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