map.c 16 KB

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