map.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  1. /*!
  2. \file lib/vector/Vlib/map.c
  3. \brief Vector library - Manipulate vector map (copy, rename, delete)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009, 2012 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Original author CERL, probably Dave Gerdes or Mike Higgins.
  9. \author Update to GRASS 5.7 Radim Blazek and David D. Gray.
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <dirent.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <fcntl.h>
  19. #include <grass/glocale.h>
  20. #include <grass/vector.h>
  21. #include <grass/dbmi.h>
  22. #include <grass/glocale.h>
  23. /*!
  24. \brief Copy file
  25. \param src source file
  26. \param[out] dst destination file
  27. \return 0 OK
  28. \return 1 error
  29. */
  30. static int copy_file(const char *src, const char *dst)
  31. {
  32. char buf[1024];
  33. int fd, fd2;
  34. FILE *f2;
  35. int len, len2;
  36. if ((fd = open(src, O_RDONLY)) < 0)
  37. return 1;
  38. /* if((fd2 = open(dst, O_CREAT|O_TRUNC|O_WRONLY)) < 0) { */
  39. if ((f2 = fopen(dst, "w")) == NULL) {
  40. close(fd);
  41. return 1;
  42. }
  43. fd2 = fileno(f2);
  44. while ((len = read(fd, buf, 1024)) > 0) {
  45. while (len && (len2 = write(fd2, buf, len)) >= 0)
  46. len -= len2;
  47. }
  48. close(fd);
  49. /* close(fd2); */
  50. fclose(f2);
  51. if (len == -1 || len2 == -1)
  52. return 1;
  53. return 0;
  54. }
  55. /*!
  56. \brief Copy vector map including attribute tables
  57. Note: Output vector map is overwritten if exists!
  58. \param in name if vector map to be copied
  59. \param mapset mapset name where the input map is located
  60. \param out name for output vector map (new map is created in current mapset)
  61. \return -1 error
  62. \return 0 success
  63. */
  64. int Vect_copy(const char *in, const char *mapset, const char *out)
  65. {
  66. int i, n, ret, type;
  67. struct Map_info In, Out;
  68. struct field_info *Fi, *Fin;
  69. char old_path[GPATH_MAX], new_path[GPATH_MAX], buf[GPATH_MAX];
  70. const char *files[] = { GV_FRMT_ELEMENT, GV_COOR_ELEMENT,
  71. GV_HEAD_ELEMENT, GV_HIST_ELEMENT,
  72. GV_TOPO_ELEMENT, GV_SIDX_ELEMENT, GV_CIDX_ELEMENT,
  73. NULL
  74. };
  75. const char *inmapset;
  76. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  77. dbDriver *driver;
  78. G_debug(2, "Copy vector '%s' in '%s' to '%s'", in, mapset, out);
  79. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  80. if (Vect_legal_filename(out) < 0)
  81. G_fatal_error(_("Vector map name is not SQL compliant"));
  82. inmapset = G_find_vector2(in, mapset);
  83. if (!inmapset) {
  84. G_warning(_("Unable to find vector map <%s> in <%s>"), in, mapset);
  85. return -1;
  86. }
  87. mapset = inmapset;
  88. /* remove mapset from fully qualified name, confuses G_file_name() */
  89. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  90. in = xname;
  91. }
  92. /* Delete old vector if it exists */
  93. if (G_find_vector2(out, G_mapset())) {
  94. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  95. out);
  96. ret = Vect_delete(out);
  97. if (ret != 0) {
  98. G_warning(_("Unable to delete vector map <%s>"), out);
  99. return -1;
  100. }
  101. }
  102. /* Copy the directory */
  103. G__make_mapset_element(GV_DIRECTORY);
  104. sprintf(buf, "%s/%s", GV_DIRECTORY, out);
  105. G__make_mapset_element(buf);
  106. i = 0;
  107. while (files[i]) {
  108. sprintf(buf, "%s/%s", in, files[i]);
  109. G_file_name(old_path, GV_DIRECTORY, buf, mapset);
  110. sprintf(buf, "%s/%s", out, files[i]);
  111. G_file_name(new_path, GV_DIRECTORY, buf, G_mapset());
  112. if (access(old_path, F_OK) == 0) { /* file exists? */
  113. G_debug(2, "copy %s to %s", old_path, new_path);
  114. if (copy_file(old_path, new_path)) {
  115. G_warning(_("Unable to copy vector map <%s> to <%s>"),
  116. old_path, new_path);
  117. }
  118. }
  119. i++;
  120. }
  121. G_file_name(old_path, GV_DIRECTORY, in, mapset);
  122. G_file_name(new_path, GV_DIRECTORY, out, G_mapset());
  123. /* Open input */
  124. Vect_set_open_level(1);
  125. Vect_open_old_head(&In, in, mapset);
  126. if (In.format != GV_FORMAT_NATIVE) { /* Done */
  127. Vect_close(&In);
  128. return 0;
  129. }
  130. /* Open output */
  131. Vect_set_open_level(1);
  132. Vect_open_update_head(&Out, out, G_mapset());
  133. /* Copy tables */
  134. n = Vect_get_num_dblinks(&In);
  135. type = GV_1TABLE;
  136. if (n > 1)
  137. type = GV_MTABLE;
  138. for (i = 0; i < n; i++) {
  139. Fi = Vect_get_dblink(&In, i);
  140. if (Fi == NULL) {
  141. G_warning(_("Database connection not defined for layer %d"),
  142. In.dblnk->field[i].number);
  143. Vect_close(&In);
  144. Vect_close(&Out);
  145. return -1;
  146. }
  147. Fin = Vect_default_field_info(&Out, Fi->number, Fi->name, type);
  148. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  149. Fi->driver, Fi->database, Fi->table, Fin->driver,
  150. Fin->database, Fin->table);
  151. Vect_map_add_dblink(&Out, Fi->number, Fi->name, Fin->table, Fi->key,
  152. Fin->database, Fin->driver);
  153. ret = db_copy_table(Fi->driver, Fi->database, Fi->table,
  154. Fin->driver, Vect_subst_var(Fin->database, &Out),
  155. Fin->table);
  156. if (ret == DB_FAILED) {
  157. G_warning(_("Unable to copy table <%s>"), Fin->table);
  158. Vect_close(&In);
  159. Vect_close(&Out);
  160. return -1;
  161. }
  162. driver =
  163. db_start_driver_open_database(Fin->driver,
  164. Vect_subst_var(Fin->database,
  165. &Out));
  166. if (driver == NULL) {
  167. G_warning(_("Unable to open database <%s> by driver <%s>"),
  168. Fin->database, Fin->driver);
  169. }
  170. else {
  171. if (db_create_index2(driver, Fin->table, Fi->key) != DB_OK)
  172. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  173. Fi->table, Fi->key);
  174. db_close_database_shutdown_driver(driver);
  175. }
  176. }
  177. Vect_close(&In);
  178. Vect_close(&Out);
  179. return 0;
  180. }
  181. /*!
  182. \brief Rename existing vector map (in the current mapset).
  183. Attribute tables are created in the same database where input tables were stored.
  184. The origial format (native/OGR) is used.
  185. Note: Output vector map is overwritten if exists!
  186. \param in name of vector map to be renamed
  187. \param out name for output vector map
  188. \return -1 error
  189. \return 0 success
  190. */
  191. int Vect_rename(const char *in, const char *out)
  192. {
  193. int i, n, ret, type;
  194. struct Map_info Map;
  195. struct field_info *Fin, *Fout;
  196. int *fields;
  197. dbDriver *driver;
  198. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  199. G_debug(2, "Rename vector '%s' to '%s'", in, out);
  200. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  201. if (Vect_legal_filename(out) < 0)
  202. G_fatal_error(_("Vector map name is not SQL compliant"));
  203. /* Delete old vector if it exists */
  204. if (G_find_vector2(out, G_mapset())) {
  205. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  206. out);
  207. Vect_delete(out);
  208. }
  209. /* remove mapset from fully qualified name */
  210. if (G_name_is_fully_qualified(in, xname, xmapset)) {
  211. in = xname;
  212. }
  213. /* Move the directory */
  214. ret = G_rename(GV_DIRECTORY, in, out);
  215. if (ret == 0) {
  216. G_warning(_("Vector map <%s> not found"), in);
  217. return -1;
  218. }
  219. else if (ret == -1) {
  220. G_warning(_("Unable to copy vector map <%s> to <%s>"), in, out);
  221. return -1;
  222. }
  223. /* Rename all tables if the format is native */
  224. Vect_set_open_level(1);
  225. Vect_open_update_head(&Map, out, G_mapset());
  226. if (Map.format != GV_FORMAT_NATIVE) { /* Done */
  227. Vect_close(&Map);
  228. return 0;
  229. }
  230. /* Copy tables */
  231. n = Vect_get_num_dblinks(&Map);
  232. type = GV_1TABLE;
  233. if (n > 1)
  234. type = GV_MTABLE;
  235. /* Make the list of fields */
  236. fields = (int *)G_malloc(n * sizeof(int));
  237. for (i = 0; i < n; i++) {
  238. Fin = Vect_get_dblink(&Map, i);
  239. fields[i] = Fin->number;
  240. }
  241. for (i = 0; i < n; i++) {
  242. G_debug(3, "field[%d] = %d", i, fields[i]);
  243. Fin = Vect_get_field(&Map, fields[i]);
  244. if (Fin == NULL) {
  245. G_warning(_("Database connection not defined for layer %d"),
  246. fields[i]);
  247. Vect_close(&Map);
  248. return -1;
  249. }
  250. Fout = Vect_default_field_info(&Map, Fin->number, Fin->name, type);
  251. G_debug(3, "Copy drv:db:table '%s:%s:%s' to '%s:%s:%s'",
  252. Fin->driver, Fin->database, Fin->table, Fout->driver,
  253. Fout->database, Fout->table);
  254. /* TODO: db_rename_table instead of db_copy_table */
  255. ret = db_copy_table(Fin->driver, Fin->database, Fin->table,
  256. Fout->driver, Vect_subst_var(Fout->database,
  257. &Map), Fout->table);
  258. if (ret == DB_FAILED) {
  259. G_warning(_("Unable to copy table <%s>"), Fin->table);
  260. Vect_close(&Map);
  261. return -1;
  262. }
  263. /* Change the link */
  264. Vect_map_del_dblink(&Map, Fin->number);
  265. Vect_map_add_dblink(&Map, Fout->number, Fout->name, Fout->table,
  266. Fin->key, Fout->database, Fout->driver);
  267. /* Delete old table */
  268. ret = db_delete_table(Fin->driver, Fin->database, Fin->table);
  269. if (ret == DB_FAILED) {
  270. G_warning(_("Unable to delete table <%s>"), Fin->table);
  271. Vect_close(&Map);
  272. return -1;
  273. }
  274. driver =
  275. db_start_driver_open_database(Fout->driver,
  276. Vect_subst_var(Fout->database,
  277. &Map));
  278. if (driver == NULL) {
  279. G_warning(_("Unable to open database <%s> by driver <%s>"),
  280. Fout->database, Fout->driver);
  281. }
  282. else {
  283. if (db_create_index2(driver, Fout->table, Fin->key) != DB_OK)
  284. G_warning(_("Unable to create index for table <%s>, key <%s>"),
  285. Fout->table, Fout->key);
  286. db_close_database_shutdown_driver(driver);
  287. }
  288. }
  289. Vect_close(&Map);
  290. free(fields);
  291. return 0;
  292. }
  293. /*!
  294. \brief Delete vector map including attribute tables
  295. Vector map must be located in current mapset.
  296. \param map name of vector map to be delete
  297. \return -1 error
  298. \return 0 success
  299. */
  300. int Vect_delete(const char *map)
  301. {
  302. int i, n, ret;
  303. struct Map_info Map;
  304. struct field_info *Fi;
  305. char buf[GPATH_MAX];
  306. DIR *dir;
  307. struct dirent *ent;
  308. const char *tmp;
  309. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  310. G_debug(3, "Delete vector '%s'", map);
  311. /* remove mapset from fully qualified name */
  312. if (G_name_is_fully_qualified(map, xname, xmapset)) {
  313. map = xname;
  314. }
  315. if (map == NULL || strlen(map) == 0) {
  316. G_warning(_("Invalid vector map name <%s>"), map ? map : "null");
  317. return -1;
  318. }
  319. sprintf(buf, "%s/%s/%s/%s/%s/%s", G_gisdbase(), G_location(),
  320. G_mapset(), GV_DIRECTORY, map, GV_DBLN_ELEMENT);
  321. G_debug(1, "dbln file: %s", buf);
  322. if (access(buf, F_OK) == 0) {
  323. /* Open input */
  324. Vect_set_open_level(1); /* Topo not needed */
  325. ret = Vect_open_old_head(&Map, map, G_mapset());
  326. if (ret < 1) {
  327. G_warning(_("Unable to open header file for vector map <%s>"),
  328. map);
  329. return -1;
  330. }
  331. /* Delete all tables, NOT external (OGR) */
  332. if (Map.format == GV_FORMAT_NATIVE) {
  333. n = Vect_get_num_dblinks(&Map);
  334. for (i = 0; i < n; i++) {
  335. Fi = Vect_get_dblink(&Map, i);
  336. if (Fi == NULL) {
  337. G_warning(_("Database connection not defined for layer %d"),
  338. Map.dblnk->field[i].number);
  339. Vect_close(&Map);
  340. return -1;
  341. }
  342. G_debug(3, "Delete drv:db:table '%s:%s:%s'", Fi->driver,
  343. Fi->database, Fi->table);
  344. ret = db_table_exists(Fi->driver, Fi->database, Fi->table);
  345. if (ret == -1) {
  346. G_warning(_("Unable to find table <%s> linked to vector map <%s>"),
  347. Fi->table, map);
  348. Vect_close(&Map);
  349. return -1;
  350. }
  351. if (ret == 1) {
  352. ret =
  353. db_delete_table(Fi->driver, Fi->database, Fi->table);
  354. if (ret == DB_FAILED) {
  355. G_warning(_("Unable to delete table <%s>"),
  356. Fi->table);
  357. Vect_close(&Map);
  358. return -1;
  359. }
  360. }
  361. else {
  362. G_warning(_("Table <%s> linked to vector map <%s> does not exist"),
  363. Fi->table, map);
  364. }
  365. }
  366. }
  367. Vect_close(&Map);
  368. }
  369. /* Delete all files from vector/name directory */
  370. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  371. G_debug(3, "opendir '%s'", buf);
  372. dir = opendir(buf);
  373. if (dir == NULL) {
  374. G_warning(_("Unable to open directory '%s'"), buf);
  375. return -1;
  376. }
  377. while ((ent = readdir(dir))) {
  378. G_debug(3, "file = '%s'", ent->d_name);
  379. if ((strcmp(ent->d_name, ".") == 0) ||
  380. (strcmp(ent->d_name, "..") == 0))
  381. continue;
  382. sprintf(buf, "%s/%s/vector/%s/%s", G_location_path(), G_mapset(), map,
  383. ent->d_name);
  384. G_debug(3, "delete file '%s'", buf);
  385. ret = unlink(buf);
  386. if (ret == -1) {
  387. G_warning(_("Unable to delete file '%s'"), buf);
  388. closedir(dir);
  389. return -1;
  390. }
  391. }
  392. closedir(dir);
  393. /* NFS can create .nfsxxxxxxxx files for those deleted
  394. * -> we have to move the directory to ./tmp before it is deleted */
  395. sprintf(buf, "%s/%s/vector/%s", G_location_path(), G_mapset(), map);
  396. tmp = G_tempfile();
  397. G_debug(3, "rename '%s' to '%s'", buf, tmp);
  398. ret = rename(buf, tmp);
  399. if (ret == -1) {
  400. G_warning(_("Unable to rename directory '%s' to '%s'"), buf, tmp);
  401. return -1;
  402. }
  403. G_debug(3, "remove directory '%s'", tmp);
  404. /* Warning: remove() fails on Windows */
  405. ret = rmdir(tmp);
  406. if (ret == -1) {
  407. G_warning(_("Unable to remove directory '%s'"), tmp);
  408. return -1;
  409. }
  410. return 0;
  411. }
  412. /*!
  413. \brief Set spatial index to be realease when vector is closed.
  414. By default, the memory occupied by spatial index is not released.
  415. \param Map vector map
  416. */
  417. void Vect_set_release_support(struct Map_info *Map)
  418. {
  419. Map->plus.release_support = TRUE;
  420. }
  421. /*!
  422. \brief Set category index to be updated when vector is changed.
  423. By default, category index is not updated if vector is changed,
  424. this function sets category index update.
  425. WARNING: currently only category for elements is updated not for
  426. areas
  427. \param Map vector map
  428. */
  429. void Vect_set_category_index_update(struct Map_info *Map)
  430. {
  431. Map->plus.update_cidx = TRUE;
  432. }