map.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. struct stat info;
  198. const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT,
  199. GV_HEAD_ELEMENT, GV_HIST_ELEMENT,
  200. GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT,
  201. NULL
  202. };
  203. const char *inmapset;
  204. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  205. dbDriver *driver;
  206. G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out);
  207. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  208. if (Vect_legal_filename(out) < 0)
  209. G_fatal_error(_("Vector map name is not SQL compliant"));
  210. inmapset = G_find_vector2(in, mapset);
  211. if (!inmapset) {
  212. G_warning(_("Unable to find vector map <%s> in <%s>"), in, mapset);
  213. return -1;
  214. }
  215. mapset = inmapset;
  216. /* remove mapset from fully qualified name, confuses G__file_name() */
  217. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  218. in = xname;
  219. }
  220. /* Delete old vector if it exists */
  221. if (G_find_vector2(out, G_mapset())) {
  222. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  223. out);
  224. ret = Vect_delete(out);
  225. if (ret != 0) {
  226. G_warning(_("Unable to delete vector map <%s>"), out);
  227. return -1;
  228. }
  229. }
  230. /* Copy the directory */
  231. G__make_mapset_element(GV_DIRECTORY);
  232. sprintf(buf, "%s/%s", GV_DIRECTORY, out);
  233. G__make_mapset_element(buf);
  234. i = 0;
  235. while (files[i]) {
  236. sprintf(buf, "%s/%s", in, files[i]);
  237. G__file_name(old_path, GV_DIRECTORY, buf, mapset);
  238. sprintf(buf, "%s/%s", out, files[i]);
  239. G__file_name(new_path, GV_DIRECTORY, buf, G_mapset());
  240. if (stat(old_path, &info) == 0) { /* file exists? */
  241. G_debug(2, "copy %s to %s", old_path, new_path);
  242. if (copy_file(old_path, new_path)) {
  243. G_warning(_("Unable to copy vector map <%s> to <%s>"),
  244. old_path, new_path);
  245. }
  246. }
  247. i++;
  248. }
  249. G__file_name(old_path, GV_DIRECTORY, in, mapset);
  250. G__file_name(new_path, GV_DIRECTORY, out, G_mapset());
  251. /* Open input */
  252. Vect_set_open_level(1);
  253. Vect_open_old_head(&In, in, mapset);
  254. if (In.format != GV_FORMAT_NATIVE) { /* Done */
  255. Vect_close(&In);
  256. return 0;
  257. }
  258. /* Open output */
  259. Vect_set_open_level(1);
  260. Vect_open_update_head(&Out, out, G_mapset());
  261. /* Copy tables */
  262. n = Vect_get_num_dblinks(&In);
  263. type = GV_1TABLE;
  264. if (n > 1)
  265. type = GV_MTABLE;
  266. for (i = 0; i < n; i++) {
  267. Fi = Vect_get_dblink(&In, i);
  268. if (Fi == NULL) {
  269. G_warning(_("Database connection not defined for layer %d"),
  270. In.dblnk->field[i].number);
  271. Vect_close(&In);
  272. Vect_close(&Out);
  273. return -1;
  274. }
  275. Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type);
  276. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  277. Fi->driver, Fi->database, Fi->table, Fin->driver,
  278. Fin->database, Fin->table);
  279. Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key,
  280. Fin->database, Fin->driver);
  281. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  282. Fin->driver, Vect_subst_var(Fin->database, &Out),
  283. Fin->table);
  284. if (ret == DB_FAILED) {
  285. G_warning(_("Unable to copy table <%s>"), Fin->table);
  286. Vect_close(&In);
  287. Vect_close(&Out);
  288. return -1;
  289. }
  290. driver =
  291. db_start_driver_open_database(Fin->driver,
  292. Vect_subst_var(Fin->database,
  293. &Out));
  294. if (driver == NULL) {
  295. G_warning(_("Unable to open database <%s> by driver <%s>"),
  296. Fin->database, Fin->driver);
  297. }
  298. else {
  299. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  300. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  301. Fi->table, Fi->key);
  302. db_close_database_shutdown_driver(driver);
  303. }
  304. }
  305. Vect_close(&In);
  306. Vect_close(&Out);
  307. return 0;
  308. }
  309. /*!
  310. \brief Rename a map.
  311. Attribute tables are created in the same database where input tables were stored.
  312. The original format (native/OGR) is used.
  313. Old map ('out') is deleted!!!
  314. \param in input vector map name
  315. \param out output vector map name
  316. \return -1 error
  317. \return 0 success
  318. */
  319. int Vect_rename(const char *in, const char *out)
  320. {
  321. int i, n, ret, type;
  322. struct Map_info Map;
  323. struct field_info *Fin, *Fout;
  324. int *fields;
  325. dbDriver *driver;
  326. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  327. G_debug(2, "Rename vector '%s' to '%s'", in, out);
  328. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  329. if (Vect_legal_filename(out) < 0)
  330. G_fatal_error(_("Vector map name is not SQL compliant"));
  331. /* Delete old vector if it exists */
  332. if (G_find_vector2(out, G_mapset())) {
  333. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  334. out);
  335. Vect_delete(out);
  336. }
  337. /* remove mapset from fully qualified name */
  338. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  339. in = xname;
  340. }
  341. /* Move the directory */
  342. ret = G_rename(GV_DIRECTORY, in, out);
  343. if (ret == 0) {
  344. G_warning(_("Vector map <%s> not found"), in);
  345. return -1;
  346. }
  347. else if (ret == -1) {
  348. G_warning(_("Unable to copy vector map <%s> to <%s>"), in, out);
  349. return -1;
  350. }
  351. /* Rename all tables if the format is native */
  352. Vect_set_open_level(1);
  353. Vect_open_update_head(&Map, out, G_mapset());
  354. if (Map.format != GV_FORMAT_NATIVE) { /* Done */
  355. Vect_close(&Map);
  356. return 0;
  357. }
  358. /* Copy tables */
  359. n = Vect_get_num_dblinks(&Map);
  360. type = GV_1TABLE;
  361. if (n > 1)
  362. type = GV_MTABLE;
  363. /* Make the list of fields */
  364. fields = (int *)G_malloc(n * sizeof(int));
  365. for (i = 0; i < n; i++) {
  366. Fin = Vect_get_dblink(&Map, i);
  367. fields[i] = Fin->number;
  368. }
  369. for (i = 0; i < n; i++) {
  370. G_debug(3, "field[%d] = %d", i, fields[i]);
  371. Fin = Vect_get_field(&Map, fields[i]);
  372. if (Fin == NULL) {
  373. G_warning(_("Database connection not defined for layer %d"),
  374. fields[i]);
  375. Vect_close(&Map);
  376. return -1;
  377. }
  378. Fout = Vect_default_field_info(&Map, Fin->number, Fin->name, type);
  379. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  380. Fin->driver, Fin->database, Fin->table, Fout->driver,
  381. Fout->database, Fout->table);
  382. /* TODO: db_rename_table instead of db_copy_table */
  383. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  384. Fout->driver, Vect_subst_var(Fout->database,
  385. &Map), Fout->table);
  386. if (ret == DB_FAILED) {
  387. G_warning(_("Unable to copy table <%s>"), Fin->table);
  388. Vect_close(&Map);
  389. return -1;
  390. }
  391. /* Change the link */
  392. Vect_map_del_dblink(&Map, Fin->number);
  393. Vect_map_add_dblink(&Map, Fout->number, Fout->name, Fout->table,
  394. Fin->key, Fout->database, Fout->driver);
  395. /* Delete old table */
  396. ret = db_delete_table(Fin->driver, Fin->database, Fin->table);
  397. if (ret == DB_FAILED) {
  398. G_warning(_("Unable to delete table <%s>"), Fin->table);
  399. Vect_close(&Map);
  400. return -1;
  401. }
  402. driver =
  403. db_start_driver_open_database(Fout->driver,
  404. Vect_subst_var(Fout->database,
  405. &Map));
  406. if (driver == NULL) {
  407. G_warning(_("Unable to open database <%s> by driver <%s>"),
  408. Fout->database, Fout->driver);
  409. }
  410. else {
  411. if (db_create_index2(driver, Fout->table, Fin->key) != DB_OK)
  412. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  413. Fout->table, Fout->key);
  414. db_close_database_shutdown_driver(driver);
  415. }
  416. }
  417. Vect_close(&Map);
  418. free(fields);
  419. return 0;
  420. }
  421. /*!
  422. \brief Delete vector map including attribute tables
  423. \param map vector map name
  424. \return -1 error
  425. \return 0 success
  426. */
  427. int Vect_delete(const char *map)
  428. {
  429. int i, n, ret;
  430. struct Map_info Map;
  431. struct field_info *Fi;
  432. char buf[GPATH_MAX];
  433. DIR *dir;
  434. struct dirent *ent;
  435. const char *tmp;
  436. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  437. G_debug(3, "Delete vector '%s'", map);
  438. /* remove mapset from fully qualified name */
  439. if (G_name_is_fully_qualified(map, xname, xmapset)) {
  440. map = xname;
  441. }
  442. if (map == NULL || strlen(map) == 0) {
  443. G_warning(_("Invalid vector map name <%s>"), map ? map : "null");
  444. return -1;
  445. }
  446. sprintf(buf, "%s/%s/%s/%s/%s/%s", G_gisdbase(), G_location(),
  447. G_mapset(), GV_DIRECTORY, map, GV_DBLN_ELEMENT);
  448. G_debug(1, "dbln file: %s", buf);
  449. if (access(buf, F_OK) == 0) {
  450. /* Open input */
  451. Vect_set_open_level(1); /* Topo not needed */
  452. ret = Vect_open_old_head(&Map, map, G_mapset());
  453. if (ret < 1) {
  454. G_warning(_("Unable to open header file for vector map <%s>"),
  455. map);
  456. return -1;
  457. }
  458. /* Delete all tables, NOT external (OGR) */
  459. if (Map.format == GV_FORMAT_NATIVE) {
  460. n = Vect_get_num_dblinks(&Map);
  461. for (i = 0; i < n; i++) {
  462. Fi = Vect_get_dblink(&Map, i);
  463. if (Fi == NULL) {
  464. G_warning(_("Database connection not defined for layer %d"),
  465. Map.dblnk->field[i].number);
  466. Vect_close(&Map);
  467. return -1;
  468. }
  469. G_debug(3, "Delete drv:db:table '%s:%s:%s'", Fi->driver,
  470. Fi->database, Fi->table);
  471. ret = db_table_exists(Fi->driver, Fi->database, Fi->table);
  472. if (ret == -1) {
  473. G_warning(_("Unable to find table <%s> linked to vector map <%s>"),
  474. Fi->table, map);
  475. Vect_close(&Map);
  476. return -1;
  477. }
  478. if (ret == 1) {
  479. ret =
  480. db_delete_table(Fi->driver, Fi->database, Fi->table);
  481. if (ret == DB_FAILED) {
  482. G_warning(_("Unable to delete table <%s>"),
  483. Fi->table);
  484. Vect_close(&Map);
  485. return -1;
  486. }
  487. }
  488. else {
  489. G_warning(_("Table <%s> linked to vector map <%s> does not exist"),
  490. Fi->table, map);
  491. }
  492. }
  493. }
  494. Vect_close(&Map);
  495. }
  496. /* Delete all files from vector/name directory */
  497. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  498. G_debug(3, "opendir '%s'", buf);
  499. dir = opendir(buf);
  500. if (dir == NULL) {
  501. G_warning(_("Unable to open directory '%s'"), buf);
  502. return -1;
  503. }
  504. while ((ent = readdir(dir))) {
  505. G_debug(3, "file = '%s'", ent->d_name);
  506. if ((strcmp(ent->d_name, ".") == 0) ||
  507. (strcmp(ent->d_name, "..") == 0))
  508. continue;
  509. sprintf(buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map,
  510. ent->d_name);
  511. G_debug(3, "delete file '%s'", buf);
  512. ret = unlink(buf);
  513. if (ret == -1) {
  514. G_warning(_("Unable to delete file '%s'"), buf);
  515. closedir(dir);
  516. return -1;
  517. }
  518. }
  519. closedir(dir);
  520. /* NFS can create .nfsxxxxxxxx files for those deleted
  521. * -> we have to move the directory to ./tmp before it is deleted */
  522. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  523. tmp = G_tempfile();
  524. G_debug(3, "rename '%s' to '%s'", buf, tmp);
  525. ret = rename(buf, tmp);
  526. if (ret == -1) {
  527. G_warning(_("Unable to rename directory '%s' to '%s'"), buf, tmp);
  528. return -1;
  529. }
  530. G_debug(3, "remove directory '%s'", tmp);
  531. /* Warning: remove() fails on Windows */
  532. ret = rmdir(tmp);
  533. if (ret == -1) {
  534. G_warning(_("Unable to remove directory '%s'"), tmp);
  535. return -1;
  536. }
  537. return 0;
  538. }
  539. /*!
  540. \brief Copy tables linked to vector map.
  541. All if field = 0, or table defined by given field if field > 0
  542. Notice, that if input map has no tables defined, it will copy
  543. nothing and return 0 (success).
  544. \param In input vector map
  545. \param[out] Out output vector map
  546. \param field layer number
  547. \return 0 on success
  548. \return -1 on error
  549. */
  550. int Vect_copy_tables(const struct Map_info *In, struct Map_info *Out,
  551. int field)
  552. {
  553. int i, n, ret, type;
  554. struct field_info *Fi, *Fin;
  555. dbDriver *driver;
  556. n = Vect_get_num_dblinks(In);
  557. G_debug(2, "Vect_copy_tables(): copying %d tables", n);
  558. type = GV_1TABLE;
  559. if (n > 1)
  560. type = GV_MTABLE;
  561. for (i = 0; i < n; i++) {
  562. Fi = Vect_get_dblink(In, i);
  563. if (Fi == NULL) {
  564. G_warning(_("Database connection not defined for layer %d"),
  565. In->dblnk->field[i].number);
  566. return -1;
  567. }
  568. if (field > 0 && Fi->number != field)
  569. continue;
  570. Fin = Vect_default_field_info(Out, Fi->number, Fi->name, type);
  571. G_debug(2, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  572. Fi->driver, Fi->database, Fi->table, Fin->driver,
  573. Fin->database, Fin->table);
  574. ret =
  575. Vect_map_add_dblink(Out, Fi->number, Fi->name, Fin->table,
  576. Fi->key, Fin->database, Fin->driver);
  577. if (ret == -1) {
  578. G_warning(_("Unable to add database link for vector map <%s>"),
  579. Out->name);
  580. return -1;
  581. }
  582. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  583. Fin->driver, Vect_subst_var(Fin->database, Out),
  584. Fin->table);
  585. if (ret == DB_FAILED) {
  586. G_warning(_("Unable to copy table <%s>"), Fin->table);
  587. return -1;
  588. }
  589. driver =
  590. db_start_driver_open_database(Fin->driver,
  591. Vect_subst_var(Fin->database, Out));
  592. if (driver == NULL) {
  593. G_warning(_("Unable to open database <%s> by driver <%s>"),
  594. Fin->database, Fin->driver);
  595. }
  596. else {
  597. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  598. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  599. Fin->table, Fin->key);
  600. db_close_database_shutdown_driver(driver);
  601. }
  602. }
  603. return 0;
  604. }
  605. /*!
  606. \brief Copy table linked to vector map based on type.
  607. \param In input vector map
  608. \param[out] Out output vector map
  609. \param field_in input layer number
  610. \param field_out output layer number
  611. \param field_name layer name
  612. \param type feature type
  613. \return 0 on success
  614. \return -1 on error
  615. */
  616. int
  617. Vect_copy_table(const struct Map_info *In, struct Map_info *Out, int field_in,
  618. int field_out, const char *field_name, int type)
  619. {
  620. return Vect_copy_table_by_cats(In, Out, field_in, field_out, field_name,
  621. type, NULL, 0);
  622. }
  623. /*!
  624. \brief Copy table linked to vector map based on category numbers.
  625. \param In input vector map
  626. \param[out] Out output vector map
  627. \param field_in input layer number
  628. \param field_out output layer number
  629. \param field_name layer name
  630. \param type feature type
  631. \param cats pointer to array of cats or NULL
  632. \param ncats number of cats in 'cats'
  633. \return 0 on success
  634. \return -1 on error
  635. */
  636. int
  637. Vect_copy_table_by_cats(const struct Map_info *In, struct Map_info *Out,
  638. int field_in, int field_out, const char *field_name,
  639. int type, int *cats, int ncats)
  640. {
  641. int ret;
  642. struct field_info *Fi, *Fin;
  643. const char *name, *key;
  644. G_debug(2, "Vect_copy_table(): field_in = %d field_out = %d", field_in,
  645. field_out);
  646. Fi = Vect_get_field(In, field_in);
  647. if (Fi == NULL) {
  648. G_warning(_("Database connection not defined for layer %d"),
  649. field_in);
  650. return -1;
  651. }
  652. if (field_name != NULL)
  653. name = field_name;
  654. else
  655. name = Fi->name;
  656. Fin = Vect_default_field_info(Out, field_out, name, type);
  657. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  658. Fi->driver, Fi->database, Fi->table, Fin->driver, Fin->database,
  659. Fin->table);
  660. ret =
  661. Vect_map_add_dblink(Out, Fin->number, Fin->name, Fin->table, Fi->key,
  662. Fin->database, Fin->driver);
  663. if (ret == -1) {
  664. G_warning(_("Unable to add database link for vector map <%s>"),
  665. Out->name);
  666. return -1;
  667. }
  668. if (cats)
  669. key = Fi->key;
  670. else
  671. key = NULL;
  672. ret = db_copy_table_by_ints(Fi->driver, Fi->database, Fi->table,
  673. Fin->driver, Vect_subst_var(Fin->database,
  674. Out), Fin->table,
  675. key, cats, ncats);
  676. if (ret == DB_FAILED) {
  677. G_warning(_("Unable to copy table <%s>"), Fin->table);
  678. return -1;
  679. }
  680. return 0;
  681. }
  682. /*!
  683. \brief Set spatial index to be realease when vector is closed.
  684. By default, the memory occupied by spatial index is not released.
  685. \param Map vector map
  686. \return
  687. */
  688. void Vect_set_release_support(struct Map_info *Map)
  689. {
  690. Map->plus.release_support = 1;
  691. }
  692. /*!
  693. \brief By default, category index is not updated if vector is
  694. changed, this function sets category index update.
  695. WARNING: currently only category for elements is updated
  696. not for areas
  697. \param Map vector map
  698. \return
  699. */
  700. void Vect_set_category_index_update(struct Map_info *Map)
  701. {
  702. Map->plus.update_cidx = 1;
  703. }