open.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /*!
  2. * \file vector/Vlib/open.c
  3. *
  4. * \brief Vector library - Open vector map (native or OGR format)
  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 <string.h>
  21. #include <unistd.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <grass/gis.h>
  25. #include <grass/vector.h>
  26. #include <grass/glocale.h>
  27. /*
  28. \brief Number of available levels
  29. */
  30. #define MAX_OPEN_LEVEL 2
  31. static int open_old_dummy()
  32. {
  33. return 0;
  34. }
  35. #ifndef HAVE_OGR
  36. static int format()
  37. {
  38. G_fatal_error(_("Requested format is not compiled in this version"));
  39. return 0;
  40. }
  41. #endif
  42. static int Open_level = 0;
  43. static int (*Open_old_array[][2]) () = {
  44. {
  45. open_old_dummy, V1_open_old_nat}
  46. #ifdef HAVE_OGR
  47. , {
  48. open_old_dummy, V1_open_old_ogr}
  49. #else
  50. , {
  51. open_old_dummy, format}
  52. #endif
  53. };
  54. static void fatal_error(int ferror, char *errmsg)
  55. {
  56. switch (ferror) {
  57. case GV_FATAL_EXIT:
  58. G_fatal_error(errmsg);
  59. break;
  60. case GV_FATAL_PRINT:
  61. G_warning(errmsg);
  62. break;
  63. case GV_FATAL_RETURN:
  64. break;
  65. }
  66. }
  67. /*!
  68. * \brief Predetermine level at which a vector map will be opened for
  69. * reading.
  70. *
  71. * If it can't open that level, the open will fail. The specified
  72. * level must be set before any call to open. The default is to try to
  73. * open the highest level possible, and keep stepping down until
  74. * success.
  75. *
  76. * NOTE: This should only be used to set when you wish to force a
  77. * lower level open. If you require a higher level, then just check
  78. * the return to verify the level instead of forcing it. This is
  79. * because future releases will have higher levels which will be
  80. * downward compatible and which your programs should support by
  81. * default.
  82. *
  83. * \param level vector access level
  84. *
  85. * \return 0 on success
  86. * \return 1 on error
  87. */
  88. int Vect_set_open_level(int level)
  89. {
  90. Open_level = level;
  91. if (Open_level < 1 || Open_level > MAX_OPEN_LEVEL) {
  92. G_warning(_("Programmer requested unknown access level %d"),
  93. Open_level);
  94. Open_level = 0;
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. /*!
  100. * \brief Open existing vector map for reading (internal use only)
  101. *
  102. * In case of error, the functions respect fatal error settings.
  103. *
  104. * \param[out] Map pointer to Map_info structure
  105. * \param name name of vector map to open
  106. * \param mapset mapset name ("" for search path)
  107. * \param update non-zero to open for update otherwise read-only mode
  108. * \param head_only read only header info from 'head', 'dbln', 'topo',
  109. * 'cidx' is not opened. The header may be opened on level 2 only.
  110. *
  111. * \return level of openness (1, 2)
  112. * \return -1 in error
  113. */
  114. int Vect__open_old(struct Map_info *Map, const char *name, const char *mapset, const char *layer,
  115. int update, int head_only)
  116. {
  117. char buf[GNAME_MAX + 10], buf2[GMAPSET_MAX + 10], xname[GNAME_MAX],
  118. xmapset[GMAPSET_MAX], errmsg[2000];
  119. FILE *fp;
  120. int level, level_request, ferror;
  121. int format, ret;
  122. int ogr_mapset;
  123. const char *fmapset;
  124. G_debug(1, "Vect__open_old(): name = %s mapset= %s update = %d", name,
  125. mapset, update);
  126. /* zero Map_info structure */
  127. G_zero(Map, sizeof(struct Map_info));
  128. /* TODO: Open header for update ('dbln') */
  129. ferror = Vect_get_fatal_error();
  130. Vect_set_fatal_error(GV_FATAL_EXIT);
  131. level_request = Open_level;
  132. Open_level = 0;
  133. /* initialize Map->head */
  134. Vect__init_head(Map);
  135. /* initialize support structures for 2D, update to 3D when reading support files */
  136. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = 0;
  137. /* initialize Map->plus */
  138. dig_init_plus(&(Map->plus));
  139. /* check OGR mapset */
  140. ogr_mapset = 0;
  141. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  142. if (strcasecmp(xmapset, "ogr") == 0) {
  143. /* unique OGR mapset detected */
  144. G_debug(1, "OGR mapset detected");
  145. ogr_mapset = 1;
  146. Map->fInfo.ogr.dsn = G_store(xname);
  147. if (layer) {
  148. Map->fInfo.ogr.layer_name = G_store(layer); /* no layer to be open */
  149. }
  150. }
  151. else {
  152. sprintf(buf, "%s/%s", GV_DIRECTORY, xname);
  153. sprintf(buf2, "%s@%s", GV_COOR_ELEMENT, xmapset);
  154. }
  155. Map->name = G_store(xname);
  156. Map->mapset = G_store(xmapset);
  157. }
  158. else {
  159. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  160. sprintf(buf2, "%s", GV_COOR_ELEMENT);
  161. Map->name = G_store(name);
  162. if (mapset)
  163. Map->mapset = G_store(mapset);
  164. else
  165. Map->mapset = G_store("");
  166. }
  167. if (!ogr_mapset) {
  168. /* try to find vector map (not for OGR mapset) */
  169. fmapset = G_find_vector2(Map->name, Map->mapset);
  170. if (fmapset == NULL) {
  171. sprintf(errmsg, _("Vector map <%s> not found"),
  172. Vect_get_full_name(Map));
  173. fatal_error(ferror, errmsg);
  174. return -1;
  175. }
  176. Map->mapset = G_store(fmapset);
  177. }
  178. else { /* OGR mapset */
  179. if (update) {
  180. sprintf(errmsg, _("OGR layer cannot be opened for update"));
  181. fatal_error(ferror, errmsg);
  182. return -1;
  183. }
  184. }
  185. Map->location = G_store(G_location());
  186. Map->gisdbase = G_store(G_gisdbase());
  187. if (update && (0 != strcmp(Map->mapset, G_mapset()))) {
  188. G_warning(_("Vector map which is not in the current mapset cannot be opened for update"));
  189. return -1;
  190. }
  191. G_debug(1, "Map name: %s", Map->name);
  192. G_debug(1, "Map mapset: %s", Map->mapset);
  193. /* Read vector format information */
  194. if (ogr_mapset) {
  195. format = GV_FORMAT_OGR;
  196. }
  197. else {
  198. format = 0;
  199. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  200. G_debug(1, "open format file: '%s/%s/%s'", Map->mapset, buf,
  201. GV_FRMT_ELEMENT);
  202. fp = G_fopen_old(buf, GV_FRMT_ELEMENT, Map->mapset);
  203. if (fp == NULL) {
  204. G_debug(1, "Vector format: %d (native)", format);
  205. format = GV_FORMAT_NATIVE;
  206. }
  207. else {
  208. format = dig_read_frmt_ascii(fp, &(Map->fInfo));
  209. fclose(fp);
  210. G_debug(1, "Vector format: %d (non-native)", format);
  211. if (format < 0) {
  212. sprintf(errmsg, _("Unable to open vector map <%s>"),
  213. Vect_get_full_name(Map));
  214. fatal_error(ferror, errmsg);
  215. return -1;
  216. }
  217. }
  218. }
  219. Map->format = format;
  220. /* projection info */
  221. Vect_set_proj(Map, G_projection());
  222. Vect_set_zone(Map, G_zone());
  223. /* read vector head (ignored for OGR mapset) */
  224. if (!ogr_mapset && Vect__read_head(Map) != 0) {
  225. sprintf(errmsg,
  226. _("Unable to open vector map <%s> on level %d. "
  227. "Try to rebuild vector topology by v.build."),
  228. Vect_get_full_name(Map), level_request);
  229. G_warning(_("Unable to read header file of vector map <%s>"),
  230. Vect_get_full_name(Map));
  231. }
  232. /* zone not set */
  233. if (Vect_get_zone(Map) == -1)
  234. Vect_set_zone(Map, G_zone());
  235. G_debug(1, "Level request = %d", level_request);
  236. /* There are only 2 possible open levels, 1 and 2. Try first to
  237. * open 'support' files (topo,sidx,cidx), these files are the same
  238. * for all formats. If it is not possible and requested level is
  239. * 2, return error, otherwise call Open_old_array[format][1], to
  240. * open remaining files/sources (level 1)
  241. */
  242. /* Try to open support files if level was not requested or
  243. * requested level is 2 (format independent) */
  244. if (level_request == 0 || level_request == 2) {
  245. level = 2; /* we expect success */
  246. /* open topo */
  247. ret = Vect_open_topo(Map, head_only);
  248. if (ret == 1) { /* topo file is not available */
  249. G_debug(1, "topo file for vector '%s' not available.",
  250. Vect_get_full_name(Map));
  251. level = 1;
  252. }
  253. else if (ret == -1) {
  254. G_fatal_error(_("Unable to open topology file for vector map <%s>"),
  255. Vect_get_full_name(Map));
  256. }
  257. /* open spatial index */
  258. if (level == 2) {
  259. ret = Vect_open_sidx(Map, (update != 0));
  260. if (ret == 1) { /* sidx file is not available */
  261. G_debug(1, "sidx file for vector '%s' not available.",
  262. Vect_get_full_name(Map));
  263. level = 1;
  264. }
  265. else if (ret == -1) {
  266. G_fatal_error(_("Unable to open spatial index file for vector map <%s>"),
  267. Vect_get_full_name(Map));
  268. }
  269. /* check with_z consistency */
  270. if ((Map->plus.with_z != 0 && Map->plus.spidx_with_z == 0) ||
  271. (Map->plus.with_z == 0 && Map->plus.spidx_with_z != 0)) {
  272. G_warning("Vector map <%s>: topology is %s, but spatial index is %s",
  273. Vect_get_full_name(Map), (Map->plus.with_z != 0 ? "3D" : "2D"),
  274. (Map->plus.spidx_with_z != 0 ? "3D" : "2D"));
  275. level = 1;
  276. }
  277. }
  278. /* open category index */
  279. if (level == 2) {
  280. ret = Vect_cidx_open(Map, head_only);
  281. if (ret == 1) { /* category index is not available */
  282. G_debug(1,
  283. "cidx file for vector '%s' not available.",
  284. Vect_get_full_name(Map));
  285. dig_free_plus(&(Map->plus)); /* free topology */
  286. dig_spidx_free(&(Map->plus)); /* free spatial index */
  287. level = 1;
  288. }
  289. else if (ret == -1) { /* file exists, but cannot be opened */
  290. G_fatal_error(_("Unable to open category index file for vector map <%s>"),
  291. Vect_get_full_name(Map));
  292. }
  293. }
  294. #ifdef HAVE_OGR
  295. /* Open OGR specific support files */
  296. if (level == 2 && Map->format == GV_FORMAT_OGR) {
  297. if (V2_open_old_ogr(Map) < 0) {
  298. dig_free_plus(&(Map->plus));
  299. dig_spidx_free(&(Map->plus));
  300. dig_cidx_free(&(Map->plus));
  301. level = 1;
  302. }
  303. }
  304. #endif
  305. if (level_request == 2 && level < 2) {
  306. if (!ogr_mapset) {
  307. /* for direct OGR read access is built pseudo-topology on the fly */
  308. sprintf(errmsg,
  309. _("Unable to open vector map <%s> on level %d. "
  310. "Try to rebuild vector topology by v.build."),
  311. Vect_get_full_name(Map), level_request);
  312. fatal_error(ferror, errmsg);
  313. return -1;
  314. }
  315. }
  316. }
  317. else {
  318. level = 1; /* i.e. requested level is 1 */
  319. }
  320. /* open level 1 files / sources (format specific) */
  321. if (!head_only || ogr_mapset) { /* no need to open coordinates */
  322. if (0 != (*Open_old_array[format][1]) (Map, update)) { /* cannot open */
  323. if (level == 2) { /* support files opened */
  324. dig_free_plus(&(Map->plus));
  325. dig_spidx_free(&(Map->plus));
  326. dig_cidx_free(&(Map->plus));
  327. }
  328. sprintf(errmsg,
  329. _("Unable to open vector map <%s> on level %d. "
  330. "Try to rebuild vector topology by v.build."),
  331. Vect_get_full_name(Map), level_request);
  332. fatal_error(ferror, errmsg);
  333. return -1;
  334. }
  335. if (ogr_mapset && !head_only && level_request != 1) {
  336. /* build pseudo-topology on the fly */
  337. int verbose;
  338. verbose = G_verbose();
  339. G_message(_("Building topology for OGR layer <%s> from datasource '%s'..."),
  340. Map->fInfo.ogr.layer_name, Map->fInfo.ogr.dsn);
  341. G_set_verbose(0);
  342. Vect_build(Map);
  343. level = 2;
  344. G_set_verbose(verbose);
  345. }
  346. }
  347. else {
  348. Map->head.with_z = Map->plus.with_z; /* take dimension from topo */
  349. }
  350. /* set status */
  351. Map->open = VECT_OPEN_CODE;
  352. Map->level = level;
  353. Map->head_only = head_only;
  354. Map->support_updated = 0;
  355. if (update) {
  356. Map->mode = GV_MODE_RW;
  357. Map->plus.mode = GV_MODE_RW;
  358. }
  359. else {
  360. Map->mode = GV_MODE_READ;
  361. Map->plus.mode = GV_MODE_READ;
  362. }
  363. if (head_only) {
  364. Map->head_only = 1;
  365. }
  366. else {
  367. Map->head_only = 0;
  368. }
  369. Map->Constraint_region_flag = 0;
  370. Map->Constraint_type_flag = 0;
  371. G_debug(1, "Vect_open_old(): vector opened on level %d", level);
  372. if (level == 1) { /* without topology */
  373. Map->plus.built = GV_BUILD_NONE;
  374. }
  375. else { /* level 2, with topology */
  376. Map->plus.built = GV_BUILD_ALL; /* highest level of topology for level 2 */
  377. }
  378. Map->plus.do_uplist = 0;
  379. /* read db links */
  380. Map->dblnk = Vect_new_dblinks_struct();
  381. Vect_read_dblinks(Map);
  382. /* open history file */
  383. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  384. if (update) { /* native only */
  385. Map->hist_fp = G_fopen_modify(buf, GV_HIST_ELEMENT);
  386. if (Map->hist_fp == NULL) {
  387. sprintf(errmsg,
  388. _("Unable to open history file for vector map <%s>"),
  389. Vect_get_full_name(Map));
  390. fatal_error(ferror, errmsg);
  391. return (-1);
  392. }
  393. G_fseek(Map->hist_fp, (off_t) 0, SEEK_END);
  394. Vect_hist_write(Map,
  395. "---------------------------------------------------------------------------------\n");
  396. }
  397. else {
  398. if (Map->format == GV_FORMAT_NATIVE || Map->format == GV_FORMAT_OGR) {
  399. Map->hist_fp =
  400. G_fopen_old(buf, GV_HIST_ELEMENT, Map->mapset);
  401. /* If NULL (does not exist) then Vect_hist_read() handle that */
  402. }
  403. else {
  404. Map->hist_fp = NULL;
  405. }
  406. }
  407. if (!head_only) { /* cannot rewind if not fully opened */
  408. Vect_rewind(Map);
  409. }
  410. /* delete support files if native format was opened for update (not head_only) */
  411. if (update && !head_only) {
  412. char file_path[2000];
  413. struct stat info;
  414. sprintf(buf, "%s/%s", GV_DIRECTORY, name);
  415. G__file_name(file_path, buf, GV_TOPO_ELEMENT, G_mapset());
  416. if (stat(file_path, &info) == 0) /* file exists? */
  417. unlink(file_path);
  418. G__file_name(file_path, buf, GV_SIDX_ELEMENT, G_mapset());
  419. if (stat(file_path, &info) == 0) /* file exists? */
  420. unlink(file_path);
  421. G__file_name(file_path, buf, GV_CIDX_ELEMENT, G_mapset());
  422. if (stat(file_path, &info) == 0) /* file exists? */
  423. unlink(file_path);
  424. }
  425. return (level);
  426. }
  427. /*!
  428. * \brief Open existing vector for reading
  429. *
  430. * In case of error, the functions respect fatal error settings.
  431. *
  432. * \param[out] Map pointer to Map_info structure
  433. * \param name name of vector map to open
  434. * \param mapset mapset name
  435. *
  436. * \return level of openness [1, 2, (3)]
  437. * \return -1 on error
  438. */
  439. int Vect_open_old(struct Map_info *Map, const char *name, const char *mapset)
  440. {
  441. return (Vect__open_old(Map, name, mapset, NULL, 0, 0));
  442. }
  443. int Vect_open_old2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  444. {
  445. return (Vect__open_old(Map, name, mapset, layer, 0, 0));
  446. }
  447. /*!
  448. * \brief Open existing vector for reading/writing
  449. *
  450. * In case of error, the functions respect fatal error settings.
  451. *
  452. * \param[out] Map pointer to Map_info structure
  453. * \param name name of vector map to update
  454. * \param mapset mapset name
  455. *
  456. * \return level of openness [1, 2, (3)]
  457. * \return -1 on error
  458. */
  459. int Vect_open_update(struct Map_info *Map, const char *name, const char *mapset)
  460. {
  461. int ret;
  462. ret = Vect__open_old(Map, name, mapset, NULL, 1, 0);
  463. if (ret > 0) {
  464. Map->plus.do_uplist = 1;
  465. Map->plus.uplines = NULL;
  466. Map->plus.n_uplines = 0;
  467. Map->plus.alloc_uplines = 0;
  468. Map->plus.upnodes = NULL;
  469. Map->plus.n_upnodes = 0;
  470. Map->plus.alloc_upnodes = 0;
  471. /* read spatial index */
  472. /* Build spatial index from topo */
  473. /* Vect_build_sidx_from_topo(Map); */
  474. }
  475. return ret;
  476. }
  477. /*!
  478. * \brief Reads only info about vector map from headers of 'head',
  479. * 'dbln', 'topo' and 'cidx' file.
  480. *
  481. * In case of error, the functions respect fatal error settings.
  482. *
  483. * \param[out] Map pointer to Map_info structure
  484. * \param name name of vector map to read (dsn for OGR)
  485. * \param mapset mapset name ("" for search path)
  486. * \param layer layer name (only for OGR)
  487. *
  488. * \return level of openness [1, 2, (3)]
  489. * \return -1 on error
  490. */
  491. int Vect_open_old_head(struct Map_info *Map, const char *name, const char *mapset)
  492. {
  493. return (Vect__open_old(Map, name, mapset, NULL, 0, 1));
  494. }
  495. int Vect_open_old_head2(struct Map_info *Map, const char *name, const char *mapset, const char *layer)
  496. {
  497. return (Vect__open_old(Map, name, mapset, layer, 0, 1));
  498. }
  499. /*!
  500. * \brief Open header file of existing vector map for updating (mostly
  501. * for database link updates)
  502. *
  503. * In case of error, the functions respect fatal error settings.
  504. *
  505. * \param[out] Map pointer to Map_info structure
  506. * \param name name of vector map to update
  507. * \param mapset mapset name
  508. *
  509. * \return level of openness [1, 2, (3)]
  510. * \return -1 on error
  511. */
  512. int Vect_open_update_head(struct Map_info *Map, const char *name,
  513. const char *mapset)
  514. {
  515. int ret;
  516. ret = Vect__open_old(Map, name, mapset, NULL, 1, 1);
  517. if (ret > 0) { /* Probably not important */
  518. Map->plus.do_uplist = 1;
  519. Map->plus.uplines = NULL;
  520. Map->plus.n_uplines = 0;
  521. Map->plus.alloc_uplines = 0;
  522. Map->plus.upnodes = NULL;
  523. Map->plus.n_upnodes = 0;
  524. Map->plus.alloc_upnodes = 0;
  525. }
  526. return ret;
  527. }
  528. /*!
  529. * \brief Create new vector map for reading/writing
  530. *
  531. * \param[in,out] Map pointer to Map_info structure
  532. * \param name name of vector map
  533. * \param with_z non-zero value for 3D vector data
  534. *
  535. * \return 1 on success
  536. * \return -1 on error
  537. */
  538. int Vect_open_new(struct Map_info *Map, const char *name, int with_z)
  539. {
  540. int ret, ferror;
  541. char errmsg[2000], buf[500];
  542. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  543. G_debug(2, "Vect_open_new(): name = %s", name);
  544. /* init header */
  545. Vect__init_head(Map);
  546. /* error handling */
  547. ferror = Vect_get_fatal_error();
  548. Vect_set_fatal_error(GV_FATAL_EXIT);
  549. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  550. if (strcmp(xmapset, G_mapset()) != 0) {
  551. sprintf(errmsg, _("%s is not in the current mapset (%s)"), name,
  552. G_mapset());
  553. fatal_error(ferror, errmsg);
  554. }
  555. name = xname;
  556. }
  557. /* check for [A-Za-z][A-Za-z0-9_]* in name */
  558. if (Vect_legal_filename(name) < 0) {
  559. sprintf(errmsg, _("Vector map name is not SQL compliant"));
  560. fatal_error(ferror, errmsg);
  561. return (-1);
  562. }
  563. /* Check if map already exists */
  564. if (G_find_vector2(name, G_mapset()) != NULL) {
  565. G_warning(_("Vector map <%s> already exists and will be overwritten"),
  566. name);
  567. ret = Vect_delete(name);
  568. if (ret == -1) {
  569. sprintf(errmsg, _("Unable to delete vector map <%s>"), name);
  570. fatal_error(ferror, errmsg);
  571. return (-1);
  572. }
  573. }
  574. Map->name = G_store(name);
  575. Map->mapset = G_store(G_mapset());
  576. Map->location = G_store(G_location());
  577. Map->gisdbase = G_store(G_gisdbase());
  578. Map->format = GV_FORMAT_NATIVE;
  579. /* set 2D/3D */
  580. Map->plus.spidx_with_z = Map->plus.with_z = Map->head.with_z = (with_z != 0);
  581. if (V1_open_new_nat(Map, name, with_z) < 0) {
  582. sprintf(errmsg, _("Unable to create vector map <%s>"),
  583. Vect_get_full_name(Map));
  584. fatal_error(ferror, errmsg);
  585. return (-1);
  586. }
  587. /* Open history file */
  588. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  589. Map->hist_fp = G_fopen_new(buf, GV_HIST_ELEMENT);
  590. if (Map->hist_fp == NULL) {
  591. sprintf(errmsg, _("Unable to open history file of vector map <%s>"),
  592. Vect_get_full_name(Map));
  593. fatal_error(ferror, errmsg);
  594. return (-1);
  595. }
  596. Open_level = 0;
  597. /* initialize topo */
  598. dig_init_plus(&(Map->plus));
  599. /* open new spatial index */
  600. Vect_open_sidx(Map, 2);
  601. Map->open = VECT_OPEN_CODE;
  602. Map->level = 1;
  603. Map->head_only = 0;
  604. Map->support_updated = 0;
  605. Map->plus.built = GV_BUILD_NONE;
  606. Map->mode = GV_MODE_RW;
  607. Map->Constraint_region_flag = 0;
  608. Map->Constraint_type_flag = 0;
  609. Map->plus.do_uplist = 0;
  610. Vect_set_proj(Map, G_projection());
  611. Vect_set_zone(Map, G_zone());
  612. Map->dblnk = Vect_new_dblinks_struct();
  613. return 1;
  614. }
  615. /*!
  616. * \brief Update Coor_info structure
  617. *
  618. * \param Map pointer to Map_info structure
  619. * \param[out] Info pointer to Coor_info structure
  620. *
  621. * \return 1 on success
  622. * \return 0 on error
  623. */
  624. int Vect_coor_info(const struct Map_info *Map, struct Coor_info *Info)
  625. {
  626. char buf[2000], path[2000];
  627. struct stat stat_buf;
  628. switch (Map->format) {
  629. case GV_FORMAT_NATIVE:
  630. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  631. G__file_name(path, buf, GV_COOR_ELEMENT, Map->mapset);
  632. G_debug(1, "get coor info: %s", path);
  633. if (0 != stat(path, &stat_buf)) {
  634. G_warning(_("Unable to stat file <%s>"), path);
  635. Info->size = -1L;
  636. Info->mtime = -1L;
  637. }
  638. else {
  639. Info->size = (off_t) stat_buf.st_size; /* file size */
  640. Info->mtime = (long)stat_buf.st_mtime; /* last modified time */
  641. }
  642. /* stat does not give correct size on MINGW
  643. * if the file is opened */
  644. #ifdef __MINGW32__
  645. if (Map->open == VECT_OPEN_CODE) {
  646. dig_fseek(&(Map->dig_fp), 0L, SEEK_END);
  647. G_debug(2, "dig_ftell = %d", dig_ftell(&(Map->dig_fp)));
  648. Info->size = dig_ftell(&(Map->dig_fp));
  649. }
  650. #endif
  651. break;
  652. case GV_FORMAT_OGR:
  653. Info->size = 0L;
  654. Info->mtime = 0L;
  655. break;
  656. }
  657. G_debug(1, "Info->size = %lu, Info->mtime = %ld",
  658. (unsigned long)Info->size, Info->mtime);
  659. return 1;
  660. }
  661. /*!
  662. * \brief Gets vector map format (as string)
  663. *
  664. * Note: string is allocated by G_store(). Free allocated memory with
  665. * G_free().
  666. *
  667. * Currently are implemeted:
  668. * - Native format (native)
  669. * - OGR format (ogr)
  670. *
  671. * \param Map pointer to Map_info structure
  672. *
  673. * \return maptype string on success
  674. * \return error message on error
  675. */
  676. const char *Vect_maptype_info(const struct Map_info *Map)
  677. {
  678. char maptype[1000];
  679. switch (Map->format) {
  680. case GV_FORMAT_NATIVE:
  681. sprintf(maptype, "native");
  682. break;
  683. case GV_FORMAT_OGR:
  684. sprintf(maptype, "ogr");
  685. break;
  686. default:
  687. sprintf(maptype, _("unknown %d (update Vect_maptype_info)"),
  688. Map->format);
  689. }
  690. return G_store(maptype);
  691. }
  692. /*!
  693. \brief Gets vector map format
  694. Currently are implemeted:
  695. - Native format (GV_FORMAT_NATIVE)
  696. - OGR format (GV_FORMAT_OGR)
  697. \param Map pointer to Map_info structure
  698. \return maptype code
  699. */
  700. int Vect_maptype(const struct Map_info *Map)
  701. {
  702. return Map->format;
  703. }
  704. /*!
  705. * \brief Open topology file ('topo')
  706. *
  707. * \param[in,out] Map pointer to Map_info structure
  708. * \param head_only open only head
  709. *
  710. * \return 0 on success
  711. * \return 1 file does not exist
  712. * \return -1 on error
  713. */
  714. int Vect_open_topo(struct Map_info *Map, int head_only)
  715. {
  716. int err, ret;
  717. char buf[500], file_path[2000];
  718. struct gvfile fp;
  719. struct Coor_info CInfo;
  720. struct Plus_head *Plus;
  721. struct stat info;
  722. G_debug(1, "Vect_open_topo(): name = %s mapset= %s", Map->name,
  723. Map->mapset);
  724. Plus = &(Map->plus);
  725. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  726. G__file_name(file_path, buf, GV_TOPO_ELEMENT, Map->mapset);
  727. if (stat(file_path, &info) != 0) /* does not exist */
  728. return 1;
  729. dig_file_init(&fp);
  730. fp.file = G_fopen_old(buf, GV_TOPO_ELEMENT, Map->mapset);
  731. if (fp.file == NULL) { /* topo file is not available */
  732. G_debug(1, "Cannot open topo file for vector '%s@%s'.",
  733. Map->name, Map->mapset);
  734. return -1;
  735. }
  736. /* get coor info */
  737. Vect_coor_info(Map, &CInfo);
  738. /* load head */
  739. if (dig_Rd_Plus_head(&fp, Plus) == -1)
  740. return -1;
  741. G_debug(1, "Topo head: coor size = %lu, coor mtime = %ld",
  742. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  743. /* do checks */
  744. err = 0;
  745. if (CInfo.size != Plus->coor_size) {
  746. G_warning(_("Size of 'coor' file differs from value saved in topology file"));
  747. err = 1;
  748. }
  749. /* Do not check mtime because mtime is changed by copy */
  750. /*
  751. if ( CInfo.mtime != Plus->coor_mtime ) {
  752. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  753. err = 1;
  754. }
  755. */
  756. if (err) {
  757. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  758. Map->name, Map->mapset);
  759. return -1;
  760. }
  761. /* load file to the memory */
  762. /* dig_file_load ( &fp); */
  763. /* load topo to memory */
  764. ret = dig_load_plus(Plus, &fp, head_only);
  765. fclose(fp.file);
  766. /* dig_file_free ( &fp); */
  767. if (ret == 0)
  768. return -1;
  769. return 0;
  770. }
  771. /*!
  772. * \brief Open spatial index file ('sidx')
  773. *
  774. * \param[in,out] Map pointer to Map_info
  775. * \param mode 0 old, 1 update, 2 new
  776. *
  777. * \return 0 on success
  778. * \return -1 on error
  779. */
  780. int Vect_open_sidx(struct Map_info *Map, int mode)
  781. {
  782. char buf[500], file_path[2000];
  783. int err;
  784. struct Coor_info CInfo;
  785. struct Plus_head *Plus;
  786. struct stat info;
  787. G_debug(1, "Vect_open_sidx(): name = %s mapset= %s mode = %s", Map->name,
  788. Map->mapset, mode == 0 ? "old" : (mode == 1 ? "update" : "new"));
  789. if (Map->plus.Spidx_built == 1) {
  790. G_warning("Spatial index already opened");
  791. return 0;
  792. }
  793. Plus = &(Map->plus);
  794. dig_file_init(&(Map->plus.spidx_fp));
  795. if (mode < 2) {
  796. sprintf(buf, "%s/%s", GV_DIRECTORY, Map->name);
  797. G__file_name(file_path, buf, GV_SIDX_ELEMENT, Map->mapset);
  798. if (stat(file_path, &info) != 0) /* does not exist */
  799. return 1;
  800. Map->plus.spidx_fp.file =
  801. G_fopen_old(buf, GV_SIDX_ELEMENT, Map->mapset);
  802. if (Map->plus.spidx_fp.file == NULL) { /* sidx file is not available */
  803. G_debug(1, "Cannot open spatial index file for vector '%s@%s'.",
  804. Map->name, Map->mapset);
  805. return -1;
  806. }
  807. /* get coor info */
  808. Vect_coor_info(Map, &CInfo);
  809. /* initialize spatial index */
  810. Map->plus.Spidx_new = 0;
  811. /* load head */
  812. if (dig_Rd_spidx_head(&(Map->plus.spidx_fp), Plus) == -1) {
  813. fclose(Map->plus.spidx_fp.file);
  814. return -1;
  815. }
  816. G_debug(1, "Sidx head: coor size = %lu, coor mtime = %ld",
  817. (unsigned long)Plus->coor_size, Plus->coor_mtime);
  818. /* do checks */
  819. err = 0;
  820. if (CInfo.size != Plus->coor_size) {
  821. G_warning(_("Size of 'coor' file differs from value saved in sidx file"));
  822. err = 1;
  823. }
  824. /* Do not check mtime because mtime is changed by copy */
  825. /*
  826. if ( CInfo.mtime != Plus->coor_mtime ) {
  827. G_warning ( "Time of last modification for 'coor' file differs from value saved in topo file.\n");
  828. err = 1;
  829. }
  830. */
  831. if (err) {
  832. G_warning(_("Please rebuild topology for vector map <%s@%s>"),
  833. Map->name, Map->mapset);
  834. fclose(Map->plus.spidx_fp.file);
  835. return -1;
  836. }
  837. }
  838. if (mode) {
  839. /* open new spatial index */
  840. Map->plus.Spidx_new = 1;
  841. if (mode == 1) {
  842. /* load spatial index for update */
  843. if (dig_Rd_spidx(&(Map->plus.spidx_fp), Plus) == -1) {
  844. fclose(Map->plus.spidx_fp.file);
  845. return -1;
  846. }
  847. }
  848. }
  849. Map->plus.Spidx_built = 1;
  850. return 0;
  851. }